Skip to content

Commit 85800e0

Browse files
committed
no fallback needed as even java 9 to 11 still have Unsafe
1 parent 47e780d commit 85800e0

File tree

1 file changed

+17
-68
lines changed

1 file changed

+17
-68
lines changed

src/main/java/bwapi/WrappedBuffer.java

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,130 +9,79 @@
99
import java.nio.charset.Charset;
1010
import java.nio.charset.CharsetEncoder;
1111
import java.nio.charset.StandardCharsets;
12-
import java.util.Optional;
1312

1413
/**
1514
* Wrapper around ByteBuffer that makes use of sun.misc.Unsafe if available.
1615
* If not available it will fall back on using the ByteBuffer itself.
1716
*/
1817
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();
2020

2121
private final ByteBuffer buffer;
2222
private final long address;
2323
private final Unsafe unsafe;
24-
private final boolean useUnsafe;
2524

2625
WrappedBuffer(final ByteBuffer byteBuffer) {
27-
final Optional<Unsafe> optionalUnsafe = getTheUnsafe();
28-
26+
unsafe = getTheUnsafe();
2927
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();
3429
}
3530

36-
private static Optional<Unsafe> getTheUnsafe() {
31+
private static Unsafe getTheUnsafe() {
3732
try {
3833
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
3934
theUnsafe.setAccessible(true);
40-
return Optional.of((Unsafe) theUnsafe.get(null));
35+
return (Unsafe) theUnsafe.get(null);
4136
}
4237
catch (final Exception e) {
4338
e.printStackTrace();
44-
return Optional.empty();
39+
return null;
4540
}
4641
}
4742

4843
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);
5545
}
5646

5747
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);
6449
}
6550

6651
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);
7353
}
7454

7555
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);
8257
}
8358

8459
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);
9161
}
9262

9363
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);
10065
}
10166

10267
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);
10969
}
11070

11171
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);
11873
}
11974

12075
public String getString(final int offset, final int maxLen) {
12176
final byte[] buf = new byte[maxLen];
12277

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);
13079

13180
int len = 0;
13281
while (len < maxLen && buf[len] != 0) {
13382
++len;
13483
}
135-
return new String(buf, 0, len, StandardCharsets.ISO_8859_1);
84+
return new String(buf, 0, len, charSet);
13685
}
13786

13887
public void putString(final int offset, final int maxLen, final String string) {

0 commit comments

Comments
 (0)