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;
}
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();
}
}
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);
}
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;
}
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();
}
}
Aggregations