Skip to content

Commit b071a6b

Browse files
committed
Add CharBufferReader benchmark
1 parent 4d37c54 commit b071a6b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

src/main/java/com/fasterxml/jackson/perf/json/JsonArbitraryFieldNameBenchmark.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
import java.io.ByteArrayInputStream;
2626
import java.io.IOException;
2727
import java.io.InputStreamReader;
28+
import java.io.Reader;
29+
import java.nio.ByteBuffer;
30+
import java.nio.CharBuffer;
31+
import java.nio.charset.Charset;
2832
import java.nio.charset.StandardCharsets;
2933
import java.util.Map;
3034
import java.util.concurrent.ThreadLocalRandom;
@@ -80,6 +84,15 @@ JsonParser create(JsonFactory factory, Supplier<byte[]> bytesSupplier) throws IO
8084
StandardCharsets.UTF_8));
8185
}
8286
},
87+
CHAR_READER() {
88+
@Override
89+
JsonParser create(JsonFactory factory, Supplier<byte[]> jsonSupplier) throws IOException {
90+
ByteBuffer byteBuffer = ByteBuffer.wrap(jsonSupplier.get());
91+
CharBuffer charBuffer = Charset.forName("UTF-8").decode(byteBuffer);
92+
CharBufferReader charBufferReader = new CharBufferReader(charBuffer);
93+
return factory.createParser(charBufferReader);
94+
}
95+
},
8396
;
8497

8598
abstract JsonParser create(JsonFactory factory, Supplier<byte[]> jsonSupplier) throws IOException;
@@ -154,6 +167,68 @@ public static final class SimpleClass {
154167
public String stringThree;
155168
}
156169

170+
public static class CharBufferReader extends Reader {
171+
private final CharBuffer charBuffer;
172+
173+
public CharBufferReader(CharBuffer buffer) {
174+
this.charBuffer = buffer.duplicate();
175+
}
176+
177+
@Override
178+
public int read(char[] chars, int off, int len) {
179+
int remaining = this.charBuffer.remaining();
180+
if (remaining <= 0) {
181+
return -1;
182+
}
183+
int length = Math.min(len, remaining);
184+
this.charBuffer.get(chars, off, length);
185+
return length;
186+
}
187+
188+
@Override
189+
public int read() {
190+
if (this.charBuffer.hasRemaining()) {
191+
return this.charBuffer.get();
192+
}
193+
return -1;
194+
}
195+
196+
@Override
197+
public long skip(long n) {
198+
if (n < 0L) {
199+
throw new IllegalArgumentException("number of characters to skip cannot be negative");
200+
}
201+
int skipped = Math.min((int) n, this.charBuffer.remaining());
202+
this.charBuffer.position(this.charBuffer.position() + skipped);
203+
return skipped;
204+
}
205+
206+
@Override
207+
public boolean ready() {
208+
return true;
209+
}
210+
211+
@Override
212+
public boolean markSupported() {
213+
return true;
214+
}
215+
216+
@Override
217+
public void mark(int readAheadLimit) {
218+
this.charBuffer.mark();
219+
}
220+
221+
@Override
222+
public void reset() {
223+
this.charBuffer.reset();
224+
}
225+
226+
@Override
227+
public void close() {
228+
this.charBuffer.position(this.charBuffer.limit());
229+
}
230+
}
231+
157232
public static void main(String[] _args) throws Exception {
158233
new Runner(new OptionsBuilder()
159234
.include(JsonArbitraryFieldNameBenchmark.class.getName())

0 commit comments

Comments
 (0)