putFloat(): This method is available in java.nio.ByteBuffer class of Java.
Approach 1: When the method takes one argument.
Syntax:
ByteBuffer java.nio.ByteBuffer.putFloat(float value)
This method takes one argument of type float as its parameter. This method writes four bytes containing the given float 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 float 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 ByteBufferputFloat {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4 };ByteBuffer bb = ByteBuffer.wrap(array);float value = 12.3f;ByteBuffer newBB = bb.putFloat(value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
[65, 68, -52, -51]
Approach 1.1: BufferOverflowException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputFloat {public static void main(String[] args) {byte array[] = { 1, 2, 3 };ByteBuffer bb = ByteBuffer.wrap(array);float value = 12.3f;ByteBuffer newBB = bb.putFloat(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.putFloat(HeapByteBuffer.java:561)
Approach 1.2:ReadOnlyBufferException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputFloat {public static void main(String[] args) {byte array[] = { 1, 2, 3 };ByteBuffer bb = ByteBuffer.wrap(array);ByteBuffer readOnly = bb.asReadOnlyBuffer();float value = 12.3f;ByteBuffer newBB = readOnly.putFloat(value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.putFloat(HeapByteBufferR.java:564)
Approach 2: When the method takes two arguments.
Syntax:
ByteBuffer java.nio.ByteBuffer.putFloat(int index, float value)
This method takes two arguments one of type int and another of type float as its parameters. This method writes four bytes containing the given float 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 float 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 ByteBufferputFloat2 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 5, 6, 7 };ByteBuffer bb = ByteBuffer.wrap(array);int index = 2;float value = 12.45f;ByteBuffer newBB = bb.putFloat(index, value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
[1, 2, 65, 71, 51, 51, 7]
Approach 2.1: IndexOutOfBoundsException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputFloat2 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 5, 6, 7 };ByteBuffer bb = ByteBuffer.wrap(array);int index = 5;float value = 12.45f;ByteBuffer newBB = bb.putFloat(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.putFloat(HeapByteBuffer.java:572)
Approach 2.2: ReadOnlyBufferException
Java
import java.nio.ByteBuffer;import java.util.Arrays;public class ByteBufferputFloat2 {public static void main(String[] args) {byte array[] = { 1, 2, 3, 4, 5, 6, 7 };ByteBuffer bb = ByteBuffer.wrap(array);ByteBuffer readOnly = bb.asReadOnlyBuffer();int index = 2;float value = 12.45f;ByteBuffer newBB = readOnly.putFloat(index, value);System.out.println(Arrays.toString(newBB.array()));}}
Output:
Exception in thread "main" java.nio.ReadOnlyBufferException at java.base/java.nio.HeapByteBufferR.putFloat(HeapByteBufferR.java:575)
No comments:
Post a Comment