ByteBuffer compact() method in Java with Examples
Last Updated :
06 Jun, 2021
The compact() method of java.nio.ByteBuffer class is used to compact the given buffer.
The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position() is copied to index zero, the byte at index p + 1 is copied to index one, and so forth until the byte at index limit() - 1 is copied to index n = limit() - 1 - p. The buffer's position is then set to n+1 and its limit is set to its capacity. The mark, if defined, is discarded.
The buffer's position is set to the number of bytes copied, rather than to zero, so that an invocation of this method can be followed immediately by an invocation of another relative put method.
Invoke this method after writing data from a buffer in case the write was incomplete.
Syntax :
public abstract ByteBuffer compact()
Return Value: This method returns the new ByteBuffer with the same content as that of this buffer.
Exception: This method throws the ReadOnlyBufferException, If this buffer is read-only.
Below program illustrates the compact() method:
Examples 1:
Java
// Java program to demonstrate
// compact() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 7;
// Creating the ByteBuffer
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int to byte typecast value in ByteBuffer
bb.put((byte)20);
bb.put((byte)30);
bb.put((byte)40);
// print the ByteBuffer
System.out.println("Original ByteBuffer: "
+ Arrays.toString(bb.array()));
System.out.println("Position: " + bb.position());
System.out.println("limit: " + bb.limit());
// Creating a compacted ByteBuffer of same ByteBuffer
// using compact() method
ByteBuffer cbb = bb.compact();
// print the ByteBuffer
System.out.println("\nCompacted ByteBuffer: "
+ Arrays.toString(cbb.array()));
System.out.println("Position: " + cbb.position());
System.out.println("limit: " + cbb.limit());
// putting the int to byte typecast value in compacted ByteBuffer
cbb.put((byte)50);
// print the ByteBuffer
System.out.println("\nUpdated Compacted ByteBuffer: "
+ Arrays.toString(cbb.array()));
System.out.println("Position: " + cbb.position());
System.out.println("limit: " + cbb.limit());
}
}
Output: Original ByteBuffer: [20, 30, 40, 0, 0, 0, 0]
Position: 3
limit: 7
Compacted ByteBuffer: [0, 0, 0, 0, 0, 0, 0]
Position: 4
limit: 7
Updated Compacted ByteBuffer: [0, 0, 0, 0, 50, 0, 0]
Position: 5
limit: 7
Examples 2:
Java
// Java program to demonstrate
// compact() method
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Declaring the capacity of the ByteBuffer
int capacity = 5;
// Creating the ByteBuffer
try {
// creating object of ByteBuffer
// and allocating size capacity
ByteBuffer bb = ByteBuffer.allocate(capacity);
// putting the int to byte typecast value in ByteBuffer
bb.put((byte)20);
bb.put((byte)30);
bb.put((byte)40);
bb.rewind();
// Creating a read-only copy of ByteBuffer
// using asReadOnlyBuffer() method
ByteBuffer bb1 = bb.asReadOnlyBuffer();
// print the ReadOnlyBuffer
System.out.print("ReadOnlyBuffer ByteBuffer: ");
while (bb1.hasRemaining())
System.out.print(bb1.get() + ", ");
System.out.println("");
// print the Position of ByteBuffer bb
System.out.println("\nPosition: " + bb.position());
// print the Limit of ByteBuffer bb
System.out.println("\nlimit: " + bb.limit());
// Creating a compacted ByteBuffer of same ReadOnlyBuffer
// using compact() method
System.out.println("\nTrying to compact the ReadOnlyBuffer bb1");
ByteBuffer rbb = bb1.compact();
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws " + e);
}
}
}
Output: ReadOnlyBuffer ByteBuffer: 20, 30, 40, 0, 0,
Position: 0
limit: 5
Trying to compact the ReadOnlyBuffer bb1
Exception throws java.nio.ReadOnlyBufferException
Similar Reads
ByteBuffer compareTo() method in Java With Examples The compareTo() method of java.nio.ByteBuffer class is used to compare one buffer to another. Two byte buffers are compared by comparing their sequences of remaining elements lexicographically, without regard to the starting position of each sequence within its corresponding buffer. Pairs of byte el
4 min read
Buffer capacity() method in Java with Examples The capacity() method of java.nio.Buffer Class is used to return this buffer's capacity. Syntax: public final int capacity() Return Value: The capacity of this buffer Below are the examples to illustrate the capacity() method: Examples 1: Java // Java program to demonstrate // capacity() method impo
2 min read
ByteBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.ByteBuffer class is used to create a new byte buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit,
3 min read
ByteBuffer clear() methods in Java with Examples The clear() method of java.nio.ByteBuffer Class is used to clear this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded. Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example: buf.clear(); // Prepa
2 min read
ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow
6 min read
ByteBuffer wrap() method in Java with Examples wrap(byte[] array) The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array; that is, modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be a
4 min read
ByteBuffer wrap() methods in Java with Examples wrap(byte[] array) The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array, i.e., modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be arra
4 min read
ByteBuffer flip() methods in Java with Examples The flip() method of java.nio.ByteBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of
3 min read
ByteBuffer slice() method in Java with Examples The slice() method of java.nio.ByteBuffer Class is used to creates a new byte buffer whose content is a shared subsequence of the given buffer's content. The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, an
3 min read
ByteBuffer array() method in Java with Examples The array() method of java.nio.ByteBuffer class is used to return the byte array that backs the taken buffer.Modifications to this buffer's content will cause the returned array's content to be modified, and vice versa.Invoke the hasArray() method before invoking this method in order to ensure that
3 min read