Help me understand something.
Using .. import sun.misc.Unsafe;
If i need to put a character 0-127
to some memory address, in order to prevent out of range chars, i do this
if (0 != (c & 0xFF80)) {
throw new RuntimeException("Only Ascii characters are supported. 0-127.");
} else {
// Since Java's chars are 16 bits long, i cast it
// to byte (8 bits) and then `putByte()`
UNSAFE.putByte(address, (byte) c);
}
When my test checks what's in that memory
char c1 = Ascii.a;
asciiEncoder.encode(address, c1);
assertThat(unsafe.getChar(address), is(c1));
This test seems to pass and fail on some machines. Let me repeat that - this works on some MACs and not the others and some Windows and not the others.
What is failing? Character different to 'a' is returned.
As i understand it, this is possible if upon allocation, memory is not cleaned up, but does not this take care of this?
@Before
public void setUp() {
address = unsafe.allocateMemory(64); // Isn't this mem clean?
}
If i change this to 8 bits, test passes always
@Before
public void setUp() {
address = unsafe.allocateMemory(8);
}
Please help me understand