* Tests if large objects with byte arrays/streams/buffers can be saved in the database
* @throws Exception if something goes wrong
*/
@Test
public void largeObjectByte() throws Exception {
VCollection coll = _master.getLargeCollection("images");
byte[] test = new byte[1024 * 1024];
for (int i = 0; i < test.length; ++i) {
test[i] = (byte)(i & 0xFF);
}
Map<String, Object> obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", test);
coll.insert(obj);
VCursor vc = coll.find();
assertEquals(1, vc.size());
Map<String, Object> obj2 = vc.iterator().next();
assertEquals("Mona Lisa", obj2.get("name"));
assertArrayEquals(test, (byte[])obj2.get("data"));
ByteArrayInputStream bais = new ByteArrayInputStream(test);
obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", bais);
coll.insert(obj);
Map<String, Object> obj3 = coll.findOne(_factory.createDocument("uid", obj.get("uid")));
assertEquals("Mona Lisa", obj3.get("name"));
InputStream is3 = (InputStream)obj3.get("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[64 * 1024];
int read;
while ((read = is3.read(buf)) > 0) {
baos.write(buf, 0, read);
}
assertArrayEquals(test, baos.toByteArray());
ByteBuffer bb = ByteBuffer.wrap(test);
obj = new HashMap<String, Object>();
obj.put("name", "Mona Lisa");
obj.put("data", bb);
coll.insert(obj);
Map<String, Object> obj4 = coll.findOne(_factory.createDocument("uid", obj.get("uid")));
assertEquals("Mona Lisa", obj4.get("name"));
ByteBuffer bb4 = (ByteBuffer)obj4.get("data");
bb4.rewind();
byte[] test4 = new byte[bb4.remaining()];
bb4.get(test4);