Skip to content

Commit 85b2923

Browse files
committed
optimize
1 parent 061574d commit 85b2923

5 files changed

Lines changed: 43 additions & 8 deletions

File tree

src/main/java/com/jsoniter/IterImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,8 @@ public static Any readAny(JsonIterator iter) throws IOException {
216216
throw iter.reportError("IterImplSkip", "do not know how to skip: " + c);
217217
}
218218
}
219+
220+
public static void skipFixedBytes(JsonIterator iter, int n) throws IOException {
221+
iter.head += n;
222+
}
219223
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,15 @@ private static byte[] copySkippedBytes(JsonIterator iter) {
366366
System.arraycopy(iter.buf, start, bytes, 0, bytes.length);
367367
return bytes;
368368
}
369+
370+
public static void skipFixedBytes(JsonIterator iter, int n) throws IOException {
371+
iter.head += n;
372+
if (iter.head >= iter.tail) {
373+
int more = iter.head - iter.tail;
374+
if (!loadMore(iter)) {
375+
throw iter.reportError("skipFixedBytes", "unexpected end");
376+
}
377+
iter.head += more;
378+
}
379+
}
369380
}

src/main/java/com/jsoniter/JsonIterator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ public final boolean readBoolean() throws IOException {
152152
byte c = IterImpl.nextToken(this);
153153
switch (c) {
154154
case 't':
155-
IterImpl.skipUntilBreak(this);
155+
IterImpl.skipFixedBytes(this, 3); // true
156156
return true;
157157
case 'f':
158-
IterImpl.skipUntilBreak(this);
158+
IterImpl.skipFixedBytes(this, 4); // false
159159
return false;
160160
default:
161161
throw reportError("readBoolean", "expect t or f, found: " + c);

src/main/java/com/jsoniter/output/JsonStream.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ public final void write(int b1, int b2, int b3) throws IOException {
5959
}
6060

6161
public final void write(byte b[], int off, int len) throws IOException {
62-
if (len >= buf.length) {
62+
if (len >= buf.length - count) {
63+
if (len >= buf.length) {
6364
/* If the request length exceeds the size of the output buffer,
6465
flush the output buffer and then write the data directly.
6566
In this way buffered streams will cascade harmlessly. */
66-
flushBuffer();
67-
out.write(b, off, len);
68-
return;
69-
}
70-
if (len > buf.length - count) {
67+
flushBuffer();
68+
out.write(b, off, len);
69+
return;
70+
}
7171
flushBuffer();
7272
}
7373
System.arraycopy(b, off, buf, count, len);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.jsoniter;
2+
3+
import junit.framework.TestCase;
4+
import org.junit.experimental.categories.Category;
5+
6+
import java.io.ByteArrayInputStream;
7+
import java.io.IOException;
8+
9+
public class TestBoolean extends TestCase {
10+
@Category(AllTests.StreamingCategory.class)
11+
public void test() throws IOException {
12+
JsonIterator iter = JsonIterator.parse(new ByteArrayInputStream("[true,false,true]".getBytes()), 4);
13+
iter.readArray();
14+
assertTrue(iter.readBoolean());
15+
iter.readArray();
16+
assertFalse(iter.readBoolean());
17+
iter.readArray();
18+
assertTrue(iter.readBoolean());
19+
}
20+
}

0 commit comments

Comments
 (0)