use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeNestedDocument.
@Test
public void testPipeNestedDocument() {
// {
// "value" : { "a" : true},
// "b" : 2
// }
writer.writeStartDocument();
writer.writeStartDocument("value");
writer.writeBoolean("a", true);
writer.writeEndDocument();
writer.writeInt32("b", 2);
writer.writeEndDocument();
byte[] bytes = buffer.toByteArray();
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
reader1.readStartDocument();
reader1.readName();
//pipe {'a':true} to writer
newWriter.pipe(reader1);
//continue reading from the same reader
assertEquals(BsonType.INT32, reader1.readBsonType());
assertEquals("b", reader1.readName());
assertEquals(2, reader1.readInt32());
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
assertEquals(BsonType.BOOLEAN, reader2.readBsonType());
assertEquals("a", reader2.readName());
assertEquals(true, reader2.readBoolean());
reader2.readEndDocument();
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipe.
//CHECKSTYLE:ON
@Test
public void testPipe() {
writer.writeStartDocument();
writer.writeBoolean("a", true);
writer.writeEndDocument();
byte[] bytes = buffer.toByteArray();
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
try {
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
try {
newWriter.pipe(reader);
} finally {
reader.close();
}
} finally {
newWriter.close();
}
assertArrayEquals(bytes, newBuffer.toByteArray());
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeDocumentIntoTopLevel.
@Test
public void testPipeDocumentIntoTopLevel() {
writer.writeStartDocument();
writer.writeString("str", "value");
writer.writeEndDocument();
byte[] bytes = buffer.toByteArray();
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
newWriter.pipe(reader1);
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
assertEquals("value", reader2.readString("str"));
reader2.readEndDocument();
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.
static <T> BsonBinaryReader prepareReaderWithObjectToBeDecoded(final T objectToDecode, final Codec<T> codec) {
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
byte[] documentAsByteArrayForReader;
try {
codec.encode(writer, objectToDecode, EncoderContext.builder().build());
documentAsByteArrayForReader = outputBuffer.toByteArray();
} finally {
writer.close();
}
return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class DocumentCodecTest method setUp.
@Before
public void setUp() throws Exception {
buffer = new BasicOutputBuffer();
writer = new BsonBinaryWriter(buffer);
}
Aggregations