Skip to content

Commit 318c36d

Browse files
committed
use unsafe for getString but not for putString (probably not faster as you need to encode anyway)
1 parent 58b4242 commit 318c36d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/main/java/bwapi/WrappedBuffer.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,15 @@ public void putDouble(final int offset, final double value) {
119119

120120
public String getString(final int offset, final int maxLen) {
121121
final byte[] buf = new byte[maxLen];
122-
buffer.position(offset);
123-
buffer.get(buf, 0, maxLen);
124-
buffer.position(0);
122+
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+
}
130+
125131
int len = 0;
126132
while (len < maxLen && buf[len] != 0) {
127133
++len;
@@ -130,13 +136,11 @@ public String getString(final int offset, final int maxLen) {
130136
}
131137

132138
public void putString(final int offset, final int maxLen, final String string) {
133-
final int len = string.length() + 1;
134-
if (len >= maxLen) {
139+
if (string.length() + 1 >= maxLen) {
135140
throw new StringIndexOutOfBoundsException();
136141
}
137142
buffer.position(offset);
138143
enc.encode(CharBuffer.wrap(string), buffer, true);
139144
buffer.put((byte) 0);
140-
buffer.rewind();
141145
}
142146
}

0 commit comments

Comments
 (0)