Last active
November 23, 2015 06:56
-
-
Save jinahya/4b5091eb66e1c5554281 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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