|
3 | 3 | import java.io.IOException; |
4 | 4 | import java.math.BigInteger; |
5 | 5 | import java.nio.ByteBuffer; |
| 6 | +import java.nio.charset.CharacterCodingException; |
6 | 7 | import java.nio.charset.Charset; |
| 8 | +import java.nio.charset.CharsetDecoder; |
7 | 9 |
|
8 | 10 | import com.fasterxml.jackson.databind.JsonNode; |
9 | 11 | import com.fasterxml.jackson.databind.ObjectMapper; |
|
15 | 17 | * This class CANNOT be shared between threads |
16 | 18 | */ |
17 | 19 | final class Decoder { |
| 20 | + private static final Charset UTF_8 = Charset.forName("UTF-8"); |
| 21 | + |
18 | 22 | // XXX - This is only for unit testings. We should possibly make a |
19 | 23 | // constructor to set this |
20 | 24 | boolean POINTER_TEST_HACK = false; |
21 | 25 | private final long pointerBase; |
22 | 26 |
|
23 | 27 | private final ObjectMapper objectMapper; |
24 | 28 |
|
| 29 | + private final CharsetDecoder utfDecoder = UTF_8.newDecoder(); |
| 30 | + |
25 | 31 | private final ByteBuffer buffer; |
26 | 32 |
|
27 | 33 | static enum Type { |
@@ -182,10 +188,12 @@ private Result decodePointer(int ctrlByte, int offset) { |
182 | 188 | return new Result(new LongNode(pointer), offset + pointerSize); |
183 | 189 | } |
184 | 190 |
|
185 | | - private String decodeString(int size) { |
186 | | - ByteBuffer buffer = this.buffer.slice(); |
187 | | - buffer.limit(size); |
188 | | - return Charset.forName("UTF-8").decode(buffer).toString(); |
| 191 | + private String decodeString(int size) throws CharacterCodingException { |
| 192 | + int oldLimit = buffer.limit(); |
| 193 | + buffer.limit(buffer.position() + size); |
| 194 | + String s = utfDecoder.decode(buffer).toString(); |
| 195 | + buffer.limit(oldLimit); |
| 196 | + return s; |
189 | 197 | } |
190 | 198 |
|
191 | 199 | private IntNode decodeUint16(int size) { |
|
0 commit comments