|
9 | 9 | import java.nio.charset.Charset; |
10 | 10 | import java.nio.charset.CharsetEncoder; |
11 | 11 | import java.nio.charset.StandardCharsets; |
12 | | -import java.util.Optional; |
13 | 12 |
|
14 | 13 | /** |
15 | 14 | * Wrapper around ByteBuffer that makes use of sun.misc.Unsafe if available. |
16 | 15 | * If not available it will fall back on using the ByteBuffer itself. |
17 | 16 | */ |
18 | 17 | class WrappedBuffer { |
19 | | - private static final CharsetEncoder enc = Charset.forName("ISO-8859-1").newEncoder(); |
| 18 | + private static final Charset charSet = StandardCharsets.ISO_8859_1; |
| 19 | + private static final CharsetEncoder enc = charSet.newEncoder(); |
20 | 20 |
|
21 | 21 | private final ByteBuffer buffer; |
22 | 22 | private final long address; |
23 | 23 | private final Unsafe unsafe; |
24 | | - private final boolean useUnsafe; |
25 | 24 |
|
26 | 25 | WrappedBuffer(final ByteBuffer byteBuffer) { |
27 | | - final Optional<Unsafe> optionalUnsafe = getTheUnsafe(); |
28 | | - |
| 26 | + unsafe = getTheUnsafe(); |
29 | 27 | buffer = byteBuffer; |
30 | | - |
31 | | - useUnsafe = optionalUnsafe.isPresent(); |
32 | | - address = useUnsafe ? ((DirectBuffer) buffer).address() : 0; |
33 | | - unsafe = useUnsafe ? optionalUnsafe.get() : null; |
| 28 | + address = ((DirectBuffer) buffer).address(); |
34 | 29 | } |
35 | 30 |
|
36 | | - private static Optional<Unsafe> getTheUnsafe() { |
| 31 | + private static Unsafe getTheUnsafe() { |
37 | 32 | try { |
38 | 33 | final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe"); |
39 | 34 | theUnsafe.setAccessible(true); |
40 | | - return Optional.of((Unsafe) theUnsafe.get(null)); |
| 35 | + return (Unsafe) theUnsafe.get(null); |
41 | 36 | } |
42 | 37 | catch (final Exception e) { |
43 | 38 | e.printStackTrace(); |
44 | | - return Optional.empty(); |
| 39 | + return null; |
45 | 40 | } |
46 | 41 | } |
47 | 42 |
|
48 | 43 | public byte getByte(final int offset) { |
49 | | - if (useUnsafe) { |
50 | | - return unsafe.getByte(address + offset); |
51 | | - } |
52 | | - else { |
53 | | - return buffer.get(offset); |
54 | | - } |
| 44 | + return unsafe.getByte(address + offset); |
55 | 45 | } |
56 | 46 |
|
57 | 47 | public void putByte(final int offset, final byte value) { |
58 | | - if (useUnsafe) { |
59 | | - unsafe.putByte(address + offset, value); |
60 | | - } |
61 | | - else { |
62 | | - buffer.put(offset, value); |
63 | | - } |
| 48 | + unsafe.putByte(address + offset, value); |
64 | 49 | } |
65 | 50 |
|
66 | 51 | public short getShort(final int offset) { |
67 | | - if (useUnsafe) { |
68 | | - return unsafe.getShort(address + offset); |
69 | | - } |
70 | | - else { |
71 | | - return buffer.getShort(offset); |
72 | | - } |
| 52 | + return unsafe.getShort(address + offset); |
73 | 53 | } |
74 | 54 |
|
75 | 55 | public void putShort(final int offset, final short value) { |
76 | | - if (useUnsafe) { |
77 | | - unsafe.putShort(address + offset, value); |
78 | | - } |
79 | | - else { |
80 | | - buffer.putShort(offset, value); |
81 | | - } |
| 56 | + unsafe.putShort(address + offset, value); |
82 | 57 | } |
83 | 58 |
|
84 | 59 | public int getInt(final int offset) { |
85 | | - if (useUnsafe) { |
86 | | - return unsafe.getInt(address + offset); |
87 | | - } |
88 | | - else { |
89 | | - return buffer.getInt(offset); |
90 | | - } |
| 60 | + return unsafe.getInt(address + offset); |
91 | 61 | } |
92 | 62 |
|
93 | 63 | public void putInt(final int offset, final int value) { |
94 | | - if (useUnsafe) { |
95 | | - unsafe.putInt(address + offset, value); |
96 | | - } |
97 | | - else { |
98 | | - buffer.putInt(offset, value); |
99 | | - } |
| 64 | + unsafe.putInt(address + offset, value); |
100 | 65 | } |
101 | 66 |
|
102 | 67 | public double getDouble(final int offset) { |
103 | | - if (useUnsafe) { |
104 | | - return unsafe.getDouble(address + offset); |
105 | | - } |
106 | | - else { |
107 | | - return buffer.getDouble(offset); |
108 | | - } |
| 68 | + return unsafe.getDouble(address + offset); |
109 | 69 | } |
110 | 70 |
|
111 | 71 | public void putDouble(final int offset, final double value) { |
112 | | - if (useUnsafe) { |
113 | | - unsafe.putDouble(address + offset, value); |
114 | | - } |
115 | | - else { |
116 | | - buffer.putDouble(offset, value); |
117 | | - } |
| 72 | + unsafe.putDouble(address + offset, value); |
118 | 73 | } |
119 | 74 |
|
120 | 75 | public String getString(final int offset, final int maxLen) { |
121 | 76 | final byte[] buf = new byte[maxLen]; |
122 | 77 |
|
123 | | - if (useUnsafe) { |
124 | | - unsafe.copyMemory(null, address + offset, buf, Unsafe.ARRAY_BYTE_BASE_OFFSET, maxLen); |
125 | | - } |
126 | | - else { |
127 | | - buffer.position(offset); |
128 | | - buffer.get(buf, 0, maxLen); |
129 | | - } |
| 78 | + unsafe.copyMemory(null, address + offset, buf, Unsafe.ARRAY_BYTE_BASE_OFFSET, maxLen); |
130 | 79 |
|
131 | 80 | int len = 0; |
132 | 81 | while (len < maxLen && buf[len] != 0) { |
133 | 82 | ++len; |
134 | 83 | } |
135 | | - return new String(buf, 0, len, StandardCharsets.ISO_8859_1); |
| 84 | + return new String(buf, 0, len, charSet); |
136 | 85 | } |
137 | 86 |
|
138 | 87 | public void putString(final int offset, final int maxLen, final String string) { |
|
0 commit comments