Open In App

ShortBuffer compact() method in Java With Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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 ShortBuffer
        int capacity = 10;
 
        // Creating the ShortBuffer
 
        // creating object of shortbuffer
        // and allocating size capacity
        ShortBuffer sb = ShortBuffer.allocate(capacity);
 
        // putting the value in shortbuffer
        sb.put((short)856);
        sb.put((short)961);
        sb.put((short)54);
 
        // print the ShortBuffer
        System.out.println("Original ShortBuffer: "
                           + Arrays.toString(sb.array()));
 
        System.out.println("Position: " + sb.position());
 
        System.out.println("limit: " + sb.limit());
 
        // Creating a compacted ShortBuffer of same ShortBuffer
        // using compact() method
        ShortBuffer shortBuffer = sb.compact();
 
        // print the ShortBuffer
        System.out.println("\nCompacted ShortBuffer: "
                           + Arrays.toString(shortBuffer.array()));
 
        System.out.println("Position: " + shortBuffer.position());
 
        System.out.println("limit: " + shortBuffer.limit());
 
        // putting the value in compacted shortbuffer
        shortBuffer.put((short)961);
 
        // print the ShortBuffer
        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




// 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 ShortBuffer
        int capacity = 10;
 
        // Creating the ShortBuffer
        try {
 
            // creating object of ShortBuffer
            // and allocating size capacity
            ShortBuffer sb = ShortBuffer.allocate(capacity);
 
            // putting the value in floatbuffer
            sb.put((short)856);
            sb.put((short)961);
            sb.put((short)6010);
            sb.rewind();
 
            // Creating a read-only copy of ShortBuffer
            // using asReadOnlyBuffer() method
            ShortBuffer sb1 = sb.asReadOnlyBuffer();
 
            // print the ReadOnlyBuffer
            System.out.print("ReadOnlyBuffer ShortBuffer: ");
            while (sb1.hasRemaining())
                System.out.print(sb1.get() + ", ");
            System.out.println("");
 
            // print the Position of ShortBuffer sb
            System.out.println("\nPosition: " + sb.position());
 
            // print the Limit of ShortBuffer sb
            System.out.println("\nlimit: " + sb.limit());
 
            // Creating a compacted ShortBuffer of same ReadOnlyBuffer
            // using compact() method
            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
Previous
Next
Share your thoughts in the comments
Similar Reads