@@ -148,48 +148,43 @@ public final class CommandMessage extends RequestMessage {
148148 * `PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE` sections.
149149 */
150150 BsonDocument getCommandDocument (final ByteBufferBsonOutput bsonOutput ) {
151- List < ByteBuf > byteBuffers = bsonOutput .getByteBuffers ();
151+ CompositeByteBuf byteBuf = new CompositeByteBuf ( bsonOutput .getByteBuffers () );
152152 try {
153- CompositeByteBuf byteBuf = new CompositeByteBuf (byteBuffers );
154- try {
155- byteBuf .position (firstDocumentPosition );
156- ByteBufBsonDocument byteBufBsonDocument = createOne (byteBuf );
157-
158- // If true, it means there is at least one `PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE` section in the OP_MSG
159- if (byteBuf .hasRemaining ()) {
160- BsonDocument commandBsonDocument = byteBufBsonDocument .toBaseBsonDocument ();
161-
162- // Each loop iteration processes one Document Sequence
163- // When there are no more bytes remaining, there are no more Document Sequences
164- while (byteBuf .hasRemaining ()) {
165- // skip reading the payload type, we know it is `PAYLOAD_TYPE_1`
166- byteBuf .position (byteBuf .position () + 1 );
167- int sequenceStart = byteBuf .position ();
168- int sequenceSizeInBytes = byteBuf .getInt ();
169- int sectionEnd = sequenceStart + sequenceSizeInBytes ;
170-
171- String fieldName = getSequenceIdentifier (byteBuf );
172- // If this assertion fires, it means that the driver has started using document sequences for nested fields. If
173- // so, this method will need to change in order to append the value to the correct nested document.
174- assertFalse (fieldName .contains ("." ));
175-
176- ByteBuf documentsByteBufSlice = byteBuf .duplicate ().limit (sectionEnd );
177- try {
178- commandBsonDocument .append (fieldName , new BsonArray (createList (documentsByteBufSlice )));
179- } finally {
180- documentsByteBufSlice .release ();
181- }
182- byteBuf .position (sectionEnd );
153+ byteBuf .position (firstDocumentPosition );
154+ ByteBufBsonDocument byteBufBsonDocument = createOne (byteBuf );
155+
156+ // If true, it means there is at least one `PAYLOAD_TYPE_1_DOCUMENT_SEQUENCE` section in the OP_MSG
157+ if (byteBuf .hasRemaining ()) {
158+ BsonDocument commandBsonDocument = byteBufBsonDocument .toBaseBsonDocument ();
159+
160+ // Each loop iteration processes one Document Sequence
161+ // When there are no more bytes remaining, there are no more Document Sequences
162+ while (byteBuf .hasRemaining ()) {
163+ // skip reading the payload type, we know it is `PAYLOAD_TYPE_1`
164+ byteBuf .position (byteBuf .position () + 1 );
165+ int sequenceStart = byteBuf .position ();
166+ int sequenceSizeInBytes = byteBuf .getInt ();
167+ int sectionEnd = sequenceStart + sequenceSizeInBytes ;
168+
169+ String fieldName = getSequenceIdentifier (byteBuf );
170+ // If this assertion fires, it means that the driver has started using document sequences for nested fields. If
171+ // so, this method will need to change in order to append the value to the correct nested document.
172+ assertFalse (fieldName .contains ("." ));
173+
174+ ByteBuf documentsByteBufSlice = byteBuf .duplicate ().limit (sectionEnd );
175+ try {
176+ commandBsonDocument .append (fieldName , new BsonArray (createList (documentsByteBufSlice )));
177+ } finally {
178+ documentsByteBufSlice .release ();
183179 }
184- return commandBsonDocument ;
185- } else {
186- return byteBufBsonDocument ;
180+ byteBuf .position (sectionEnd );
187181 }
188- } finally {
189- byteBuf .release ();
182+ return commandBsonDocument ;
183+ } else {
184+ return byteBufBsonDocument ;
190185 }
191186 } finally {
192- byteBuffers . forEach ( ByteBuf :: release );
187+ byteBuf . release ( );
193188 }
194189 }
195190
0 commit comments