Java 类org.bson.io.BasicOutputBuffer 实例源码

项目:bsoncodec    文件:AbstractTest.java   
@SuppressWarnings("resource")
protected <T> void writeReadCompare(T source, Codec<T> codec) {
    BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(bsonOutput);
    writer.writeStartDocument();
    writer.writeName("name");
    codec.encode(writer, source, EncoderContext.builder().build());
    writer.writeEndDocument();
    writer.close();

    BsonBinaryReader reader = new BsonBinaryReader(
            ByteBuffer.wrap(bsonOutput.toByteArray()));
    reader.readStartDocument();
    assertThat(reader.readName()).isEqualTo("name");
    T readNow = codec.decode(reader, DecoderContext.builder().build());

    assertThat(readNow).isEqualTo(source);
}
项目:bson-codecs-jsr310    文件:CodecsTests.java   
private <T> void testCodec(Codec<T> codec, T value) {
    try (BasicOutputBuffer output = new BasicOutputBuffer()) {

        encodeValue(output, codec, value);

        T decoded = decodeValue(
                wrap(output.toByteArray()),
                codec
        );

        assertEquals(value, decoded);
    }
}
项目:BsonMapper    文件:DefaultBsonMapper.java   
@Override
public BsonOutput writeToBsonOutput(Object object) {
    if (object == null) {
        return null;
    }
    BsonOutput bsonOutput = new BasicOutputBuffer();
    BsonWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(bsonMapperConfig.getMaxMapperLayerNum()),
            new BsonBinaryWriterSettings(bsonMapperConfig.getMaxDocumentSizeForBsonWriter()), bsonOutput);
    BsonValueConverterRepertory.getBsonDocumentConverter().encode(bsonBinaryWriter, object);
    return bsonOutput;
}
项目:BsonMapper    文件:BsonDocumentConverterTest.java   
@Test
public void testByteIOEncode() throws Exception {
    BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
    BsonWriter bsonWriter = new BsonBinaryWriter(bsonOutput);
    BsonValueConverterRepertory.getBsonDocumentConverter().encode(bsonWriter, getObject());
    BsonReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(bsonOutput.getByteBuffers().get(0)));
    BsonTest bsonTest = BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonReader, BsonTest.class, BsonMapperConfig.DEFALUT);
    System.out.println(bsonTest);
}
项目:BsonMapper    文件:BsonMapperTest.java   
@Test
public void testWriteToOutput() throws Exception {
    BsonTest bsonObject = getBsonObject();
    BasicOutputBuffer bsonOutput = (BasicOutputBuffer) bsonMapper.writeToBsonOutput(bsonObject);
    byte[] internalBuffer = bsonOutput.getInternalBuffer();
    byte[] expectedBytes = getBsonByte();
    Assert.assertArrayEquals("bytes should be equals", expectedBytes, internalBuffer);
}
项目:chariot    文件:BSONEncoder.java   
public byte[] encode( BSONObject o ){
    BasicOutputBuffer buf = new BasicOutputBuffer();
    set( buf );
    putObject( o );
    done();
    return buf.toByteArray();
}
项目:java-code-gen    文件:TestCodecs.java   
@Before
public void before() {
    buffer = new BasicOutputBuffer();
    writer = new BsonBinaryWriter(buffer);
    writer.writeStartDocument();
    writer.writeName("test");
}
项目:HZS.Durian    文件:BasicBSONEncoder.java   
public byte[] encode( BSONObject o ){
    BasicOutputBuffer buf = new BasicOutputBuffer();
    set( buf );
    putObject( o );
    done();
    return buf.toByteArray();
}
项目:lumongo    文件:LumongoUtil.java   
public static byte[] mongoDocumentToByteArray(Document mongoDocument) {
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    new DocumentCodec().encode(writer, mongoDocument, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
    return outputBuffer.toByteArray();
}
项目:immutables    文件:Jsons.java   
static org.bson.BsonReader asBsonReader(JsonObject gson) throws IOException {
  BasicOutputBuffer buffer = new BasicOutputBuffer();
  BsonWriter writer = new BsonWriter(new BsonBinaryWriter(buffer));
  TypeAdapters.JSON_ELEMENT.write(writer, gson);
  return new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
}
项目:immutables    文件:Jsons.java   
static JsonReader asGsonReader(BsonDocument bson) {
  BasicOutputBuffer output = new BasicOutputBuffer();
  new BsonDocumentCodec().encode(new BsonBinaryWriter(output), bson, EncoderContext.builder().build());
  return new BsonReader(new BsonBinaryReader(ByteBuffer.wrap(output.toByteArray())));
}
项目:vertx-native-mongo    文件:EncoderPerfTest.java   
@Benchmark
public Object encodeMongoTest() throws Exception {
    BasicOutputBuffer out = new BasicOutputBuffer(512);
    new BsonDocumentCodec().encode(new BsonBinaryWriter(out), doc1, EncoderContext.builder().build());
    return out.getByteBuffers();
}