put(ByteBuffer): This method is available in java.nio.ByteBuffer class of Java.
Syntax:
ByteBuffer java.nio.ByteBuffer.put(ByteBuffer src)
This method takes one argument of type ByteBuffer as its parameter. This method transfers the bytes remaining in the given source buffer into this buffer.
Note:
1. If there are more bytes remaining in the source buffer than in this buffer, that is, if src.remaining() > remaining(),then no bytes are transferred and a BufferOverflowException is thrown.
2. Otherwise, this method copies n = src.remaining() bytes from the given buffer into this buffer, starting at each buffer's current position. The positions of both buffers are then incremented by n.
Parameters: One parameter is required for this method.
src: The source buffer from which bytes are to be read; must not be this buffer.
Returns: This buffer.
Throws:
1. BufferOverflowException - If there is insufficient space in this buffer for the remaining bytes in the source buffer.
2. IllegalArgumentException - If the source buffer is this buffer.
3. ReadOnlyBufferException - If this buffer is read-only
Approach 1: When no exceptions.
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput3 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 5 };ByteBuffer bb = ByteBuffer.wrap(array);byte array2[] = { 10, 20, 30, 56 };ByteBuffer src = ByteBuffer.wrap(array2);System.out.println(Arrays.toString(bb.put(src).array()));}}
Output:
[10, 20, 30, 56, 5]
Approach 2: BufferOverflowException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput3 {public static void main(String[] args) {byte array[] = { 1, 2, 3 };ByteBuffer bb = ByteBuffer.wrap(array);byte array2[] = { 10, 20, 30, 56 };ByteBuffer src = ByteBuffer.wrap(array2);System.out.println(Arrays.toString(bb.put(src).array()));}}
Output:
Exception in thread "main" java.nio.BufferOverflowException at java.base/java.nio.ByteBuffer.put(ByteBuffer.java:957) at java.base/java.nio.HeapByteBuffer.put(HeapByteBuffer.java:247)
Approach 3: IllegalArgumentException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput3 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 5 };ByteBuffer bb = ByteBuffer.wrap(array);System.out.println(Arrays.toString(bb.put(bb).array()));}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: The source buffer is this buffer at java.base/java.nio.Buffer.createSameBufferException(Buffer.java:261) at java.base/java.nio.ByteBuffer.put(ByteBuffer.java:949) at java.base/java.nio.HeapByteBuffer.put(HeapByteBuffer.java:247)
Approach 4: ReadOnlyBufferException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferput3 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 5 };ByteBuffer bb = ByteBuffer.wrap(array);ByteBuffer readOnly = bb.asReadOnlyBuffer();byte array2[] = { 10, 20, 30, 56 };ByteBuffer src = ByteBuffer.wrap(array2);System.out.println(Arrays.toString(readOnly.put(src).array()));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.put(HeapByteBufferR.java:250)
No comments:
Post a Comment