@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(); } }
BsonBinaryWriter binaryWriter = new BsonBinaryWriter(buffer); new DocumentCodec().encode(binaryWriter, tr.internalDoc, EncoderContext.builder().build()); int length = binaryWriter.getBsonOutput().getSize();
@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(); } }