|
14 | 14 | * Wrapper around ByteBuffer that makes use of sun.misc.Unsafe if available. |
15 | 15 | */ |
16 | 16 | class WrappedBuffer { |
17 | | - private static final Charset charSet = StandardCharsets.ISO_8859_1; |
18 | | - private static final CharsetEncoder enc = charSet.newEncoder(); |
19 | | - |
20 | 17 | private final ByteBuffer buffer; |
21 | 18 | private final long address; |
22 | 19 | private final Unsafe unsafe; |
@@ -72,25 +69,26 @@ void putDouble(final int offset, final double value) { |
72 | 69 | } |
73 | 70 |
|
74 | 71 | String getString(final int offset, final int maxLen) { |
75 | | - buffer.position(offset); |
76 | | - ByteBuffer source = buffer.slice(); |
77 | | - int rem = maxLen - 1; |
78 | | - while (source.get() != 0 && rem > 0) { |
79 | | - rem--; |
80 | | - } |
81 | | - if (rem > 0) { |
82 | | - source.position(source.position() - 1); |
| 72 | + char[] buf = new char[maxLen]; |
| 73 | + long pos = offset + address; |
| 74 | + for (int i = 0; i < maxLen; i++) { |
| 75 | + byte b = unsafe.getByte(pos); |
| 76 | + if (b == 0) break; |
| 77 | + buf[i] = (char) (b & 0xff); |
| 78 | + pos++; |
83 | 79 | } |
84 | | - source.flip(); |
85 | | - return charSet.decode(source).toString(); |
| 80 | + return new String(buf, 0, (int) (pos - offset - address)); |
86 | 81 | } |
87 | 82 |
|
88 | 83 | void putString(final int offset, final int maxLen, final String string) { |
89 | 84 | if (string.length() + 1 >= maxLen) { |
90 | 85 | throw new StringIndexOutOfBoundsException(); |
91 | 86 | } |
92 | | - buffer.position(offset); |
93 | | - enc.encode(CharBuffer.wrap(string), buffer, true); |
94 | | - buffer.put((byte) 0); |
| 87 | + long pos = offset + address; |
| 88 | + for (int i = 0; i < string.length(); i++) { |
| 89 | + unsafe.putByte(pos, (byte) string.charAt(i)); |
| 90 | + pos++; |
| 91 | + } |
| 92 | + unsafe.putByte(pos, (byte) 0); |
95 | 93 | } |
96 | 94 | } |
0 commit comments