use of org.bson.io.BasicOutputBuffer in project mongo-hadoop by mongodb.
the class BSONWritable method write.
/**
* {@inheritDoc}
*
* @see Writable#write(DataOutput)
*/
public void write(final DataOutput out) throws IOException {
BSONEncoder enc = new BasicBSONEncoder();
BasicOutputBuffer buf = new BasicOutputBuffer();
enc.set(buf);
enc.putObject(doc);
enc.done();
buf.pipe(new DataOutputOutputStreamAdapter(out));
}
use of org.bson.io.BasicOutputBuffer in project mongo-hadoop by mongodb.
the class MongoUpdateWritable method write.
/**
* /** {@inheritDoc}
*
* @see Writable#write(DataOutput)
*/
public void write(final DataOutput out) throws IOException {
BasicOutputBuffer buf = new BasicOutputBuffer();
enc.set(buf);
enc.putObject(query);
enc.done();
enc.set(buf);
enc.putObject(modifiers);
enc.done();
buf.pipe(new DataOutputOutputStreamAdapter(out));
out.writeBoolean(upsert);
out.writeBoolean(multiUpdate);
out.writeBoolean(replace);
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class BsonBinaryWriterTest method testPipeDocumentIntoDocument.
@Test
public void testPipeDocumentIntoDocument() {
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.writeStartDocument();
newWriter.writeName("doc");
newWriter.pipe(reader1);
newWriter.writeEndDocument();
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer.toByteArray()))));
//checking what writer piped
reader2.readStartDocument();
assertEquals("doc", reader2.readName());
reader2.readStartDocument();
assertEquals("value", reader2.readString("str"));
reader2.readEndDocument();
reader2.readEndDocument();
}
use of org.bson.io.BasicOutputBuffer in project mongo-java-driver by mongodb.
the class GenericBsonTest 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 CodecTestUtil method prepareReaderWithObjectToBeDecoded.
static BsonBinaryReader prepareReaderWithObjectToBeDecoded(final Object objectToDecode) {
//Need to encode it wrapped in a document to conform to the validation
Document document = new Document("wrapperDocument", objectToDecode);
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
byte[] documentAsByteArrayForReader;
try {
new DocumentCodec().encode(writer, document, EncoderContext.builder().build());
documentAsByteArrayForReader = outputBuffer.toByteArray();
} finally {
writer.close();
}
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
//have to read off the wrapper document so the reader is in the correct position for the test
reader.readStartDocument();
reader.readName();
return reader;
}
Aggregations