use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeDocumentIntoScopeDocument.
@Test
public void testPipeDocumentIntoScopeDocument() {
writer.writeStartDocument();
writer.writeInt32("i", 0);
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.writeStartDocument();
newWriter.writeJavaScriptWithScope("js", "i++");
newWriter.pipe(reader1);
newWriter.writeEndDocument();
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
reader2.readJavaScriptWithScope("js");
reader2.readStartDocument();
assertEquals(0, reader2.readInt32("i"));
reader2.readEndDocument();
reader2.readEndDocument();
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method setup.
@Before
public void setup() {
buffer = new BasicOutputBuffer();
writer = new BsonBinaryWriter(new BsonWriterSettings(100), new BsonBinaryWriterSettings(1024), buffer);
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeOfDocumentWithInvalidSize.
@Test
public void testPipeOfDocumentWithInvalidSize() {
// minimum document size is 5;
byte[] bytes = { 4, 0, 0, 0 };
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);
fail("Pipe is expected to fail with document size is < 5");
} catch (BsonSerializationException e) {
// expected
} finally {
reader.close();
}
} finally {
newWriter.close();
}
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class DBEncoderAdapter method encode.
// TODO: this can be optimized to reduce copying of buffers. For that we'd need an InputBuffer that could iterate
// over an array of ByteBuffer instances from a PooledByteBufferOutputBuffer
@Override
public void encode(final BsonWriter writer, final DBObject document, final EncoderContext encoderContext) {
BasicOutputBuffer buffer = new BasicOutputBuffer();
try {
encoder.writeObject(buffer, document);
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(buffer.toByteArray()))));
try {
writer.pipe(reader);
} finally {
reader.close();
}
} finally {
buffer.close();
}
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BasicBSONEncoder method encode.
@Override
public byte[] encode(final BSONObject document) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
set(outputBuffer);
putObject(document);
done();
return outputBuffer.toByteArray();
}
Aggregations