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
andposition The positionof a buffer is the index of the next element of the buffer that will be read or written. of a buffer to its beginning.limit The limitof a buffer is the first index that should not be read or written. - The
positionof the buffer is set equal to the number of values copied. - The
limitof the buffer is set equal to itscapacity.The capacityof a buffer is the number of elements the buffer contains. - If a
markis defined, it is discarded. Marking a position means recording a position that can be restored by theFloatBuffer.reset()method. This marked position is discarded by theFloatBuffer.compact()method.
Declaration
The FloatBuffer.compact() method is declared as follows:
buff.compact()
buff: TheFloatBufferthat 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,
ReadOnlyBufferExceptionis 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
FloatBufferbuff1is declared in line 7 with the capacityn1 = 6. - Six elements are added to
buff1using theput()method in lines 8-13. - The position of
buff1is set to 2 using theposition()method in line 15. - The limit of
buff1is set to 5 using thelimit()method in line 16. - The
FloatBuffer.compact()method is used in line 22 to compactbuff1. The elements between thepositionandlimitofbuff1are copied to its beginning. Thepositionofbuff1is set equal to the number of elements copied. Thelimitofbuff1is set equal to itscapacity.
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
FloatBufferbuff1is declared in line 7 with the capacityn1 = 6. - Six elements are added to
buff1using theput()method in lines 8-13. - The position of
buff1is set to 2 using theposition()method in line 15. - The limit of
buff1is set to 5 using thelimit()method in line 16. - A
FloatBufferbuff2is declared in line 22 that is the read-only copy ofbuff1. - The
FloatBuffer.compact()method is used in line 24 to compactbuff2. TheReadOnlyBufferExceptionis thrown becausebuff2is read-only.
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved