Skip to content

Commit 5885044

Browse files
committed
remove dependency on directbuffer
1 parent 1289a23 commit 5885044

File tree

7 files changed

+49
-45
lines changed

7 files changed

+49
-45
lines changed

src/main/java/bwapi/Client.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ of this software and associated documentation files (the "Software"), to deal
2929
import bwapi.ClientData.GameData;
3030
import bwapi.ClientData.Shape;
3131
import com.sun.jna.Native;
32+
import com.sun.jna.Pointer;
3233
import com.sun.jna.platform.win32.Kernel32;
3334
import com.sun.jna.win32.W32APIOptions;
3435

@@ -57,7 +58,7 @@ public interface EventHandler {
5758
private ClientData.GameData gameData;
5859
private boolean connected = false;
5960
private RandomAccessFile pipeObjectHandle = null;
60-
private ByteBuffer mapFileHandle = null;
61+
private WrappedBuffer mapFileHandle = null;
6162
private ByteBuffer gameTableFileHandle = null;
6263

6364
private boolean debugConnection = false;
@@ -69,8 +70,8 @@ public interface EventHandler {
6970
/**
7071
* For test purposes only
7172
*/
72-
Client(ByteBuffer buffer) {
73-
clientData = new ClientData(buffer);
73+
Client(final WrappedBuffer memory) {
74+
clientData = new ClientData(memory);
7475
gameData = clientData.new GameData(0);
7576
}
7677

@@ -130,7 +131,6 @@ boolean connect() {
130131
gameTableFileHandle = Kernel32.INSTANCE.MapViewOfFile(
131132
MappingKernel.INSTANCE.OpenFileMapping(READ_WRITE, false, "Local\\bwapi_shared_memory_game_list"), READ_WRITE, 0, 0, GameTable.SIZE)
132133
.getByteBuffer(0, GameTable.SIZE);
133-
gameTableFileHandle.order(ByteOrder.LITTLE_ENDIAN);
134134
}
135135
catch (Exception e) {
136136
System.err.println("Game table mapping not found.");
@@ -186,9 +186,10 @@ boolean connect() {
186186
System.out.println("Connected");
187187

188188
try {
189-
mapFileHandle = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
189+
final Pointer mapFileView = Kernel32.INSTANCE.MapViewOfFile(MappingKernel.INSTANCE
190190
.OpenFileMapping(READ_WRITE, false, sharedMemoryName), READ_WRITE,
191-
0, 0, GameData.SIZE).getByteBuffer(0, GameData.SIZE);
191+
0, 0, GameData.SIZE);
192+
mapFileHandle = new WrappedBuffer(mapFileView, GameData.SIZE);
192193
}
193194
catch (Exception e) {
194195
System.err.println("Unable to open shared memory mapping: " + sharedMemoryName);

src/main/java/bwapi/ClientData.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package bwapi;
2-
import java.nio.ByteBuffer;
2+
33
final class ClientData {
44
final WrappedBuffer buffer;
5-
ClientData(final ByteBuffer buffer) {
6-
this.buffer = new WrappedBuffer(buffer);
5+
6+
ClientData(final WrappedBuffer buffer) {
7+
this.buffer = buffer;
78
}
89
class UnitCommand {
910
static final int SIZE = 24;

src/main/java/bwapi/WrappedBuffer.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package bwapi;
22

3+
import com.sun.jna.Memory;
4+
import com.sun.jna.Pointer;
35
import sun.misc.Unsafe;
4-
import sun.nio.ch.DirectBuffer;
56

67
import java.lang.reflect.Field;
78
import java.nio.ByteBuffer;
@@ -10,27 +11,33 @@
1011
* Wrapper around ByteBuffer that makes use of sun.misc.Unsafe if available.
1112
*/
1213
class WrappedBuffer {
13-
private final ByteBuffer buffer;
14+
private final Pointer pointer;
15+
private final int size;
1416
private final long address;
15-
private final Unsafe unsafe;
1617

17-
WrappedBuffer(final ByteBuffer byteBuffer) {
18-
unsafe = getTheUnsafe();
19-
buffer = byteBuffer;
20-
address = ((DirectBuffer) buffer).address();
21-
}
22-
23-
private static Unsafe getTheUnsafe() {
18+
private static Unsafe unsafe;
19+
static {
2420
try {
2521
final Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
2622
theUnsafe.setAccessible(true);
27-
return (Unsafe) theUnsafe.get(null);
23+
unsafe = (Unsafe) theUnsafe.get(null);
24+
2825
} catch (final Exception e) {
2926
e.printStackTrace();
30-
return null;
27+
System.exit(-1);
3128
}
3229
}
3330

31+
WrappedBuffer(final int size) {
32+
this(new Memory(size), size);
33+
}
34+
35+
WrappedBuffer(final Pointer pointer, final int size) {
36+
this.pointer = pointer;
37+
this.size = size;
38+
this.address = Pointer.nativeValue(pointer);
39+
}
40+
3441
byte getByte(final int offset) {
3542
return unsafe.getByte(address + offset);
3643
}
@@ -64,7 +71,7 @@ void putDouble(final int offset, final double value) {
6471
}
6572

6673
String getString(final int offset, final int maxLen) {
67-
char[] buf = new char[maxLen];
74+
final char[] buf = new char[maxLen];
6875
long pos = offset + address;
6976
for (int i = 0; i < maxLen; i++) {
7077
byte b = unsafe.getByte(pos);
@@ -88,6 +95,6 @@ void putString(final int offset, final int maxLen, final String string) {
8895
}
8996

9097
ByteBuffer getBuffer() {
91-
return buffer;
98+
return pointer.getByteBuffer(0, size);
9299
}
93100
}

src/test/java/bwapi/ClientDataBenchmark.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.openjdk.jmh.annotations.*;
44
import org.openjdk.jmh.infra.Blackhole;
55

6-
import java.nio.ByteBuffer;
76
import java.util.SplittableRandom;
87
import java.util.stream.Collectors;
98

@@ -19,7 +18,7 @@ public static class EmptyState {
1918

2019
@Setup(Level.Invocation)
2120
public void setup() {
22-
client = new Client(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
21+
client = new Client(new WrappedBuffer(ClientData.GameData.SIZE));
2322
game = new Game(client);
2423
strings = buildStrings();
2524
}
@@ -34,7 +33,7 @@ public static class FilledWithStrings {
3433

3534
@Setup(Level.Invocation)
3635
public void setup() {
37-
client = new Client(ByteBuffer.allocateDirect(ClientData.GameData.SIZE));
36+
client = new Client(new WrappedBuffer(ClientData.GameData.SIZE));
3837
data = client.gameData();
3938
game = new Game(client);
4039
String[] strings = buildStrings();

src/test/java/bwapi/GameBuilder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
5-
import java.nio.Buffer;
6-
import java.nio.ByteBuffer;
75
import java.nio.file.Files;
86
import java.nio.file.Paths;
97
import java.util.zip.InflaterOutputStream;
@@ -15,20 +13,20 @@ public static Game createGame() throws IOException {
1513
}
1614

1715
public static Game createGame(String mapName) throws IOException {
18-
final ByteBuffer buffer = binToBuffer("src/test/resources/" + mapName + "_frame0_buffer.bin");
16+
final WrappedBuffer buffer = binToBuffer("src/test/resources/" + mapName + "_frame0_buffer.bin");
1917
return createGame(new Client(buffer));
2018
}
2119

22-
public static ByteBuffer binToBuffer(String binLocation) throws IOException {
20+
public static WrappedBuffer binToBuffer(String binLocation) throws IOException {
2321
final byte[] compressedBytes = Files.readAllBytes(Paths.get(binLocation));
2422
final ByteArrayOutputStream out = new ByteArrayOutputStream();
2523
final InflaterOutputStream zin = new InflaterOutputStream(out);
2624
zin.write(compressedBytes);
2725
zin.flush();
2826
zin.close();
2927
final byte[] bytes = out.toByteArray();
30-
final ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.length);
31-
buffer.put(bytes);
28+
final WrappedBuffer buffer = new WrappedBuffer(bytes.length);
29+
buffer.getBuffer().put(bytes);
3230
return buffer;
3331
}
3432

src/test/java/bwapi/GameTest.java

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

3-
import static org.assertj.core.api.Assertions.assertThat;
4-
import static org.junit.Assert.assertNull;
5-
import static org.mockito.BDDMockito.given;
6-
import static org.mockito.Mockito.mock;
7-
8-
import java.io.IOException;
9-
import java.nio.ByteBuffer;
10-
import java.util.ArrayList;
11-
import java.util.List;
123
import org.junit.Before;
134
import org.junit.Test;
145
import org.junit.experimental.theories.DataPoints;
@@ -17,6 +8,15 @@
178
import org.junit.experimental.theories.Theory;
189
import org.junit.runner.RunWith;
1910

11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.junit.Assert.assertNull;
17+
import static org.mockito.BDDMockito.given;
18+
import static org.mockito.Mockito.mock;
19+
2020
@RunWith(Theories.class)
2121
public class GameTest {
2222

@@ -89,7 +89,7 @@ public void shouldNotFindNonOverlappingUnits(
8989

9090
@Test
9191
public void ifReplaySelfAndEnemyShouldBeNull() throws IOException {
92-
ByteBuffer buffer = GameBuilder.binToBuffer("src/test/resources/" + "(2)Benzene.scx" + "_frame0_buffer.bin");
92+
WrappedBuffer buffer = GameBuilder.binToBuffer("src/test/resources/" + "(2)Benzene.scx" + "_frame0_buffer.bin");
9393

9494
Client client = new Client(buffer);
9595
// modify the buffer to fake a replay

src/test/java/bwapi/WrappedBufferTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import org.junit.Test;
44

5-
import java.nio.ByteBuffer;
6-
75
import static org.assertj.core.api.Assertions.assertThat;
86

97
public class WrappedBufferTest {
10-
private WrappedBuffer sut = new WrappedBuffer(ByteBuffer.allocateDirect(1024));
8+
private final WrappedBuffer sut = new WrappedBuffer(1024);
119

1210
@Test
1311
public void shouldGetAndSetStrings() {

0 commit comments

Comments
 (0)