putInt(): This method is available in java.nio.ByteBuffer class of Java.
Approach 1: When the method takes one argument.
Syntax:
ByteBuffer java.nio.ByteBuffer.putInt(int value)
This method takes one argument of type int as its parameter. This method writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.
Parameters: One parameter is required for this method.
value: The int value to be written.
Returns: This buffer.
Throws:
1. BufferOverflowException - If there are fewer than four bytes remaining in this buffer.
2. ReadOnlyBufferException - If this buffer is read-only.
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputInt {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);int value = 12;ByteBuffer newBB = bb.putInt(value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
[0, 0, 0, 12]
Approach 1.1: BufferOverflowException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputInt {public static void main(String[] args) {byte array[] = { 1, 2, 3 };ByteBuffer bb = ByteBuffer.wrap(array);int value = 12;ByteBuffer newBB = bb.putInt(value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
Exception in thread "main" java.nio.BufferOverflowException at java.base/java.nio.Buffer.nextPutIndex(Buffer.java:725) at java.base/java.nio.HeapByteBuffer.putInt(HeapByteBuffer.java:446)
Approach 1.2: ReadOnlyBufferException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputInt {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);ByteBuffer readOnly = bb.asReadOnlyBuffer();int value = 12;ByteBuffer newBB = readOnly.putInt(value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.putInt(HeapByteBufferR.java:449)
Approach 2: When the method takes two arguments.
Syntax:
ByteBuffer java.nio.ByteBuffer.putInt(int index, int value)
This method takes two arguments of type int as its parameters. This method writes four bytes containing the given int value, in the current byte order, into this buffer at the given index.
Parameters: Two parameters are required for this method.
index: The index at which the bytes will be written.
value: The int value to be written.
Returns: This buffer.
Throws:
1. IndexOutOfBoundsException - If the index is negative or not smaller than the buffer's limit, minus three.
2. ReadOnlyBufferException - If this buffer is read-only
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputInt2 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 6, 7, 8 };ByteBuffer bb = ByteBuffer.wrap(array);int index = 2;int value = 12;ByteBuffer newBB = bb.putInt(index, value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
[1, 2, 0, 0, 0, 12, 8]
Approach 2.1: IndexOutOfBoundsException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputInt2 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 6, 7, 8 };ByteBuffer bb = ByteBuffer.wrap(array);int index = 5;int value = 12;ByteBuffer newBB = bb.putInt(index, value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.nio.Buffer.checkIndex(Buffer.java:744) at java.base/java.nio.HeapByteBuffer.putInt(HeapByteBuffer.java:456)
Approach 2.2: ReadOnlyBufferException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputInt2 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 6, 7, 8 };ByteBuffer bb = ByteBuffer.wrap(array);ByteBuffer readOnly = bb.asReadOnlyBuffer();int index = 2;int value = 12;ByteBuffer newBB = readOnly.putInt(index, value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.putInt(HeapByteBufferR.java:459)
No comments:
Post a Comment