Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 442e48a

Browse files
committed
Fixed bug with incorrectly computed free buffer capacity.
1 parent 471ea9b commit 442e48a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

‎src/main/java/com/jsoniter/IterImplForStreaming.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ private static boolean keepSkippedBytesThenRead(JsonIterator iter) throws IOExce
277277
int offset = iter.tail - iter.skipStartedAt;
278278
byte[] srcBuffer = iter.buf;
279279
// Check there is no unused buffer capacity
280-
if (iter.buf.length - iter.tail == 0) {
280+
if ((getUnusedBufferByteCount(iter)) == 0) {
281281
// If auto expand buffer enabled, then create larger buffer
282282
if (iter.autoExpandBufferStep > 0) {
283283
iter.buf = new byte[iter.buf.length + iter.autoExpandBufferStep];
@@ -301,6 +301,11 @@ private static boolean keepSkippedBytesThenRead(JsonIterator iter) throws IOExce
301301
return true;
302302
}
303303

304+
private static int getUnusedBufferByteCount(JsonIterator iter) {
305+
// Get bytes from 0 to skipStart + from tail till end
306+
return iter.buf.length - iter.tail + iter.skipStartedAt;
307+
}
308+
304309
final static byte readByte(JsonIterator iter) throws IOException {
305310
if (iter.head == iter.tail) {
306311
if (!loadMore(iter)) {

‎src/test/java/com/jsoniter/IterImplForStreamingTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jsoniter.any.Any;
44
import com.jsoniter.spi.JsonException;
5+
import java.io.ByteArrayInputStream;
56
import java.io.IOException;
67
import java.io.InputStream;
78
import junit.framework.TestCase;
@@ -35,7 +36,7 @@ public void testLoadMore() throws IOException {
3536
// Check buffer was not expanded
3637
assertEquals(initialBufferSize, jsonIterator.buf.length);
3738

38-
// Case #2: Data does fit into initial buffer, autoresizing off
39+
// Case #2: Data does not fit into initial buffer, autoresizing off
3940
initialBufferSize = originalContent.length() / 2;
4041
jsonIterator = JsonIterator.parse(getSluggishInputStream(src), initialBufferSize, 0);
4142
jsonIterator.readObject();
@@ -59,6 +60,15 @@ public void testLoadMore() throws IOException {
5960
assertEquals(originalContent, parsedString.toString());
6061
// Check buffer was expanded exactly once
6162
assertEquals(initialBufferSize + autoExpandBufferStep, jsonIterator.buf.length);
63+
64+
// Case #4: Data does not fit (but largest string does) into initial buffer, autoresizing on
65+
initialBufferSize = originalContent.length() + 2;
66+
jsonIterator = JsonIterator.parse(new ByteArrayInputStream(src), initialBufferSize, 0);
67+
jsonIterator.readObject();
68+
parsedString = jsonIterator.readAny();
69+
assertEquals(originalContent, parsedString.toString());
70+
// Check buffer was expanded exactly once
71+
assertEquals(initialBufferSize, jsonIterator.buf.length);
6272
}
6373

6474
private static InputStream getSluggishInputStream(final byte[] src) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /