Skip to content

Commit 4e23051

Browse files
committed
fix(driver-compat) make Bits.readFully support offset for buffer array
1 parent 6f3eeae commit 4e23051

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

driver-compat/src/main/org/bson/io/Bits.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.bson.io;
1818

19-
import java.io.ByteArrayOutputStream;
2019
import java.io.EOFException;
2120
import java.io.IOException;
2221
import java.io.InputStream;
@@ -35,7 +34,7 @@ public static void readFully(final InputStream in, final byte[] buffer, final in
3534

3635
public static void readFully(final InputStream in, final byte[] buffer, final int offset, final int length)
3736
throws IOException {
38-
if (buffer.length < length) {
37+
if (buffer.length < length + offset) {
3938
throw new IllegalArgumentException("Buffer is too small");
4039
}
4140

@@ -108,14 +107,4 @@ public static long readLong(final byte[] buffer, final int offset) {
108107
x |= (0xFFL & buffer[offset + 7]) << 56;
109108
return x;
110109
}
111-
112-
private static class BufferExposingByteArrayOutputStream extends ByteArrayOutputStream {
113-
BufferExposingByteArrayOutputStream(final int size) {
114-
super(size);
115-
}
116-
117-
byte[] getInternalBytes() {
118-
return buf;
119-
}
120-
}
121110
}

driver-compat/src/test/unit/org/bson/io/BitsTest.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,40 @@ public class BitsTest {
3636
};
3737

3838
@Test
39-
public void testReadFully() throws IOException {
39+
public void testReadFullyWithBufferLargerThanExpected() throws IOException {
4040
final byte[] buffer = new byte[8192];
4141
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, BYTES.length);
4242
assertArrayEquals(BYTES, Arrays.copyOfRange(buffer, 0, BYTES.length));
4343
}
4444

45+
@Test
46+
public void testReadFullyWithOffset() throws IOException {
47+
final int offset = 10;
48+
final byte[] buffer = new byte[8192];
49+
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, offset, BYTES.length);
50+
assertArrayEquals(BYTES, Arrays.copyOfRange(buffer, offset, BYTES.length + offset));
51+
}
52+
53+
@Test
54+
public void testReadFullyWithBufferEqualsToExpected() throws IOException {
55+
final int offset = 10;
56+
final byte[] buffer = new byte[offset + BYTES.length];
57+
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, offset, BYTES.length);
58+
assertArrayEquals(BYTES, Arrays.copyOfRange(buffer, offset, BYTES.length + offset));
59+
}
4560

46-
@Test (expected = IllegalArgumentException.class)
61+
@Test(expected = IllegalArgumentException.class)
4762
public void testReadFullyUsingNotEnoughBigBuffer() throws IOException {
4863
Bits.readFully(new ByteArrayInputStream(BYTES), new byte[2], BYTES.length);
4964
}
5065

66+
@Test(expected = IllegalArgumentException.class)
67+
public void testReadFullyUsingNotEnoughBigBufferWithOffset() throws IOException {
68+
final int offset = 10;
69+
final byte[] buffer = new byte[BYTES.length];
70+
Bits.readFully(new ByteArrayInputStream(BYTES), buffer, offset, BYTES.length);
71+
}
72+
5173
@Test
5274
public void testReadInt() {
5375
assertEquals(41, Bits.readInt(BYTES));
@@ -68,7 +90,6 @@ public void testReadIntInBigEndianNotation() {
6890
assertEquals(-12, Bits.readIntBE(new byte[]{-1, -1, -1, -12}, 0));
6991
}
7092

71-
7293
@Test
7394
public void testReadLong() {
7495
assertEquals(Long.MAX_VALUE, Bits.readLong(BYTES, 24));
@@ -79,5 +100,4 @@ public void testReadLongWithNotEnoughData() {
79100
Bits.readLong(Arrays.copyOfRange(BYTES, 24, 30), 0);
80101
}
81102

82-
83103
}

0 commit comments

Comments
 (0)