Skip to content

Commit cc85e50

Browse files
committed
jnh-connector: Fixed issue where FirstByteCachingStream failed to include the buffered byte in returned data length when reading with array
1 parent 237a78e commit cc85e50

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

connectors/jnh-connector/src/main/java/org/glassfish/jersey/jnh/connector/JavaNetHttpConnector.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ public int read(byte[] b, int off, int len) throws IOException {
377377
if (zero != -1) {
378378
b[off] = (byte) (zero & 0xFF);
379379
r = inner.read(b, off + 1, len - 1);
380+
r = (r == -1) ? 1 : r + 1;
380381
} else {
381382
r = inner.read(b, off, len);
382383
}

connectors/jnh-connector/src/test/java/org/glassfish/jersey/jnh/connector/FirstByteCachingStreamTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ void testOneByte() throws Exception {
5353
Assertions.assertEquals(0, testIs.available());
5454
}
5555

56+
@Test
57+
void testOneByteInArray() throws Exception {
58+
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new byte[]{'A'});
59+
InputStream testIs = createFirstByteCachingStream(byteArrayInputStream);
60+
Assertions.assertEquals(1, testIs.available());
61+
62+
byte[] bytes = new byte[1];
63+
int l = testIs.read(bytes);
64+
Assertions.assertEquals(1, l);
65+
Assertions.assertEquals('A', bytes[0]);
66+
Assertions.assertEquals(0, testIs.available());
67+
}
68+
5669
@Test
5770
void testTwoBytes() throws Exception {
5871
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(new byte[]{'A', 'B'});
@@ -73,7 +86,8 @@ void testTwoBytesReadAtOnce() throws Exception {
7386
Assertions.assertEquals(2, testIs.available());
7487

7588
byte[] bytes = new byte[2];
76-
testIs.read(bytes);
89+
int l = testIs.read(bytes);
90+
Assertions.assertEquals(2, l);
7791
Assertions.assertEquals('A', bytes[0]);
7892
Assertions.assertEquals('B', bytes[1]);
7993
Assertions.assertEquals(0, testIs.available());

0 commit comments

Comments
 (0)