java.nio.Buffer Class in Java
The Buffer class provides a buffer or a container for data chunks of specific primitive types. A finite sequence of elements is stored linearly in a buffer.
Important properties of a buffer that make it convenient to perform read and write operations in the data are:
- Capacity: This property determines the maximum number of elements that can be there in a buffer.
- Limit: This property determines the limit of the data that can be read or write by providing the index of the elements.
- Position: This property determines the location of the current element in the buffer.
Syntax: Class declaration
public abstract class Buffer extends Object
Buffer class provides a subclass for each of the following buffer data types such as ByteBuffer, MappedByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer. Buffer class inherits the following methods from class java.lang.Object such as clone(), finalize(), getClass(), hashCode(), notify(), notifyAll(), toString(), wait(). Now, moving on to the methods of Buffer class is as follows as shown alphabetically in the tabular format shown below:
Method |
Description |
array() |
This method returns the array that backs this buffer |
arrayOffset() |
This method returns the offset within this buffer’s backing array of the first element of the buffer |
capacity() |
This method returns this buffer’s capacity. |
clear() |
This method clears this buffer. |
flip() |
This method flips this buffer. |
hasArray() |
This method tells whether or not this buffer is backed by an accessible array. |
hasRemaining() |
This method tells whether there are any elements between the current position and the limit. |
isDirect() |
This method tells whether or not this buffer is direct. |
isReadOnly() |
This method tells whether or not this buffer is read-only. |
limit() |
This method returns this buffer’s limit. |
limit(int newLimit) |
This method sets this buffer’s limit. |
mark() |
This method sets this buffer’s mark at its position. |
position() |
This method returns this buffer’s position. |
position(int newPosition) |
This method sets this buffer’s position. |
remaining() |
This method returns the number of elements between the current position and the limit. |
reset() |
This method resets this buffer’s position to the previously marked position. |
rewind() |
This method rewinds this buffer. |
Implementation: Buffer class and its methods
Example 1
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int capacity = 5 ;
try {
ByteBuffer bufferObj
= ByteBuffer.allocate(capacity);
bufferObj.put(( byte ) 10 );
bufferObj.put(( byte ) 20 );
bufferObj.put(( byte ) 30 );
bufferObj.put(( byte ) 40 );
bufferObj.put(( byte ) 50 );
Buffer bufferObj1 = (Buffer)bufferObj;
byte [] arr = ( byte [])bufferObj1.array();
System.out.print( " The array is : [" );
for ( int i = 0 ; i < arr.length; i++)
System.out.print( " " + arr[i]);
System.out.print( " ]" );
}
catch (ReadOnlyBufferException e) {
System.out.println( "Exception throws: " + e);
}
}
}
|
Output
The array is : [ 10 20 30 40 50 ]
Example 2
Java
import java.nio.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
try {
byte [] barr = { 10 , 20 , 30 , 40 , 50 };
ByteBuffer bufferObj = ByteBuffer.wrap(barr);
Buffer bufferObj1 = (Buffer)bufferObj;
bufferObj1.position( 2 );
bufferObj1.mark();
bufferObj1.position( 5 );
System.out.println( "position before reset: "
+ bufferObj1.position());
bufferObj1.clear();
System.out.println( "position after reset: "
+ bufferObj1.position());
}
catch (InvalidMarkException e) {
System.out.println(
"new position is less than "
+ "the position we marked before " );
System.out.println( "Exception throws: " + e);
}
}
}
|
Output
position before reset: 5
position after reset: 0
Last Updated :
03 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...