use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeDocumentIntoArray.
@Test
public void testPipeDocumentIntoArray() {
writer.writeStartDocument();
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.writeStartArray("a");
newWriter.pipe(reader1);
newWriter.writeEndArray();
newWriter.writeEndDocument();
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
reader2.readStartArray();
reader2.readStartDocument();
reader2.readEndDocument();
reader2.readEndArray();
reader2.readEndDocument();
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonHelper method toBson.
public static ByteBuffer toBson(final BsonDocument document) {
BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
new BsonDocumentCodec().encode(new BsonBinaryWriter(bsonOutput), document, EncoderContext.builder().build());
return ByteBuffer.wrap(bsonOutput.toByteArray());
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class GenericBsonDecimal128Test method encodeToHex.
private String encodeToHex(final BsonDocument decodedDocument) {
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
new BsonDocumentCodec().encode(new BsonBinaryWriter(outputBuffer), decodedDocument, EncoderContext.builder().build());
return DatatypeConverter.printHexBinary(outputBuffer.toByteArray());
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class MessageHelper method encodeJson.
private static ByteBuf encodeJson(final String json) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
JsonReader jsonReader = new JsonReader(json);
BsonDocumentCodec codec = new BsonDocumentCodec();
BsonDocument document = codec.decode(jsonReader, DecoderContext.builder().build());
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
codec.encode(writer, document, EncoderContext.builder().build());
ByteBuffer documentByteBuffer = ByteBuffer.allocate(outputBuffer.size());
documentByteBuffer.put(outputBuffer.toByteArray());
return new ByteBufNIO(documentByteBuffer);
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BSONTest method shouldUseCustomEncodersWhenDecodingObjectOfRegisteredClass.
@Test
public void shouldUseCustomEncodersWhenDecodingObjectOfRegisteredClass() throws IOException {
// given
StubTransformer stubTransformer = new StubTransformer();
BSON.addEncodingHook(TestDate.class, stubTransformer);
BSONEncoder encoder = new BasicBSONEncoder();
encoder.set(new BasicOutputBuffer());
BSONObject document = new BasicBSONObject("date", new TestDate(2009, 1, 23, 10, 53, 42));
// when
encoder.putObject(document);
encoder.done();
// then
assertThat(stubTransformer.transformCalled, is(true));
}
Aggregations