use of org.bson.io.ByteBufferBsonInput 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.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testWriteRead.
@Test
public //CHECKSTYLE:OFF
void testWriteRead() throws IOException {
ObjectId oid1 = new ObjectId();
writer.writeStartDocument();
{
writer.writeBoolean("b1", true);
writer.writeBoolean("b2", false);
writer.writeStartArray("a1");
{
writer.writeString("danke");
writer.writeString("");
}
writer.writeEndArray();
writer.writeStartDocument("d1");
{
writer.writeDouble("do", 60);
writer.writeInt32("i32", 40);
writer.writeInt64("i64", Long.MAX_VALUE);
}
writer.writeEndDocument();
writer.writeJavaScriptWithScope("js1", "print x");
writer.writeStartDocument();
{
writer.writeInt32("x", 1);
}
writer.writeEndDocument();
writer.writeObjectId("oid1", oid1);
}
writer.writeEndDocument();
assertEquals(139, buffer.getPosition());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffer.pipe(baos);
ByteBufferBsonInput basicInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer);
try {
assertEquals(BsonType.DOCUMENT, reader.readBsonType());
reader.readStartDocument();
{
assertEquals(BsonType.BOOLEAN, reader.readBsonType());
assertEquals("b1", reader.readName());
assertEquals(true, reader.readBoolean());
assertEquals(BsonType.BOOLEAN, reader.readBsonType());
assertEquals("b2", reader.readName());
assertEquals(false, reader.readBoolean());
assertEquals(BsonType.ARRAY, reader.readBsonType());
assertEquals("a1", reader.readName());
reader.readStartArray();
{
assertEquals(BsonType.STRING, reader.readBsonType());
assertEquals("danke", reader.readString());
assertEquals(BsonType.STRING, reader.readBsonType());
assertEquals("", reader.readString());
}
assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
reader.readEndArray();
assertEquals("d1", reader.readName());
reader.readStartDocument();
{
assertEquals(BsonType.DOUBLE, reader.readBsonType());
assertEquals("do", reader.readName());
assertEquals(60, reader.readDouble(), 0);
assertEquals(BsonType.INT32, reader.readBsonType());
assertEquals("i32", reader.readName());
assertEquals(40, reader.readInt32());
assertEquals(BsonType.INT64, reader.readBsonType());
assertEquals("i64", reader.readName());
assertEquals(Long.MAX_VALUE, reader.readInt64());
}
assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
reader.readEndDocument();
assertEquals(BsonType.JAVASCRIPT_WITH_SCOPE, reader.readBsonType());
assertEquals("js1", reader.readName());
assertEquals("print x", reader.readJavaScriptWithScope());
reader.readStartDocument();
{
assertEquals(BsonType.INT32, reader.readBsonType());
assertEquals("x", reader.readName());
assertEquals(1, reader.readInt32());
}
assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
reader.readEndDocument();
assertEquals(BsonType.OBJECT_ID, reader.readBsonType());
assertEquals("oid1", reader.readName());
assertEquals(oid1, reader.readObjectId());
assertEquals(BsonType.END_OF_DOCUMENT, reader.readBsonType());
reader.readEndDocument();
}
} finally {
reader.close();
}
}
use of org.bson.io.ByteBufferBsonInput 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.ByteBufferBsonInput 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.ByteBufferBsonInput in project mongo-java-driver by mongodb.
the class DocumentCodecTest method createInputBuffer.
// TODO: factor into common base class;
private BsonInput createInputBuffer() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
buffer.pipe(baos);
return new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
}
Aggregations