The java.nio
class represents New IO that exists as 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.DoubleBuffer
class extends the java.nio.Buffer
superclass. It defines the following essential operations that can be performed on DoubleBuffer
:
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.
DoubleBuffer
can be created using either the allocate()
method, which allocates a fixed amount of space for the buffer, or by wrapping an existing Double
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;double [] doubleArray = {1.0F, 2.5F};DoubleBuffer buff1 = DoubleBuffer.allocate(CAPACITY);DoubleBuffer buff2 = DoubleBuffer.wrap(doubleArray);buff1.put(1, 99.0F);System.out.println(Arrays.toString(buff1.array()));System.out.println(Arrays.toString(buff2.array()));}}
The code above shows an example of creating a DoubleBuffer
object. We create two buffers, buff1
and buff2
. buff1
is created using the allocate()
method, and buff2
is made by wrapping an already existing double array called doubleArray
into a buffer using the wrap()
method.
The .array()
method returns the double 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 DoubleBuffer. |
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.