Package org.bson.io

Examples of org.bson.io.BasicOutputBuffer.toByteArray()


    public byte[] encode( BSONObject o ){
        BasicOutputBuffer buf = new BasicOutputBuffer();
        set( buf );
        putObject( o );
        done();
        return buf.toByteArray();
    }

    public void set( OutputBuffer out ){
        if ( _buf != null )
            throw new IllegalStateException( "in the middle of something" );
View Full Code Here


            Object id = dbo.get("_id");
            _checkKeys(dbo);
            DBEncoder encoder = factory.create();
            OutputBuffer buffer = new BasicOutputBuffer();
            encoder.writeObject(buffer, dbo);
            return buffer.toByteArray();
        }

        private void _checkKeys(DBObject o) {
            for (String s : o.keySet()) {
                validateKey(s);
View Full Code Here

        DBEncoder encoder = BsonDBEncoder.FACTORY.create();
        BasicOutputBuffer buffer = new BasicOutputBuffer();

        encoder.writeObject(buffer, new LazyDBObject(new byte[]{5, 0, 0, 0, 0}, null));

        assertThat(buffer.toByteArray()).isEqualTo(new byte[]{5, 0, 0, 0, 0});

    }

    @Test
    public void shouldEncodeDBObject() throws Exception {
View Full Code Here

        assertEquals(lazyDBObj.getBSONSize(), outputBuffer.size());

        // this is just asserting that the encoder actually piped the correct bytes
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        lazyDBObj.pipe(baos);
        assertArrayEquals(baos.toByteArray(), outputBuffer.toByteArray());
    }

    private DBObject createSimpleTestDoc() {
        DBObject obj = new BasicDBObject("_id", new ObjectId());
        obj.put("first", 1);
View Full Code Here

    public byte[] encode( BSONObject o ){
        BasicOutputBuffer buf = new BasicOutputBuffer();
        set( buf );
        putObject( o );
        done();
        return buf.toByteArray();
    }

    @Override
    public void set( OutputBuffer out ){
        if ( _buf != null )
View Full Code Here

        encoder.writeObject(buf, o);

        DefaultDBCallback cb = new DefaultDBCallback( null );
        BSONDecoder decoder = new BasicBSONDecoder();
        decoder.decode( buf.toByteArray() , cb );
        DBObject read = cb.dbget();

        assertEquals("{\"!\":{\"$ref\":\"hello\",\"$id\":\"world\"}}", read.toString().replaceAll( " +" , "" ));
    }
View Full Code Here

        assertEquals( hash , buf.md5() );
        e.done();
       
        BSONDecoder d = new BasicBSONDecoder();
        BSONCallback cb = new BasicBSONCallback();
        int s = d.decode( new ByteArrayInputStream( buf.toByteArray() ) , cb );
        assertEquals( size , s );

        OutputBuffer buf2 = new BasicOutputBuffer();
        e.set( buf2 );
        e.putObject( (BSONObject)cb.get() );
View Full Code Here

        BSON.addEncodingHook( TestDate.class, tf );
        e.putObject( o );
        e.done();

        d.decode( new ByteArrayInputStream( buf.toByteArray() ), cb );
        Object result = cb.get();
        assertTrue( result instanceof BSONObject );
        BSONObject bson = (BSONObject) result;
        assertNotNull( bson.get( "date" ) );
        assertTrue( bson.get( "date" ) instanceof java.util.Date );
View Full Code Here

        OutputBuffer buf = new BasicOutputBuffer();
        e.set( buf );
        e.putObject( o );
        e.done();

        d.decode( new ByteArrayInputStream( buf.toByteArray() ), cb );
        Object result = cb.get();
        assertTrue( result instanceof BSONObject );
        BSONObject bson = (BSONObject) result;
        assertNotNull( bson.get( "date" ) );
        assertTrue( bson.get( "date" ) instanceof java.util.Date );
View Full Code Here

        assertNotNull( bson.get( "date" ) );
        assertTrue( bson.get( "date" ) instanceof java.util.Date );

        BSON.addDecodingHook( Date.class, tf );

        d.decode( new ByteArrayInputStream( buf.toByteArray() ), cb );
        bson = (BSONObject) cb.get();
        assertNotNull( bson.get( "date" ) );
        assertTrue( bson.get( "date" ) instanceof TestDate );
        assertEquals( bson.get( "date" ), td );
View Full Code Here

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.