The java.nio
class represents New IO, an alternative to the regular IO API in Java. It allows you to do non-blocking IO using channels and buffers.
A channel is like a regular stream, but unlike a stream, a single channel can be used to read and write data, whereas a stream is usually only one-way. Channels can be written to asynchronously. Channels usually write to and read from buffers.
A buffer is a block of memory where data can be written and can be read later on. It is a finite sequence of elements of any particular primitive type.
Note: Even though
java.nio
supports non-blocking IO, it is NOT an asynchronous class because some operations are actually blocking, e.g., file IO.
Buffers
have three essential properties:
The java.nio.IntBuffer
class extends the java.nio.Buffer
superclass. It defines the following essential operations that can be performed on IntBuffer
.
Absolute and relative put
and get
methods for reading and writing data from the buffer as a single Double
element.
Absolute and relative bulk put
and get
methods for reading and writing data from the buffer or a Double array
as an array.
Methods for slicing, compacting, and duplicating the buffer.
IntBuffer
IntBuffer
can be created using either the allocate()
method, which allocates a fixed amount of space for the buffer, or by wrapping an existing Int
array into a buffer using the wrap()
method or by creating a view
buffer.
import java.nio.*;import java.util.*;class HelloWorld {public static void main( String args[] ) {int CAPACITY = 10;int [] intArray = {1, 2, 3};IntBuffer buff1 = IntBuffer.allocate(CAPACITY);IntBuffer buff2 = IntBuffer.wrap(intArray);buff1.put(1, 99);System.out.println(Arrays.toString(buff1.array()));System.out.println(Arrays.toString(buff2.array()));}}
The code above shows an example of creating an IntBuffer
object. We create two buffers, buff1
and buff2
. buff1
is created using the allocate()
method, and buff2
is made by wrapping an already existing Int
array called intArray
into a buffer using the wrap()
method.
The .array()
method returns the Int
array that backs up the buffer and then we use the .toString()
method to print it out to the standard output.
Method | Description |
allocate(int capacity) | Allocates a new float buffer. Returns static IntBuffer. |
array() | Returns the float array that backs this buffer (optional operation). |
arrayOffset() | Returns the offset as an int within this buffer's backing array of the first element of the buffer (optional operation). |
asReadOnlyBuffer() | Creates a new, read-only float buffer that shares this buffer's content. |
compact() | Compacts this buffer (optional operation). |
compareTo(FloatBuffer that) | Compares this buffer to another. |
duplicate() | Creates a new float buffer that shares this buffer's content. |
equals(Object ob) | Tells whether or not this buffer is equal to another object. Returns boolean. |
get() | Relative get method. Returns float. |
get(float[] dst) | Relative bulk get method. |
get(float[] dst, int offset, int length) | Relative bulk get method. |
put(float f) | Relative put method (optional operation). |
put(float[] src) | Relative bulk put method (optional operation). |
put(int index, float f) | Absolute put method (optional operation). |
A complete list of the class methods can be found here.
Free Resources