Skip to content

Commit 43f5bd5

Browse files
committed
Merge remote-tracking branch 'origin/develop' into async
2 parents 34e5d4c + d2f252b commit 43f5bd5

File tree

4 files changed

+67
-19
lines changed

4 files changed

+67
-19
lines changed

pom.xml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13-
<jmh.version>1.23</jmh.version>
13+
<jmh.version>1.26</jmh.version>
1414
</properties>
1515

1616
<build>
@@ -49,17 +49,17 @@
4949
<plugin>
5050
<groupId>org.apache.maven.plugins</groupId>
5151
<artifactId>maven-site-plugin</artifactId>
52-
<version>3.9.0</version>
52+
<version>3.9.1</version>
5353
</plugin>
5454
<plugin>
5555
<groupId>org.apache.maven.plugins</groupId>
5656
<artifactId>maven-project-info-reports-plugin</artifactId>
57-
<version>3.0.0</version>
57+
<version>3.1.1</version>
5858
</plugin>
5959
<plugin>
6060
<groupId>org.apache.maven.plugins</groupId>
6161
<artifactId>maven-surefire-plugin</artifactId>
62-
<version>2.22.0</version>
62+
<version>2.22.2</version>
6363
<configuration>
6464
<!-- <parallel>all</parallel> &lt;!&ndash; Run tests in parallel&ndash;&gt;-->
6565
<!-- <useUnlimitedThreads>true</useUnlimitedThreads>-->
@@ -113,37 +113,37 @@
113113
<dependency>
114114
<groupId>net.java.dev.jna</groupId>
115115
<artifactId>jna</artifactId>
116-
<version>5.5.0</version>
116+
<version>5.6.0</version>
117117
</dependency>
118118

119119
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform -->
120120
<dependency>
121121
<groupId>net.java.dev.jna</groupId>
122122
<artifactId>jna-platform</artifactId>
123-
<version>5.5.0</version>
123+
<version>5.6.0</version>
124124
</dependency>
125125

126126
<!-- https://mvnrepository.com/artifact/junit/junit -->
127127
<dependency>
128128
<groupId>junit</groupId>
129129
<artifactId>junit</artifactId>
130-
<version>4.13</version>
130+
<version>4.13.1</version>
131131
<scope>test</scope>
132132
</dependency>
133133

134134
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
135135
<dependency>
136136
<groupId>org.mockito</groupId>
137137
<artifactId>mockito-core</artifactId>
138-
<version>3.3.3</version>
138+
<version>3.5.15</version>
139139
<scope>test</scope>
140140
</dependency>
141141

142142
<!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
143143
<dependency>
144144
<groupId>org.assertj</groupId>
145145
<artifactId>assertj-core</artifactId>
146-
<version>3.16.1</version>
146+
<version>3.17.2</version>
147147
<scope>test</scope>
148148
</dependency>
149149

src/main/java/bwapi/UnsafeTools.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,39 @@
33
import sun.misc.Unsafe;
44

55
import java.lang.reflect.Field;
6+
import java.nio.Buffer;
67

78
class UnsafeTools {
8-
private static Unsafe unsafe;
99

10-
static {
11-
try {
12-
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
13-
theUnsafe.setAccessible(true);
14-
unsafe = (Unsafe) theUnsafe.get(null);
10+
private static Object getOrCrash(final Class<?> className, final Object object, final String fieldName) {
11+
try { // get
12+
final Field field = className.getDeclaredField(fieldName);
13+
final boolean accessible = field.isAccessible();
14+
if (!accessible) {
15+
field.setAccessible(true);
16+
}
17+
final Object result = field.get(object);
18+
if (!accessible) {
19+
field.setAccessible(false);
20+
}
21+
return result;
1522

16-
} catch (final Exception e) {
23+
} catch (final Exception e) { // or crash...
1724
e.printStackTrace();
1825
System.exit(-1);
26+
return null;
1927
}
2028
}
2129

2230
static Unsafe getUnsafe() {
23-
return unsafe;
31+
return (Unsafe) getOrCrash(Unsafe.class, null, "theUnsafe");
32+
}
33+
34+
/**
35+
* Alternative to `((DirectBuffer) buffer).address())`
36+
* (ab)using reflection
37+
*/
38+
static long getAddress(final Buffer buffer) {
39+
return (long) getOrCrash(Buffer.class, buffer, "address");
2440
}
2541
}

src/main/java/bwapi/WrappedBuffer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package bwapi;
22

3-
import com.sun.jna.Memory;
43
import com.sun.jna.Pointer;
54
import sun.misc.Unsafe;
65

@@ -16,7 +15,8 @@ class WrappedBuffer {
1615
private static final Unsafe unsafe = UnsafeTools.getUnsafe();
1716

1817
WrappedBuffer(final int size) {
19-
this(new Memory(size), size);
18+
buffer = ByteBuffer.allocateDirect(size);
19+
address = UnsafeTools.getAddress(buffer);
2020
}
2121

2222
WrappedBuffer(final Pointer pointer, final int size) {
@@ -77,6 +77,7 @@ void putString(final int offset, final int maxLen, final String string) {
7777
unsafe.putByte(pos, (byte) 0);
7878
}
7979

80+
8081
ByteBuffer getBuffer() {
8182
return buffer;
8283
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package bwapi;
2+
3+
import org.junit.Test;
4+
import sun.misc.Unsafe;
5+
6+
import java.nio.ByteBuffer;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class UnsafeToolsTest {
11+
12+
@Test
13+
public void testGettingUnsafe() {
14+
assertNotNull(UnsafeTools.getUnsafe());
15+
}
16+
17+
@Test
18+
public void testDirectByteBuffer() {
19+
byte[] bytes = {112, 117, 114, 112, 108, 101, 119, 97, 118, 101};
20+
final ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
21+
buffer.put(bytes);
22+
23+
long address = UnsafeTools.getAddress(buffer);
24+
assertNotEquals(0, address);
25+
26+
Unsafe unsafe = UnsafeTools.getUnsafe(); //to obtain memory values via the address
27+
for (int i=0; i < bytes.length; i++) {
28+
assertEquals(bytes[i], unsafe.getByte(address + i));
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)