Skip to content

Instantly share code, notes, and snippets.

@jinahya
Last active November 23, 2015 06:56
Show Gist options
  • Save jinahya/4b5091eb66e1c5554281 to your computer and use it in GitHub Desktop.
Save jinahya/4b5091eb66e1c5554281 to your computer and use it in GitHub Desktop.
import java.nio.ByteBuffer;
import java.util.Arrays;
import static java.util.concurrent.ThreadLocalRandom.current;
public class ArrayWrappingByteBufferTest {
public static void main(final String[] args) {
final byte[] array = new byte[] {1, 1, 1, 1, 1, 1, 1, 1};
System.out.println("array: " + Arrays.toString(array));
final int offset = 1;
final int length = 6;
final ByteBuffer buffer = ByteBuffer.wrap(array, offset, length);
assert buffer.capacity() == array.length;
assert buffer.position() == offset;
assert buffer.limit() == offset + length;
// change the value before offset
buffer.position(0);
buffer.put((byte) 0);
System.out.println("array: " + Arrays.toString(array));
// change the value after offset + length
buffer.limit(8);
buffer.position(7);
buffer.put((byte) 0);
System.out.println("array: " + Arrays.toString(array));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment