put(byte[], int, int): This method is available in java.nio.ByteBuffer class of Java.
Syntax:
ByteBuffer java.nio.ByteBuffer.put(byte[] src, int offset, int length)
This method takes three arguments one of type byte array and rest two are of type int as its parameters. This method transfers bytes into this buffer from the given source array.
Note:
1. If there are more bytes to be copied from the array than remain in this buffer, that is, if length > remaining(), then no bytes are transferred and a BufferOverflowException is thrown.
2. Otherwise, this method copies length bytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length.
Parameters: Three parameters are required for this method.
src: The array from which bytes are to be read.
offset: The offset within the array of the first byte to be read; must be non-negative and no larger than src.length.
length: The number of bytes to be read from the given array; must be non-negative and no larger than src.length - offset.
Returns: This buffer.
Throws:
1. BufferOverflowException - If there is insufficient space in this buffer.
2. IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold.
3. ReadOnlyBufferException - If this buffer is read-only
Approach 1: When no exceptions.
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput6 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);byte src[] = { 90, 45, 67 };int offset = 0, length = 2;System.out.println(Arrays.toString(bb.put(src, offset, length).array()));}}
Output:
[90, 45, 3, 4]
Approach 2: BufferOverflowException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput6 {public static void main(String[] args) {byte array[] = { 1, 2 };ByteBuffer bb = ByteBuffer.wrap(array);byte src[] = { 90, 45, 67 };int offset = 0, length = 3;System.out.println(Arrays.toString(bb.put(src, offset, length).array()));}}
Output:
Exception in thread "main" java.nio.BufferOverflowException at java.base/java.nio.HeapByteBuffer.put(HeapByteBuffer.java:235)
Approach 3: IndexOutOfBoundsException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput6 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);byte src[] = { 90, 45, 67 };int offset = 0, length = 10;System.out.println(Arrays.toString(bb.put(src, offset, length).array()));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Range [0, 0 + 10) out of bounds for length 3 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.put(HeapByteBuffer.java:232)
Approach 4: ReadOnlyBufferException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput6 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);ByteBuffer readOnly = bb.asReadOnlyBuffer();byte src[] = { 90, 45, 67 };int offset = 0, length = 2;System.out.println(Arrays.toString(readOnly.put(src,offset, length).array()));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.put(HeapByteBufferR.java:240)
No comments:
Post a Comment