Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
floatbuffer

What is the FloatBuffer asReadOnlyBuffer() method in Java?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

java.nio.FloatBuffer is a class that is used to store a buffer of floats. The asReadOnlyBuffer() method of the class java.nio.FloatBuffer is used to create a read-only copy of a buffer. Creating a read-only copy of a buffer buff means:

  • The read-only copy has the same elements as that of buff.
  • The positionThe index of the next element of the buffer that will be read or written., limitThe first index of the buffer that should not be read or written., capacityThe number of elements a buffer contains, and markA recorded position of a buffer values of the read-only copy will be identical to that of buff.
  • Any modifications to the content of the read-only copy will not be allowed. If a modification is tried on a read-only buffer, the ReadOnlyBufferException is thrown.

Declaration

The FloatBuffer.asReadOnlyBuffer() method is declared as follows:

buff.asReadOnlyBuffer()
  • buff: The FloatBuffer whose read-only copy will be created.

Return value

The FloatBuffer.asReadOnlyBuffer() method returns a read-only copy of buff.

Note: If buff is read-only, the FloatBuffer.asReadOnlyBuffer() method behaves in the same way as the FloatBuffer.duplicate() method.

Examples

Example 1

Consider the code snippet below, which demonstrates the use of the FloatBuffer.asReadOnlyBuffer() method.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 6;
try {
FloatBuffer buff1 = FloatBuffer.allocate(n1);
buff1.put(1.3F);
buff1.put(2.8F);
buff1.put(3.2F);
buff1.put(4.5F);
buff1.put(5.2F);
buff1.put(6.6F);
buff1.position(2);
buff1.limit(5);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
System.out.println("position at: " + buff1.position());
System.out.println("Limit at: " + buff1.limit());
FloatBuffer buff2 = buff1.asReadOnlyBuffer();
boolean foo = buff2.equals(buff1);
System.out.println("buff2 is equal to buff1: " + foo);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A FloatBuffer buff1 is declared in line 7 with the capacity n1 = 6.
  • Six elements are added to buff1 using the put() method in lines 8-13.
  • The position of buff1 is set to 2 using the position() method in line 15.
  • The limit of buff1 is set to 5 using the limit() method in line 16.
  • A FloatBuffer buff2 is declared in line 22 that is the read-only copy of buff1. The read-only copy buff2 is created using the FloatBuffer.asReadOnlyBuffer() method.
  • The FloatBuffer.equals() method is used in line 24 to check if buff1 is equal to its read-only copy buff2. The FloatBuffer.equals() method returns true, which means that buff1 is equal to buff2.

Example 2

As explained above, any modification on a read-only buffer throws the ReadOnlyBufferException. Consider the code snippet below in which a read-only buffer is modified.

import java.nio.*;
import java.util.*;
public class main {
public static void main(String[] args) {
int n1 = 6;
try {
FloatBuffer buff1 = FloatBuffer.allocate(n1);
buff1.put(1.3F);
buff1.put(2.8F);
buff1.put(3.2F);
buff1.put(4.5F);
buff1.put(5.2F);
buff1.put(6.6F);
buff1.position(2);
buff1.limit(5);
System.out.println("buff1: " + Arrays.toString(buff1.array()));
System.out.println("position at: " + buff1.position());
System.out.println("Limit at: " + buff1.limit());
FloatBuffer buff2 = buff1.asReadOnlyBuffer();
buff2.put(6.6F);
} catch (IllegalArgumentException e) {
System.out.println("Error!!! IllegalArgumentException");
} catch (ReadOnlyBufferException e) {
System.out.println("Error!!! ReadOnlyBufferException");
}
}
}

Explanation

  • A FloatBuffer buff1 is declared in line 7 with the capacity n1 = 6.
  • Six elements are added to buff1 using the put() method in lines 8-13.
  • The position of buff1 is set to 2 using the position() method in line 15.
  • The limit of buff1 is set to 5 using the limit() method in line 16.
  • A FloatBuffer buff2 is declared in line 22 that is the read-only copy of buff1. The read-only copy buff2 is created using the FloatBuffer.asReadOnlyBuffer() method.
  • The FloatBuffer.put() method is used in line 24 to try writing a value to buff2. The ReadOnlyBufferException is thrown because buff2 is read-only and cannot be modified.

RELATED TAGS

java
floatbuffer

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring