Search in sources :

Example 1 with BsonBinaryWriter

use of org.bson.BsonBinaryWriter 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;
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) Document(org.bson.Document) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 2 with BsonBinaryWriter

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

the class DBDecoderAdapter method decode.

@Override
public DBObject decode(final BsonReader reader, final DecoderContext decoderContext) {
    ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(bufferProvider);
    BsonBinaryWriter binaryWriter = new BsonBinaryWriter(bsonOutput);
    try {
        binaryWriter.pipe(reader);
        BufferExposingByteArrayOutputStream byteArrayOutputStream = new BufferExposingByteArrayOutputStream(binaryWriter.getBsonOutput().getSize());
        bsonOutput.pipe(byteArrayOutputStream);
        return decoder.decode(byteArrayOutputStream.getInternalBytes(), collection);
    } catch (IOException e) {
        // impossible with a byte array output stream
        throw new MongoInternalException("An unlikely IOException thrown.", e);
    } finally {
        binaryWriter.close();
        bsonOutput.close();
    }
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) IOException(java.io.IOException) ByteBufferBsonOutput(com.mongodb.connection.ByteBufferBsonOutput)

Example 3 with BsonBinaryWriter

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

the class BaseWriteCommandMessage method encodeMessageBodyWithMetadata.

@Override
protected EncodingMetadata encodeMessageBodyWithMetadata(final BsonOutput outputStream, final int messageStartPosition) {
    BaseWriteCommandMessage nextMessage = null;
    writeCommandHeader(outputStream);
    int commandStartPosition = outputStream.getPosition();
    int firstDocumentStartPosition = outputStream.getPosition();
    BsonBinaryWriter writer = new BsonBinaryWriter(new BsonWriterSettings(), new BsonBinaryWriterSettings(getSettings().getMaxDocumentSize() + HEADROOM), outputStream, getFieldNameValidator());
    try {
        writer.writeStartDocument();
        writeCommandPrologue(writer);
        nextMessage = writeTheWrites(outputStream, commandStartPosition, writer);
        writer.writeEndDocument();
    } finally {
        writer.close();
    }
    return new EncodingMetadata(nextMessage, firstDocumentStartPosition);
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonBinaryWriterSettings(org.bson.BsonBinaryWriterSettings) BsonWriterSettings(org.bson.BsonWriterSettings)

Example 4 with BsonBinaryWriter

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

the class ClientMetadataHelper method clientMetadataDocumentTooLarge.

static boolean clientMetadataDocumentTooLarge(final BsonDocument document) {
    BasicOutputBuffer buffer = new BasicOutputBuffer(MAXIMUM_CLIENT_METADATA_ENCODED_SIZE);
    new BsonDocumentCodec().encode(new BsonBinaryWriter(buffer), document, EncoderContext.builder().build());
    return buffer.getPosition() > MAXIMUM_CLIENT_METADATA_ENCODED_SIZE;
}
Also used : BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 5 with BsonBinaryWriter

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

the class RawBsonDocumentCodec method decode.

@Override
public RawBsonDocument decode(final BsonReader reader, final DecoderContext decoderContext) {
    BasicOutputBuffer buffer = new BasicOutputBuffer(0);
    BsonBinaryWriter writer = new BsonBinaryWriter(buffer);
    try {
        writer.pipe(reader);
        return new RawBsonDocument(buffer.getInternalBuffer(), 0, buffer.getPosition());
    } finally {
        writer.close();
        buffer.close();
    }
}
Also used : RawBsonDocument(org.bson.RawBsonDocument) BsonBinaryWriter(org.bson.BsonBinaryWriter) 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