Search in sources :

Example 6 with OutputBuffer

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);
}
Also used : BsonDocument(org.bson.BsonDocument) JsonReader(org.bson.json.JsonReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) ByteBuffer(java.nio.ByteBuffer) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 7 with OutputBuffer

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();
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) OutputBuffer(org.bson.io.OutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 8 with OutputBuffer

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);
}
Also used : BsonDocument(org.bson.BsonDocument) JsonReader(org.bson.json.JsonReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) ByteBuffer(java.nio.ByteBuffer) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 9 with OutputBuffer

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()));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 10 with OutputBuffer

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();
}
Also used : BasicOutputBuffer(org.bson.io.BasicOutputBuffer) OutputBuffer(org.bson.io.OutputBuffer) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Aggregations

BasicOutputBuffer (org.bson.io.BasicOutputBuffer)12 OutputBuffer (org.bson.io.OutputBuffer)12 BsonBinaryWriter (org.bson.BsonBinaryWriter)5 BsonDocument (org.bson.BsonDocument)3 ByteBuffer (java.nio.ByteBuffer)2 BsonWriter (org.bson.BsonWriter)2 ByteBufNIO (org.bson.ByteBufNIO)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 JsonReader (org.bson.json.JsonReader)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Test (org.junit.Test)1