What is the FloatBuffer compact() method in Java?

java.nio.FloatBuffer is a class we can use to store a buffer of floats. The compact() method of the java.nio.FloatBuffer class compacts a buffer. Compacting a buffer means:

  • Copying the values between the positionThe position of a buffer is the index of the next element of the buffer that will be read or written. and limitThe limit of a buffer is the first index that should not be read or written. of a buffer to its beginning.
  • The position of the buffer is set equal to the number of values copied.
  • The limit of the buffer is set equal to its capacity.The capacity of a buffer is the number of elements the buffer contains.
  • If a mark is defined, it is discarded. Marking a position means recording a position that can be restored by the FloatBuffer.reset() method. This marked position is discarded by the FloatBuffer.compact() method.

Declaration

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

buff.compact()
  • buff: The FloatBuffer that will be compacted.

Return value

The FloatBuffer.compact() method returns a compacted copy of buff.

If the buffer is backed by an array but the array is read-only, ReadOnlyBufferException is thrown.

Code

Example 1

Consider the code snippet below, which demonstrates the use of the FloatBuffer.compact() 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(before compact): " + buff1.position());
System.out.println("Limit at(before compact): " + buff1.limit());
buff1 = buff1.compact();
System.out.println("compact()");
System.out.println("buff1: " + Arrays.toString(buff1.array()));
System.out.println("position at(after compact): " + buff1.position());
System.out.println("Limit at(after compact): " + buff1.limit());
} 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.
  • The FloatBuffer.compact() method is used in line 22 to compact buff1. The elements between the position and limit of buff1 are copied to its beginning. The position of buff1 is set equal to the number of elements copied. The limit of buff1 is set equal to its capacity.

Example 2

Consider another example of the FloatBuffer.compact() method in which ReadOnlyBufferException is thrown.

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(before compact): " + buff1.position());
System.out.println("Limit at(before compact): " + buff1.limit());
FloatBuffer buff2 = buff1.asReadOnlyBuffer();
buff2 = buff2.compact();
System.out.println("compact()");
System.out.println("buff2: " + Arrays.toString(buff2.array()));
System.out.println("position at(after compact): " + buff2.position());
System.out.println("Limit at(after compact): " + buff2.limit());
} 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 FloatBuffer.compact() method is used in line 24 to compact buff2. The ReadOnlyBufferException is thrown because buff2 is read-only.
Copyright ©2024 Educative, Inc. All rights reserved