|
25 | 25 | import java.io.ByteArrayInputStream;
|
26 | 26 | import java.io.IOException;
|
27 | 27 | 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; |
28 | 32 | import java.nio.charset.StandardCharsets;
|
29 | 33 | import java.util.Map;
|
30 | 34 | import java.util.concurrent.ThreadLocalRandom;
|
@@ -80,6 +84,15 @@ JsonParser create(JsonFactory factory, Supplier<byte[]> bytesSupplier) throws IO
|
80 | 84 | StandardCharsets.UTF_8));
|
81 | 85 | }
|
82 | 86 | },
|
| 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 | + }, |
83 | 96 | ;
|
84 | 97 |
|
85 | 98 | abstract JsonParser create(JsonFactory factory, Supplier<byte[]> jsonSupplier) throws IOException;
|
@@ -154,6 +167,68 @@ public static final class SimpleClass {
|
154 | 167 | public String stringThree;
|
155 | 168 | }
|
156 | 169 |
|
| 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 | + |
157 | 232 | public static void main(String[] _args) throws Exception {
|
158 | 233 | new Runner(new OptionsBuilder()
|
159 | 234 | .include(JsonArbitraryFieldNameBenchmark.class.getName())
|
|
0 commit comments