Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit cb34eea

Browse files
author
Nat Luengnaruemitchai
committed
JERSEY-2837: For empty stream, it should return -1 when read
1 parent e08a7c8 commit cb34eea

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

core-common/src/main/java/org/glassfish/jersey/internal/util/collection/ByteBufferInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private int tryRead(final byte[] b, final int off, final int len, boolean block)
303303
}
304304
}
305305

306-
return i;
306+
return i == 0 && eof ? -1 : i;
307307
}
308308

309309
private int tryRead(boolean block) throws IOException {

core-common/src/test/java/org/glassfish/jersey/internal/util/collection/ByteBufferInputStreamTest.java

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141

4242
import java.io.IOException;
4343
import java.nio.ByteBuffer;
44+
import java.util.Random;
4445
import java.util.concurrent.ExecutorService;
4546
import java.util.concurrent.Executors;
47+
import java.util.concurrent.Semaphore;
4648
import java.util.concurrent.TimeUnit;
49+
import java.util.concurrent.atomic.AtomicBoolean;
4750

4851
import org.glassfish.jersey.internal.LocalizationMessages;
4952

@@ -52,6 +55,7 @@
5255
import static org.junit.Assert.assertNotEquals;
5356
import static org.junit.Assert.assertNotNull;
5457
import static org.junit.Assert.assertNull;
58+
import static org.junit.Assert.assertTrue;
5559
import static org.junit.Assert.fail;
5660

5761
/**
@@ -61,6 +65,112 @@
6165
*/
6266
public class ByteBufferInputStreamTest {
6367

68+
@Test
69+
public void testBlockingReadAByteEmptyStream() throws Exception {
70+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
71+
bbis.closeQueue();
72+
assertEquals(-1, bbis.read());
73+
}
74+
75+
@Test
76+
public void testNonBlockingReadAByteEmptyStream() throws Exception {
77+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
78+
bbis.closeQueue();
79+
assertEquals(-1, bbis.tryRead());
80+
}
81+
82+
@Test
83+
public void testBlockingReadByteArrayEmptyStream() throws Exception {
84+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
85+
bbis.closeQueue();
86+
byte[] buf = new byte[1024];
87+
assertEquals(-1, bbis.read(buf));
88+
}
89+
90+
@Test
91+
public void testNonBlockingReadByteArrayEmptyStream() throws Exception {
92+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
93+
bbis.closeQueue();
94+
byte[] buf = new byte[1024];
95+
assertEquals(-1, bbis.tryRead(buf));
96+
}
97+
98+
@Test
99+
public void testBlockingReadByteArrayFromFinishedExactLengthStream() throws Exception {
100+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
101+
byte[] sourceData = new byte[1024];
102+
new Random().nextBytes(sourceData);
103+
ByteBuffer byteBuf = ByteBuffer.wrap(sourceData);
104+
bbis.put(byteBuf);
105+
bbis.closeQueue();
106+
byte[] buf = new byte[1024];
107+
assertEquals(1024, bbis.read(buf));
108+
// no more data to read; so it should return -1
109+
assertEquals(-1, bbis.read(buf));
110+
}
111+
112+
@Test
113+
public void testNonBlockingReadByteArrayFromFinishedExactLengthStream() throws Exception {
114+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
115+
byte[] sourceData = new byte[1024];
116+
new Random().nextBytes(sourceData);
117+
ByteBuffer byteBuf = ByteBuffer.wrap(sourceData);
118+
bbis.put(byteBuf);
119+
byte[] buf = new byte[1024];
120+
assertEquals(1024, bbis.tryRead(buf));
121+
// the queue has not been close; so it should return 0
122+
assertEquals(0, bbis.tryRead(buf));
123+
bbis.closeQueue();
124+
}
125+
126+
@Test
127+
public void testBlockingReadByteArrayFromUnfinishedExactLengthStream() throws Exception {
128+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
129+
byte[] sourceData = new byte[1024];
130+
new Random().nextBytes(sourceData);
131+
ByteBuffer byteBuf = ByteBuffer.wrap(sourceData);
132+
bbis.put(byteBuf);
133+
final byte[] buf = new byte[1024];
134+
assertEquals(1024, bbis.read(buf));
135+
final AtomicBoolean closed = new AtomicBoolean(false);
136+
final Semaphore s = new Semaphore(1);
137+
s.acquire();
138+
Thread t = new Thread(new Runnable() {
139+
@Override
140+
public void run() {
141+
try {
142+
// it should return -1 since there is no more data
143+
assertEquals(-1, bbis.read(buf));
144+
// it should only reach here if the stream has been closed
145+
assertTrue(closed.get());
146+
} catch (IOException e) {
147+
e.printStackTrace();
148+
} finally {
149+
s.release();
150+
}
151+
}
152+
});
153+
t.start();
154+
Thread.sleep(500);
155+
closed.set(true);
156+
bbis.closeQueue();
157+
// wait until the job is done
158+
s.acquire();
159+
}
160+
161+
@Test
162+
public void testNonBlockingReadByteArrayFromUnfinishedExactLengthStream() throws Exception {
163+
final ByteBufferInputStream bbis = new ByteBufferInputStream();
164+
byte[] sourceData = new byte[1024];
165+
new Random().nextBytes(sourceData);
166+
ByteBuffer byteBuf = ByteBuffer.wrap(sourceData);
167+
bbis.put(byteBuf);
168+
bbis.closeQueue();
169+
byte[] buf = new byte[1024];
170+
assertEquals(1024, bbis.tryRead(buf));
171+
assertEquals(-1, bbis.tryRead(buf));
172+
}
173+
64174
/**
65175
* Test for non blocking single-byte read of the stream.
66176
*

0 commit comments

Comments
 (0)