get(byte[], int, int): This method is available in java.nio.ByteBuffer class of Java.
Syntax:
ByteBuffer java.nio.ByteBuffer.get(byte[] dst, int offset, int length)
This method takes three arguments one of type byte array and the other two are of type int as its parameters. This method transfers bytes from this buffer into the given destination array.
1. If there are fewer bytes remaining in the buffer then are required to satisfy the request, that is, if length > remaining(), then no bytes are transferred and a BufferUnderflowException is thrown.
2. Otherwise, this method copies length bytes from this buffer into the given array, starting at the current position of this buffer and at the given offset in the array. The position of this buffer is then incremented by length.
Parameters: Three parameters are required for this method.
dst: The array into which bytes are to be written.
offset: The offset within the array of the first byte to be written; must be non-negative and no larger than dst.length.
length: The maximum number of bytes to be written to the given array; must be non-negative and no larger than dst.length - offset.
Returns: This buffer.
Throws:
1. BufferUnderflowException - If there are fewer than length bytes remaining in this buffer.
2. IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
Approach 1: When no exceptions.
Java
import java.nio.ByteBuffer;public class ByteBufferget5 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 10, 89, 18, 23, 5 };ByteBuffer bb = ByteBuffer.wrap(array);byte dst[] = { 1, 2, 3, 4 };int offset = 0, length = 3;System.out.println(bb.get(dst, offset, length));}}
Output:
java.nio.HeapByteBuffer[pos=3 lim=9 cap=9]
Approach 2: BufferUnderflowException
Java
import java.nio.ByteBuffer;public class ByteBufferget5 {public static void main(String[] args) {byte array[] = { 1, 2 };ByteBuffer bb = ByteBuffer.wrap(array);byte dst[] = { 1, 2, 3, 4 };int offset = 0, length = 3;System.out.println(bb.get(dst, offset, length));}}
Output:
Exception in thread "main" java.nio.BufferUnderflowException at java.base/java.nio.HeapByteBuffer.get(HeapByteBuffer.java:185)
Approach 3: IndexOutOfBoundsException
Java
import java.nio.ByteBuffer;public class ByteBufferget5 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 10, 89, 18, 23, 5 };ByteBuffer bb = ByteBuffer.wrap(array);byte dst[] = { 1, 2, 3, 4 };int offset = 6, length = 3;System.out.println(bb.get(dst, offset, length));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [6, 6 + 3) out of bounds for length 4 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromIndexSize(Preconditions.java:82) at java.base/jdk.internal.util.Preconditions.checkFromIndexSize(Preconditions.java:343) at java.base/java.util.Objects.checkFromIndexSize(Objects.java:411) at java.base/java.nio.HeapByteBuffer.get(HeapByteBuffer.java:182)
No comments:
Post a Comment