ShortBuffer compact() method in Java With Examples
The compact() method of java.nio.ShortBuffer Class is used to compact the given buffer. The shorts between the buffer’s current position and its limit, if any, are copied to the beginning of the buffer. That is, the short at index p = position() is copied to index zero, the short at index p + 1 is copied to index one, and so forth until the short 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 shorts copied, rather than to zero, so that an invocation of this method can be followed immediately by an invocation of another relative put method. Syntax:
public abstract ShortBuffer compact()
Return Value: This method returns the new ShortBuffer 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: Program 1:
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
ShortBuffer sb = ShortBuffer.allocate(capacity);
sb.put(( short ) 856 );
sb.put(( short ) 961 );
sb.put(( short ) 54 );
System.out.println("Original ShortBuffer: "
+ Arrays.toString(sb.array()));
System.out.println("Position: " + sb.position());
System.out.println("limit: " + sb.limit());
ShortBuffer shortBuffer = sb.compact();
System.out.println("\nCompacted ShortBuffer: "
+ Arrays.toString(shortBuffer.array()));
System.out.println("Position: " + shortBuffer.position());
System.out.println("limit: " + shortBuffer.limit());
shortBuffer.put(( short ) 961 );
System.out.println("\nUpdated Compacted ShortBuffer: "
+ Arrays.toString(shortBuffer.array()));
System.out.println("Position: " + shortBuffer.position());
System.out.println("limit: " + shortBuffer.limit());
}
}
|
Output:
Original ShortBuffer: [856, 961, 54, 0, 0, 0, 0, 0, 0, 0]
Position: 3
limit: 10
Compacted ShortBuffer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Position: 7
limit: 10
Updated Compacted ShortBuffer: [0, 0, 0, 0, 0, 0, 0, 961, 0, 0]
Position: 8
limit: 10
Program 2: To demonstrate ReadOnlyBufferException.
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 10 ;
try {
ShortBuffer sb = ShortBuffer.allocate(capacity);
sb.put(( short ) 856 );
sb.put(( short ) 961 );
sb.put(( short ) 6010 );
sb.rewind();
ShortBuffer sb1 = sb.asReadOnlyBuffer();
System.out.print("ReadOnlyBuffer ShortBuffer: ");
while (sb1.hasRemaining())
System.out.print(sb1.get() + ", ");
System.out.println("");
System.out.println("\nPosition: " + sb.position());
System.out.println("\nlimit: " + sb.limit());
System.out.println("\nTrying to compact the ReadOnlyBuffer sb1");
ShortBuffer floatBuffer = sb1.compact();
}
catch (IllegalArgumentException e) {
System.out.println("Exception throws " + e);
}
catch (ReadOnlyBufferException e) {
System.out.println("Exception throws " + e);
}
}
}
|
Output:
ReadOnlyBuffer ShortBuffer: 856, 961, 6010, 0, 0, 0, 0, 0, 0, 0,
Position: 0
limit: 10
Trying to compact the ReadOnlyBuffer sb1
Exception throws java.nio.ReadOnlyBufferException
Last Updated :
13 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...