Skip to content

Commit 6727d0a

Browse files
committed
Removed closeInput from BsonBinaryReader constructors
Removed closeOutput from BsonBinaryWriter constructors JAVA-1735
1 parent 342de66 commit 6727d0a

24 files changed

+107
-119
lines changed

bson/src/main/org/bson/BsonBinaryReader.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
package org.bson;
1818

1919
import org.bson.io.BsonInput;
20+
import org.bson.io.ByteBufferBsonInput;
2021
import org.bson.types.ObjectId;
2122

23+
import java.nio.ByteBuffer;
24+
2225
import static java.lang.String.format;
26+
import static org.bson.assertions.Assertions.notNull;
2327

2428
/**
2529
* A BsonReader implementation that reads from a binary stream of data. This is the most commonly used implementation.
@@ -29,31 +33,33 @@
2933
public class BsonBinaryReader extends AbstractBsonReader {
3034

3135
private final BsonInput bsonInput;
32-
private final boolean closeInput;
3336
private Mark mark;
3437

38+
/**
39+
* Construct an instance.
40+
*
41+
* @param byteBuffer the input for this reader
42+
*/
43+
public BsonBinaryReader(final ByteBuffer byteBuffer) {
44+
this(new ByteBufferBsonInput(new ByteBufNIO(notNull("byteBuffer", byteBuffer))));
45+
}
46+
3547
/**
3648
* Construct an instance.
3749
*
3850
* @param bsonInput the input for this reader
39-
* @param closeInput whether the reader should close the input when it is closed itself
4051
*/
41-
public BsonBinaryReader(final BsonInput bsonInput, final boolean closeInput) {
42-
super();
52+
public BsonBinaryReader(final BsonInput bsonInput) {
4353
if (bsonInput == null) {
4454
throw new IllegalArgumentException("bsonInput is null");
4555
}
4656
this.bsonInput = bsonInput;
47-
this.closeInput = closeInput;
4857
setContext(new Context(null, BsonContextType.TOP_LEVEL, 0, 0));
4958
}
5059

5160
@Override
5261
public void close() {
5362
super.close();
54-
if (closeInput) {
55-
bsonInput.close();
56-
}
5763
}
5864

5965
/**

bson/src/main/org/bson/BsonBinaryWriter.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class BsonBinaryWriter extends AbstractBsonWriter {
3333
private final BsonBinaryWriterSettings binaryWriterSettings;
3434

3535
private final BsonOutput bsonOutput;
36-
private final boolean closeOutput;
3736
private final Stack<Integer> maxDocumentSizeStack = new Stack<Integer>();
3837
private Mark mark;
3938

@@ -51,10 +50,9 @@ public BsonBinaryWriter(final BsonOutput bsonOutput, final FieldNameValidator va
5150
* Construct an instance.
5251
*
5352
* @param bsonOutput the output to write to
54-
* @param closeOutput whether to close the bsonOutput when it is closed itself
5553
*/
56-
public BsonBinaryWriter(final BsonOutput bsonOutput, final boolean closeOutput) {
57-
this(new BsonWriterSettings(), new BsonBinaryWriterSettings(), bsonOutput, closeOutput);
54+
public BsonBinaryWriter(final BsonOutput bsonOutput) {
55+
this(new BsonWriterSettings(), new BsonBinaryWriterSettings(), bsonOutput);
5856
}
5957

6058
/**
@@ -63,11 +61,10 @@ public BsonBinaryWriter(final BsonOutput bsonOutput, final boolean closeOutput)
6361
* @param settings the generic BsonWriter settings
6462
* @param binaryWriterSettings the settings specific to a BsonBinaryWriter
6563
* @param bsonOutput the output to write to
66-
* @param closeOutput whether to close the bsonOutput when it is closed itself
6764
*/
6865
public BsonBinaryWriter(final BsonWriterSettings settings, final BsonBinaryWriterSettings binaryWriterSettings,
69-
final BsonOutput bsonOutput, final boolean closeOutput) {
70-
this(settings, binaryWriterSettings, bsonOutput, new NoOpFieldNameValidator(), closeOutput);
66+
final BsonOutput bsonOutput) {
67+
this(settings, binaryWriterSettings, bsonOutput, new NoOpFieldNameValidator());
7168
}
7269

7370
/**
@@ -80,24 +77,15 @@ public BsonBinaryWriter(final BsonWriterSettings settings, final BsonBinaryWrite
8077
*/
8178
public BsonBinaryWriter(final BsonWriterSettings settings, final BsonBinaryWriterSettings binaryWriterSettings,
8279
final BsonOutput bsonOutput, final FieldNameValidator validator) {
83-
this(settings, binaryWriterSettings, bsonOutput, validator, false);
84-
}
85-
86-
private BsonBinaryWriter(final BsonWriterSettings settings, final BsonBinaryWriterSettings binaryWriterSettings,
87-
final BsonOutput bsonOutput, final FieldNameValidator validator, final boolean closeOutput) {
8880
super(settings, validator);
8981
this.binaryWriterSettings = binaryWriterSettings;
9082
this.bsonOutput = bsonOutput;
91-
this.closeOutput = closeOutput;
9283
maxDocumentSizeStack.push(binaryWriterSettings.getMaxDocumentSize());
9384
}
9485

9586
@Override
9687
public void close() {
9788
super.close();
98-
if (closeOutput) {
99-
bsonOutput.close();
100-
}
10189
}
10290

10391
/**

bson/src/main/org/bson/RawBsonDocument.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public RawBsonDocument(final byte[] bytes) {
6868
*/
6969
public <T> RawBsonDocument(final T document, final Codec<T> codec) {
7070
BasicOutputBuffer buffer = new BasicOutputBuffer();
71-
BsonBinaryWriter writer = new BsonBinaryWriter(buffer, true);
71+
BsonBinaryWriter writer = new BsonBinaryWriter(buffer);
7272
try {
7373
codec.encode(writer, document, EncoderContext.builder().build());
7474
this.bytes = buffer.toByteArray();
@@ -260,7 +260,7 @@ private BsonValue deserializeBsonValue(final BsonBinaryReader bsonReader) {
260260
}
261261

262262
private BsonBinaryReader createReader() {
263-
return new BsonBinaryReader(new ByteBufferBsonInput(getByteBuffer()), true);
263+
return new BsonBinaryReader(new ByteBufferBsonInput(getByteBuffer()));
264264
}
265265

266266
private BsonDocument toBsonDocument() {

bson/src/main/org/bson/codecs/RawBsonDocumentCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public RawBsonDocumentCodec() {
4444

4545
@Override
4646
public void encode(final BsonWriter writer, final RawBsonDocument value, final EncoderContext encoderContext) {
47-
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(value.getByteBuffer()), true);
47+
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(value.getByteBuffer()));
4848
try {
4949
writer.pipe(reader);
5050
} finally {
@@ -55,7 +55,7 @@ public void encode(final BsonWriter writer, final RawBsonDocument value, final E
5555
@Override
5656
public RawBsonDocument decode(final BsonReader reader, final DecoderContext decoderContext) {
5757
BasicOutputBuffer buffer = new BasicOutputBuffer();
58-
BsonBinaryWriter writer = new BsonBinaryWriter(buffer, true);
58+
BsonBinaryWriter writer = new BsonBinaryWriter(buffer);
5959
try {
6060
writer.pipe(reader);
6161
BufferExposingByteArrayOutputStream byteArrayOutputStream = new BufferExposingByteArrayOutputStream(writer.getBsonOutput()

bson/src/test/unit/org/bson/BsonBinaryReaderTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ public class BsonBinaryReaderTest {
3030
@Test
3131
public void testReadDBPointer() {
3232
BsonBinaryReader reader = createReaderForBytes(new byte[]{26, 0, 0, 0, 12, 97, 0, 2, 0, 0, 0, 98, 0, 82, 9, 41, 108,
33-
-42, -60, -29, -116, -7, 111, -1, -36, 0
34-
});
33+
-42, -60, -29, -116, -7, 111, -1, -36, 0});
3534

3635
reader.readStartDocument();
3736
assertThat(reader.readBsonType(), is(BsonType.DB_POINTER));
3837
BsonDbPointer dbPointer = reader.readDBPointer();
3938
assertThat(dbPointer.getNamespace(), is("b"));
4039
assertThat(dbPointer.getId(), is(new ObjectId("5209296cd6c4e38cf96fffdc")));
4140
reader.readEndDocument();
41+
reader.close();
4242
}
4343

4444
private BsonBinaryReader createReaderForBytes(final byte[] bytes) {
45-
return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
45+
return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
4646
}
4747
}

bson/src/test/unit/org/bson/BsonBinaryWriterTest.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class BsonBinaryWriterTest {
4040
@Before
4141
public void setup() {
4242
buffer = new BasicOutputBuffer();
43-
writer = new BsonBinaryWriter(new BsonWriterSettings(100), new BsonBinaryWriterSettings(1024), buffer, true);
43+
writer = new BsonBinaryWriter(new BsonWriterSettings(100), new BsonBinaryWriterSettings(1024), buffer);
4444
}
4545

4646
@After
@@ -385,7 +385,7 @@ public void testWriteRead() throws IOException {
385385

386386
ByteBufferBsonInput basicInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
387387

388-
BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer, false);
388+
BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer);
389389

390390
try {
391391
assertEquals(BsonType.DOCUMENT, reader.readBsonType());
@@ -465,9 +465,9 @@ public void testPipe() {
465465
byte[] bytes = buffer.toByteArray();
466466

467467
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
468-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer, false);
468+
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
469469
try {
470-
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
470+
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
471471
try {
472472
newWriter.pipe(reader);
473473
} finally {
@@ -495,8 +495,8 @@ public void testPipeNestedDocument() {
495495
byte[] bytes = buffer.toByteArray();
496496

497497
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
498-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer, true);
499-
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
498+
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
499+
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
500500
reader1.readStartDocument();
501501
reader1.readName();
502502

@@ -507,8 +507,7 @@ public void testPipeNestedDocument() {
507507
assertEquals(2, reader1.readInt32());
508508

509509
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer
510-
.toByteArray()))),
511-
true);
510+
.toByteArray()))));
512511

513512
reader2.readStartDocument(); //checking what writer piped
514513
assertEquals(BsonType.BOOLEAN, reader2.readBsonType());
@@ -526,8 +525,8 @@ public void testPipeDocumentIntoArray() {
526525
byte[] bytes = buffer.toByteArray();
527526

528527
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
529-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer, true);
530-
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
528+
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
529+
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
531530

532531
newWriter.writeStartDocument();
533532
newWriter.writeStartArray("a");
@@ -536,8 +535,7 @@ public void testPipeDocumentIntoArray() {
536535
newWriter.writeEndDocument();
537536

538537
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer
539-
.toByteArray()))),
540-
true);
538+
.toByteArray()))));
541539

