Code example for ByteArrayOutputStream

Methods: toByteArray

        int size = encoder.writeObject(outputBuffer, lazyDBObj);
        assertEquals(lazyDBObj.getBSONSize(), size);
        assertEquals(lazyDBObj.getBSONSize(), outputBuffer.size());
        // this is just asserting that the encoder actually piped the correct bytes 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        assertArrayEquals(baos.toByteArray(), outputBuffer.toByteArray());
    private DBObject createSimpleTestDoc() { 
        DBObject obj = new BasicDBObject("_id", new ObjectId());
        obj.put("first", 1);
        obj.put("second", "str1");
        obj.put("third", true);
        obj.put("fourth", new BasicDBObject("firstNested", 1));
        return obj;
Stop searching for code, let great code find you!  Add Codota to your java IDE