Search in sources :

Example 6 with BsonBinaryWriter

use of org.bson.BsonBinaryWriter in project mongo-java-driver by mongodb.

the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.

static <T> BsonBinaryReader prepareReaderWithObjectToBeDecoded(final T objectToDecode, final Codec<T> codec) {
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    byte[] documentAsByteArrayForReader;
    try {
        codec.encode(writer, objectToDecode, EncoderContext.builder().build());
        documentAsByteArrayForReader = outputBuffer.toByteArray();
    } finally {
        writer.close();
    }
    return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 7 with BsonBinaryWriter

use of org.bson.BsonBinaryWriter in project mongo-java-driver by mongodb.

the class DocumentCodecTest method setUp.

@Before
public void setUp() throws Exception {
    buffer = new BasicOutputBuffer();
    writer = new BsonBinaryWriter(buffer);
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) Before(org.junit.Before)

Example 8 with BsonBinaryWriter

use of org.bson.BsonBinaryWriter 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)

Aggregations

BsonBinaryWriter (org.bson.BsonBinaryWriter)8 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)6 ByteBufNIO (org.bson.ByteBufNIO)3 BsonBinaryReader (org.bson.BsonBinaryReader)2 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)2 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)2 ByteBufferBsonOutput (com.mongodb.connection.ByteBufferBsonOutput)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 BsonBinaryWriterSettings (org.bson.BsonBinaryWriterSettings)1 BsonDocument (org.bson.BsonDocument)1 BsonWriterSettings (org.bson.BsonWriterSettings)1 Document (org.bson.Document)1 RawBsonDocument (org.bson.RawBsonDocument)1 OutputBuffer (org.bson.io.OutputBuffer)1 JsonReader (org.bson.json.JsonReader)1 Before (org.junit.Before)1