542540
//checking what writer piped
543541
reader2.readStartDocument();
@@ -557,17 +555,16 @@ public void testPipeDocumentIntoDocument() {
557555
byte[] bytes = buffer.toByteArray();
558556

559557
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
560-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer, true);
561-
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
558+
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
559+
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
562560

563561
newWriter.writeStartDocument();
564562
newWriter.writeName("doc");
565563
newWriter.pipe(reader1);
566564
newWriter.writeEndDocument();
567565

568566
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer
569-
.toByteArray()))),
570-
true);
567+
.toByteArray()))));
571568

572569
//checking what writer piped
573570
reader2.readStartDocument();
@@ -587,14 +584,13 @@ public void testPipeDocumentIntoTopLevel() {
587584
byte[] bytes = buffer.toByteArray();
588585

589586
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
590-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer, true);
591-
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
587+
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
588+
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
592589

593590
newWriter.pipe(reader1);
594591

595592
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer
596-
.toByteArray()))),
597-
true);
593+
.toByteArray()))));
598594

599595
//checking what writer piped
600596
reader2.readStartDocument();
@@ -611,17 +607,16 @@ public void testPipeDocumentIntoScopeDocument() {
611607
byte[] bytes = buffer.toByteArray();
612608

613609
BasicOutputBuffer newBuffer = new BasicOutputBuffer();
614-
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer, true);
615-
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
610+
BsonBinaryWriter newWriter = new BsonBinaryWriter(newBuffer);
611+
BsonBinaryReader reader1 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
616612

617613
newWriter.writeStartDocument();
618614
newWriter.writeJavaScriptWithScope("js", "i++");
619615
newWriter.pipe(reader1);
620616
newWriter.writeEndDocument();
621617

622618
BsonBinaryReader reader2 = new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(newBuffer
623-
.toByteArray()))),
624-
true);
619+
.toByteArray()))));
625620

626621
//checking what writer piped
627622
reader2.readStartDocument();
@@ -662,7 +657,7 @@ public void testMarkAndReset() throws IOException {
662657

663658
ByteBufferBsonInput basicInputBuffer = new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(baos.toByteArray())));
664659

665-
BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer, true);
660+
BsonBinaryReader reader = new BsonBinaryReader(basicInputBuffer);
666661

667662
try {
668663
reader.readStartDocument();
@@ -689,6 +684,6 @@ public void testMarkAndReset() throws IOException {
689684
// CHECKSTYLE:ON
690685

691686
private BsonBinaryReader createReaderForBytes(final byte[] bytes) {
692-
return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))), true);
687+
return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(ByteBuffer.wrap(bytes))));
693688
}
694689
}

0 commit comments

Comments
 (0)