use of org.bson.io.OutputBuffer 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.OutputBuffer in project mongo-java-driver by mongodb.
the class BasicDBObject method toBson.
/**
* Convert the object to its BSON representation, using the {@code STANDARD} representation for UUID. This is safe to do in the context
* of this class because currently this method is only used for equality and hash code, and is not passed to any other parts of the
* library.
*/
private static byte[] toBson(final BasicDBObject dbObject) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
DEFAULT_CODEC.encode(new BsonBinaryWriter(outputBuffer), dbObject, EncoderContext.builder().build());
return outputBuffer.toByteArray();
}
use of org.bson.io.OutputBuffer 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.OutputBuffer in project mongo-java-driver by mongodb.
the class BSONTest method checkEncodingAndDecoding.
private void checkEncodingAndDecoding(final BSONObject toEncodeAndDecode, final int expectedEncodedSize, final String expectedHash) throws IOException {
// check encoding
BSONEncoder bsonEncoder = new BasicBSONEncoder();
OutputBuffer buf = new BasicOutputBuffer();
bsonEncoder.set(buf);
bsonEncoder.putObject(toEncodeAndDecode);
assertEquals(expectedEncodedSize, buf.size());
assertEquals(expectedHash, hexMD5(buf.toByteArray()));
bsonEncoder.done();
// check decoding
BSONDecoder bsonDecoder = new BasicBSONDecoder();
BSONCallback callback = new BasicBSONCallback();
int numberOfBytesDecoded = bsonDecoder.decode(new ByteArrayInputStream(buf.toByteArray()), callback);
assertEquals(expectedEncodedSize, numberOfBytesDecoded);
assertEquals(callback.get(), toEncodeAndDecode);
// I believe this is an obscure way of checking the decoded object is the the one we expect
OutputBuffer buf2 = new BasicOutputBuffer();
bsonEncoder.set(buf2);
bsonEncoder.putObject((BSONObject) callback.get());
assertEquals(expectedEncodedSize, buf2.size());
assertEquals(expectedHash, hexMD5(buf2.toByteArray()));
}
use of org.bson.io.OutputBuffer in project mongo-java-driver by mongodb.
the class BSONTest method encodeDocumentToByteArray.
private byte[] encodeDocumentToByteArray(final BSONObject document) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
BSONEncoder encoder = new BasicBSONEncoder();
encoder.set(outputBuffer);
encoder.putObject(document);
encoder.done();
return outputBuffer.toByteArray();
}
Aggregations