Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is the ByteBuffer compact() method in Java?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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 position and limit of 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 limit of the buffer is set equal to its capacity. The limit of a buffer is its first index that should not be read or written. The capacity of a buffer is the number of elements a buffer contains.
  • If a mark is defined, it is discarded. Marking a position means recording a position that can be restored by the ByteBuffer.reset() method. This marked position is discarded by the ByteBuffer.compact() method.

Declaration

The ByteBuffer.compact() method is declared as follows:

buff.compact()
  • buff: The ByteBuffer that 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, ReadOnlyBufferException is 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 ByteBuffer buff1 is declared in line 7 with the capacity n1 = 6.
  • Six elements are added to buff1 using the put() method in lines 8-13.
  • The position of buff1 is set to 2 using the position() method in line 15.
  • The limit of buff1 is set to 5 using the limit() method in line 16.
  • The ByteBuffer.compact() method is used in line 22 to compact buff1. The elements between the position and limit of buff1 are copied to its beginning. The position of buff1 is set equal to the number of elements copied. The limit of buff1 is set equal to its capacity.

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 ByteBuffer buff1 is declared in line 7 with the capacity n1 = 6.
  • Six elements are added to buff1 using the put() method in lines 8-13.
  • The position of buff1 is set to 2 using the position() method in line 15.
  • The limit of buff1 is set to 5 using the limit() method in line 16.
  • A ByteBuffer buff2 is declared in line 22 that is the read-only copy of buff1.
  • The ByteBuffer.compact() method is used in line 24 to compact buff2. ReadOnlyBufferException is thrown because buff2 is read-only.

RELATED TAGS

java

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring