Package org.bson.io

Examples of org.bson.io.OutputBuffer


    request.setFuture(future);
   
    // チャネルを通して送信
    try {
      // バッファを確保
      OutputBuffer outputBuffer = new BasicOutputBuffer();
      BSONEncoder encoder = new BSONEncoder();
      encoder.set(outputBuffer);
      // リクエスト内容を出力
      writeRequest(request, encoder, outputBuffer);
     
      // Safeリクエストの場合は、 getLastError クエリを付加
      Request getLastError = null;
      BSONObject query = request.getConsistency().getLastErrorQuery();
      if (query != null) {
        getLastError = new Query(
            request.getDatabaseName(),
            "$cmd",
            0,
            1,
            query,
            null
        );
        getLastError.setFuture(future);
        writeRequest(getLastError, encoder, outputBuffer);
      }
     
      // ChannelBuffer を生成
      ChannelBuffer channelBuffer = ChannelBuffers.buffer(outputBuffer.size());
      outputBuffer.pipe(new ChannelBufferOutputStream(channelBuffer));
     
      // ChannelFuture を取得し RequestFuture に設定
      ChannelFuture channelFuture = e.getFuture();
      future.setChannelFuture(channelFuture);
     
View Full Code Here


        private byte[] encode(DBEncoderFactory factory, DBObject dbo) {
            Object id = dbo.get("_id");
            _checkKeys(dbo);
            DBEncoder encoder = factory.create();
            OutputBuffer buffer = new BasicOutputBuffer();
            encoder.writeObject(buffer, dbo);
            return buffer.toByteArray();
        }
View Full Code Here

        DBRef ref = new DBRef(getDatabase(), "hello", "world");
        DBObject o = new BasicDBObject("!", ref);

        DBEncoder encoder = DefaultDBEncoder.FACTORY.create();
        OutputBuffer buf = new BasicOutputBuffer();

        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

   
    void _test( BSONObject o , int size , String hash )
        throws IOException {
        BSONEncoder e = new BasicBSONEncoder();
        OutputBuffer buf = new BasicOutputBuffer();
        e.set( buf );
        e.putObject( o );
        assertEquals( size , buf.size() );
        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() );
        assertEquals( size , buf2.size() );
        assertEquals( hash , buf2.md5() );       
       
    }
View Full Code Here

    }

    @Test( expected =  IllegalArgumentException.class )
    public void testNullKeysFail() {
        BSONEncoder e = new BasicBSONEncoder();
        OutputBuffer buf = new BasicOutputBuffer();
        e.set( buf );
        e.putObject( new BasicBSONObject( "foo\0bar","baz" ) );
    }
View Full Code Here

        TestDate td = new TestDate( 2009 , 01 , 23 , 10 , 53 , 42 );
        BSONObject o = new BasicBSONObject( "date" , td );
        BSONEncoder e = new BasicBSONEncoder();
        BSONDecoder d = new BasicBSONDecoder();
        BSONCallback cb = new BasicBSONCallback();
        OutputBuffer buf = new BasicOutputBuffer();
        e.set( buf );
        boolean encodeFailed = false;
        try {
            e.putObject( o );
        }
        catch ( IllegalArgumentException ieE ) {
            encodeFailed = true;
        }
        assertTrue( encodeFailed);
        // Reset the buffer
        buf.seekStart();
        assertTrue( tf.transform( td ) instanceof java.util.Date );

        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

        Date dt = new Date( 2009 , 01 , 23 , 10 , 53 , 42 );
        BSONObject o = new BasicBSONObject( "date" , dt );
        BSONDecoder d = new BasicBSONDecoder();
        BSONEncoder e = new BasicBSONEncoder();
        BSONCallback cb = new BasicBSONCallback();
        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 );

        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

Related Classes of org.bson.io.OutputBuffer

Copyright © 2018 www.massapicom. 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.