Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/main/java/bwapi/WrappedBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
* Wrapper around ByteBuffer that makes use of sun.misc.Unsafe if available.
*/
class WrappedBuffer {
private static final Charset charSet = StandardCharsets.ISO_8859_1;
private static final CharsetEncoder enc = charSet.newEncoder();

private final ByteBuffer buffer;
private final long address;
private final Unsafe unsafe;
Expand Down Expand Up @@ -72,25 +69,26 @@ void putDouble(final int offset, final double value) {
}

String getString(final int offset, final int maxLen) {
buffer.position(offset);
ByteBuffer source = buffer.slice();
int rem = maxLen - 1;
while (source.get() != 0 && rem > 0) {
rem--;
}
if (rem > 0) {
source.position(source.position() - 1);
char[] buf = new char[maxLen];
long pos = offset + address;
for (int i = 0; i < maxLen; i++) {
byte b = unsafe.getByte(pos);
if (b == 0) break;
buf[i] = (char) (b & 0xff);
pos++;
}
source.flip();
return charSet.decode(source).toString();
return new String(buf, 0, (int) (pos - offset - address));
}

void putString(final int offset, final int maxLen, final String string) {
if (string.length() + 1 >= maxLen) {
throw new StringIndexOutOfBoundsException();
}
buffer.position(offset);
enc.encode(CharBuffer.wrap(string), buffer, true);
buffer.put((byte) 0);
long pos = offset + address;
for (int i = 0; i < string.length(); i++) {
unsafe.putByte(pos, (byte) string.charAt(i));
pos++;
}
unsafe.putByte(pos, (byte) 0);
}
}
8 changes: 8 additions & 0 deletions src/test/java/bwapi/ClientDataBenchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ private static String[] buildStrings() {
return strings;
}

@Benchmark
@Measurement(iterations = 2, time = 2)
@Warmup(time = 2)
@Fork(2)
public void reference(Blackhole blackhole) {
blackhole.consume(0);
}

@Benchmark
@OperationsPerInvocation(Client.MAX_COUNT)
public int addUnitCommand(EmptyState s) {
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/bwapi/WrappedBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class WrappedBufferTest {
@Test
public void shouldGetAndSetStrings() {
// GIVEN
String testString = "Test";
String testString = "@µöú";
sut.putString(123, 100, testString);

// WHEN
Expand All @@ -25,7 +25,7 @@ public void shouldGetAndSetStrings() {
@Test
public void shouldCutOffAtMaxLength() {
// GIVEN
String testString = "Test";
String testString = "@µöú";
sut.putString(123, 100, testString);

// WHEN
Expand Down