What is the ByteBuffer compact() method in Java?
java.nio.ByteBuffer is a class that is used to store a buffer of bytes. The compact() method of the class java.nio.ByteBuffer is used to compact a buffer. Compacting a buffer means:
- Copying the values between the
positionandlimitof a buffer to its beginning. - The position of the buffer is set equal to the number of values copied. The position of a buffer is the index of the next element of the buffer that will be read or written.
- The
limitof the buffer is set equal to itscapacity. Thelimitof a buffer is its first index that should not be read or written. Thecapacityof a buffer is the number of elements a buffer contains. - If a
markis defined, it is discarded. Marking a position means recording a position that can be restored by theByteBuffer.reset()method. This marked position is discarded by theByteBuffer.compact()method.
Declaration
The ByteBuffer.compact() method is declared as follows:
buff.compact()
buff: TheByteBufferthat will be compacted.
Return value
The ByteBuffer.compact() method returns a compacted copy of buff.
Note: If the buffer is backed by an array but the array is read-only,
ReadOnlyBufferExceptionis thrown.
Examples
Example 1
Consider the code snippet below, which demonstrates the use of the ByteBuffer.compact() method.
import java.nio.*;import java.util.*;public class main {public static void main(String[] args) {int n1 = 6;try {ByteBuffer buff1 = ByteBuffer.allocate(n1);buff1.put((byte)1);buff1.put((byte)2);buff1.put((byte)3);buff1.put((byte)4);buff1.put((byte)5);buff1.put((byte)6);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
ByteBufferbuff1is 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 to2using theposition()method in line 15. - The limit of
buff1is set to5using thelimit()method in line 16. - The
ByteBuffer.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 ByteBuffer.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 {ByteBuffer buff1 = ByteBuffer.allocate(n1);buff1.put((byte)1);buff1.put((byte)2);buff1.put((byte)3);buff1.put((byte)4);buff1.put((byte)5);buff1.put((byte)6);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());ByteBuffer 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
ByteBufferbuff1is 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 to2using theposition()method in line 15. - The limit of
buff1is set to5using thelimit()method in line 16. - A
ByteBufferbuff2is declared in line 22 that is the read-only copy ofbuff1. - The
ByteBuffer.compact()method is used in line 24 to compactbuff2.ReadOnlyBufferExceptionis thrown becausebuff2is read-only.
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved