Skip to content

Commit 515124d

Browse files
committed
improved TestLinkedBufferInput
1 parent a39ad12 commit 515124d

3 files changed

Lines changed: 133 additions & 70 deletions

File tree

src/main/java/org/msgpack/io/LinkedBufferInput.java

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,23 @@ public LinkedBufferInput(int bufferSize) {
4141
}
4242

4343
public int read(byte[] b, int off, int len) throws EOFException {
44+
if(link.isEmpty()) {
45+
return 0;
46+
}
4447
int olen = len;
4548
while(true) {
4649
ByteBuffer bb = link.peekFirst();
47-
if(bb == null) {
48-
break;
49-
}
5050
if(len < bb.remaining()) {
5151
bb.get(b, off, len);
52-
if(bb.remaining() == 0) {
53-
link.removeFirst();
54-
}
5552
return olen;
5653
}
5754
int rem = bb.remaining();
5855
bb.get(b, off, rem);
5956
len -= rem;
6057
off += rem;
61-
link.removeFirst();
58+
if(!removeFirstLink(bb)) {
59+
break;
60+
}
6261
}
6362
return olen - len;
6463
}
@@ -70,45 +69,49 @@ public byte readByte() throws EOFException {
7069
}
7170
byte result = bb.get();
7271
if(bb.remaining() == 0) {
73-
if(link.size() == 1 && writable >= 0) {
74-
bb.position(0);
75-
bb.limit(0);
76-
writable = bb.capacity();
77-
} else {
78-
link.removeFirst();
79-
}
72+
removeFirstLink(bb);
8073
}
8174
return result;
8275
}
8376

8477
public void advance() {
78+
if(link.isEmpty()) {
79+
return;
80+
}
8581
int len = nextAdvance;
8682
ByteBuffer bb;
8783
while(true) {
8884
bb = link.peekFirst();
89-
if(bb == null) {
90-
nextAdvance = 0;
91-
return;
92-
}
93-
if(len <= bb.remaining()) {
85+
if(len < bb.remaining()) {
9486
bb.position(bb.position()+len);
95-
if(bb.remaining() == 0) {
96-
link.removeFirst();
97-
}
9887
break;
9988
}
10089
len -= bb.remaining();
101-
link.removeFirst();
102-
}
103-
if(link.isEmpty() && writable >= 0) {
104-
bb.position(0);
105-
bb.limit(0);
106-
link.addLast(bb);
107-
writable = bb.capacity();
90+
if(!removeFirstLink(bb)) {
91+
break;
92+
}
10893
}
10994
nextAdvance = 0;
11095
}
11196

97+
private boolean removeFirstLink(ByteBuffer first) {
98+
if(link.size() == 1) {
99+
if(writable >= 0) {
100+
first.position(0);
101+
first.limit(0);
102+
writable = first.capacity();
103+
return false;
104+
} else {
105+
link.removeFirst();
106+
return false;
107+
}
108+
} else {
109+
link.removeFirst();
110+
writable = -1;
111+
return true;
112+
}
113+
}
114+
112115
private void requireMore(int n) throws EOFException {
113116
int off = 0;
114117
for(ByteBuffer bb : link) {
@@ -286,6 +289,7 @@ public void clear() {
286289
writable = bb.capacity();
287290
} else {
288291
link.clear();
292+
writable = -1;
289293
}
290294
}
291295
}

src/main/java/org/msgpack/io/StreamInput.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ public void advance() {
6161
filled = 0;
6262
}
6363

64-
private void require(int n) throws IOException {
65-
if(filled < n) {
66-
filled += in.read(castBuffer, filled, filled - n);
67-
if(filled < n) {
64+
private void require(int len) throws IOException {
65+
while(filled < len) {
66+
int n = in.read(castBuffer, filled, filled - len);
67+
if(n < 0) {
6868
throw new EOFException();
6969
}
70+
filled += n;
7071
}
7172
}
7273

src/test/java/org/msgpack/io/TestLinkedBufferInput.java

Lines changed: 95 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,7 @@ public void testReadByte() throws IOException {
3737
assertEquals(b1.readByte(), b2.readByte());
3838
}
3939

40-
try {
41-
b2.readByte();
42-
assertTrue(false);
43-
} catch(EOFException eof) {
44-
assertTrue(true);
45-
}
40+
assertEndOfBuffer(b2);
4641
}
4742

4843
@Test
@@ -81,12 +76,7 @@ public void testFeedByteArrayCopy() throws IOException {
8176
assertEquals(b1.readByte(), b2.readByte());
8277
}
8378

84-
try {
85-
b2.readByte();
86-
assertTrue(false);
87-
} catch(EOFException eof) {
88-
assertTrue(true);
89-
}
79+
assertEndOfBuffer(b2);
9080
}
9181

9282
@Test
@@ -125,12 +115,7 @@ public void testFeedByteArrayNoCopy() throws IOException {
125115
assertEquals(b1.readByte(), b2.readByte());
126116
}
127117

128-
try {
129-
b2.readByte();
130-
assertTrue(false);
131-
} catch(EOFException eof) {
132-
assertTrue(true);
133-
}
118+
assertEndOfBuffer(b2);
134119
}
135120

136121
@Test
@@ -180,7 +165,7 @@ public void testFeedByteArrayCopyNoCopy() throws IOException {
180165
try {
181166
b2.readByte();
182167
assertTrue(false);
183-
} catch(EOFException eof) {
168+
} catch(EndOfBufferException eof) {
184169
assertTrue(true);
185170
}
186171
}
@@ -238,25 +223,74 @@ public void testGetPrimitivesChunks() throws IOException {
238223
LinkedBufferInput b = new LinkedBufferInput(1024);
239224

240225
for(int i=0; i < 2; i++) {
241-
for(int j=0; j < src.length; j++) {
242-
b.feed(src, j, 1, true);
243-
}
226+
int p = 0;
244227

228+
b.feed(src, p++, 1, true);
245229
assertEquals((byte)2, b.getByte());
246230
assertEquals((byte)2, b.getByte());
247231
b.advance();
232+
233+
for(int j=0; j < 2; j++) {
234+
try {
235+
b.getShort();
236+
assertTrue(false);
237+
} catch(EndOfBufferException eof) {
238+
assertTrue(true);
239+
}
240+
b.feed(src, p++, 1, true);
241+
}
248242
assertEquals((short)2, b.getShort());
249243
assertEquals((short)2, b.getShort());
250244
b.advance();
245+
246+
for(int j=0; j < 4; j++) {
247+
try {
248+
b.getInt();
249+
assertTrue(false);
250+
} catch(EndOfBufferException eof) {
251+
assertTrue(true);
252+
}
253+
b.feed(src, p++, 1, true);
254+
}
251255
assertEquals(2, b.getInt());
252256
assertEquals(2, b.getInt());
253257
b.advance();
258+
259+
for(int j=0; j < 8; j++) {
260+
try {
261+
b.getLong();
262+
assertTrue(false);
263+
} catch(EndOfBufferException eof) {
264+
assertTrue(true);
265+
}
266+
b.feed(src, p++, 1, true);
267+
}
254268
assertEquals(2L, b.getLong());
255269
assertEquals(2L, b.getLong());
256270
b.advance();
271+
272+
for(int j=0; j < 4; j++) {
273+
try {
274+
b.getFloat();
275+
assertTrue(false);
276+
} catch(EndOfBufferException eof) {
277+
assertTrue(true);
278+
}
279+
b.feed(src, p++, 1, true);
280+
}
257281
assertEquals(1.1f, b.getFloat());
258282
assertEquals(1.1f, b.getFloat());
259283
b.advance();
284+
285+
for(int j=0; j < 8; j++) {
286+
try {
287+
b.getDouble();
288+
assertTrue(false);
289+
} catch(EndOfBufferException eof) {
290+
assertTrue(true);
291+
}
292+
b.feed(src, p++, 1, true);
293+
}
260294
assertEquals(1.1, b.getDouble());
261295
assertEquals(1.1, b.getDouble());
262296
b.advance();
@@ -300,12 +334,7 @@ public void testFeedByteBufferCopy() throws IOException {
300334
assertEquals(b1.readByte(), b2.readByte());
301335
}
302336

303-
try {
304-
b2.readByte();
305-
assertTrue(false);
306-
} catch(EOFException eof) {
307-
assertTrue(true);
308-
}
337+
assertEndOfBuffer(b2);
309338
}
310339

311340
@Test
@@ -344,12 +373,7 @@ public void testFeedByteBufferNoCopy() throws IOException {
344373
assertEquals(b1.readByte(), b2.readByte());
345374
}
346375

347-
try {
348-
b2.readByte();
349-
assertTrue(false);
350-
} catch(EOFException eof) {
351-
assertTrue(true);
352-
}
376+
assertEndOfBuffer(b2);
353377
}
354378

355379
@Test
@@ -396,13 +420,47 @@ public void testFeedByteBufferCopyNoCopy() throws IOException {
396420
assertEquals(b1.readByte(), b2.readByte());
397421
}
398422

423+
assertEndOfBuffer(b2);
424+
}
425+
426+
@Test
427+
public void testClear() throws IOException {
428+
byte[] src = new byte[8];
429+
430+
LinkedBufferInput b = new LinkedBufferInput(11);
431+
432+
for(int i=0; i < 2; i++) {
433+
try {
434+
b.readByte();
435+
assertTrue(false);
436+
} catch(EndOfBufferException eof) {
437+
assertTrue(true);
438+
}
439+
440+
b.feed(src);
441+
442+
b.clear();
443+
444+
assertEndOfBuffer(b);
445+
446+
b.feed(src);
447+
448+
for(int j=0; j < src.length; j++) {
449+
b.readByte();
450+
}
451+
452+
b.clear();
453+
}
454+
}
455+
456+
@Test
457+
private void assertEndOfBuffer(LinkedBufferInput b) throws IOException {
399458
try {
400-
b2.readByte();
459+
b.readByte();
401460
assertTrue(false);
402-
} catch(EOFException eof) {
461+
} catch(EndOfBufferException eof) {
403462
assertTrue(true);
404463
}
405464
}
406-
407465
}
408466

0 commit comments

Comments
 (0)