By Ron Hitchens
Book Price: $34.95 USD
£24.95 GBP
PDF Price: $27.99
Cover | Table of Contents | Colophon
|
Process time (ms)
|
I/O time (ms)
|
Throughput (units/sec)
|
Gain (%)
|
|---|---|---|---|
|
5
|
100
|
9.52
|
(benchmark)
|
|
2.5
|
100
|
9.76
|
2.44
|
|
1
|
java.io spend a lot
of time breaking down into little pieces, often copying each piece
between several layers of objects. The operating
system wants to deliver data by the truckload. The
java.io classes want to process data by the
shovelful. NIO makes it easier to back the truck
right up to where you can make direct use of the data (a
ByteBuffer object).
java.nio package provides new
abstractions to address this problem. The
Channel and Selector
classes in particular provide generic java.nio
packages with the Buffer classes. These classes
are the foundation upon which java.nio is built.
In this chapter, we'll take a close look at buffers,
discover the various types, and learn how to use them.
We'll then see how the java.nio
buffers relate to the channel classes of
java.nio.channels.
mark = position. Calling
reset(
) sets position =
mark. The mark is undefined until set.
0 <= mark <= position <= limit <= capacity
ByteBuffer with a capacity of 10.
public abstract class CharBuffer
extends Buffer implements CharSequence, Comparable
{
// This is a partial API listing
public static CharBuffer allocate (int capacity)
public static CharBuffer wrap (char [] array)
public static CharBuffer wrap (char [] array, int offset, int length)
public final boolean hasArray( )
public final char [] array( )
public final int arrayOffset( )
}
capacity data elements. Wrapping creates a buffer
object but does not allocate any space to hold the data elements. It
uses the
array
you provide as backing storage to hold the data elements of the
buffer.
chars:
CharBuffer charBuffer = CharBuffer.allocate (100);
char array from the
heap to act as backing store for the 100 chars.
char [] myArray = new char [100]; CharBuffer charbuffer = CharBuffer.wrap (myArray);
public abstract class CharBuffer
extends Buffer implements CharSequence, Comparable
{
// This is a partial API listing
public abstract CharBuffer duplicate( );
public abstract CharBuffer asReadOnlyBuffer( );
public abstract CharBuffer slice( );
}
ByteBuffer.
Some of these methods have been discussed in previous sections and
are simply type-specific versions. The new methods will be covered in
this and following sections.
package java.nio;
public abstract class ByteBuffer extends Buffer
implements Comparable
{
public static ByteBuffer allocate (int capacity)
public static ByteBuffer allocateDirect (int capacity)
public abstract boolean isDirect( );
public static ByteBuffer wrap (byte[] array, int offset, int length)
public static ByteBuffer wrap (byte[] array)
public abstract ByteBuffer duplicate( );
public abstract ByteBuffer asReadOnlyBuffer( );
public abstract ByteBuffer slice( );
public final boolean hasArray( )
public final byte [] array( )
public final int arrayOffset( )
public abstract byte get( );
public abstract byte get (int index);
public ByteBuffer get (byte[] dst, int offset, int length)
public ByteBuffer get (byte[] dst, int offset, int length)
public abstract ByteBuffer put (byte b);
public abstract ByteBuffer put (int index, byte b);
public ByteBuffer put (ByteBuffer src)
public ByteBuffer put (byte[] src, int offset, int length)
public final ByteBuffer put (byte[] src)
public final ByteOrder order( )
public final ByteBuffer order (ByteOrder bo)
public abstract CharBuffer asCharBuffer( );
public abstract ShortBuffer asShortBuffer( );
public abstract IntBuffer asIntBuffer( );
public abstract LongBuffer asLongBuffer( );
public abstract FloatBuffer asFloatBuffer( );
public abstract DoubleBuffer asDoubleBuffer( );
public abstract char getChar( );
public abstract char getChar (int index);
public abstract ByteBuffer putChar (char value);
public abstract ByteBuffer putChar (int index, char value);
public abstract short getShort( );
public abstract short getShort (int index);
public abstract ByteBuffer putShort (short value);
public abstract ByteBuffer putShort (int index, short value);
public abstract int getInt( );
public abstract int getInt (int index);
public abstract ByteBuffer putInt (int value);
public abstract ByteBuffer putInt (int index, int value);
public abstract long getLong( );
public abstract long getLong (int index);
public abstract ByteBuffer putLong (long value);
public abstract ByteBuffer putLong (int index, long value);
public abstract float getFloat( );
public abstract float getFloat (int index);
public abstract ByteBuffer putFloat (float value);
public abstract ByteBuffer putFloat (int index, float value);
public abstract double getDouble( );
public abstract double getDouble (int index);
public abstract ByteBuffer putDouble (double value);
public abstract ByteBuffer putDouble (int index, double value);
public abstract ByteBuffer compact( );
public boolean equals (Object ob) {
public int compareTo (Object ob) {
public String toString( )
public int hashCode( )
} java.nio package. Buffer objects enable the
advanced I/O capabilities covered in the remaining
chapters. These key buffer topics were covered in this chapter:
java.nio. The next stop on the tour is
java.nio.channels where you will encounter, not
surprisingly, channels. Channels interact with byte buffers and
unlock the door to high-performance I/O. Hop back
on the bus, it's a short trip to our next stop.
java.nio. They are not an extension or
enhancement, but a new, first-class Java I/O
paradigm. They provide direct connections to I/O
services. A Channel is a conduit that transports
data efficiently between byte buffers and the entity on the other end
of the channel (usually a file or socket).
package java.nio.channels;
public interface Channel
{
public boolean isOpen( );
public void close( ) throws IOException;
}
java.nio.channels.spi.
These classes,
AbstractInterruptibleChannel
and
public interface ScatteringByteChannel
extends ReadableByteChannel
{
public long read (ByteBuffer [] dsts)
throws IOException;
public long read (ByteBuffer [] dsts, int offset, int length)
throws IOException;
}
public interface GatheringByteChannel
extends WritableByteChannel
{
public long write(ByteBuffer[] srcs)
throws IOException;
public long write(ByteBuffer[] srcs, int offset, int length)
throws IOException;
}
java.nio.channels.spi package.
This means that it's possible to perform readiness
selection of socket channels using a Selector
object. Selection and multiplexed I/O are
discussed in Chapter 4.
java.net are reused
for most protocol operations.
java.nio.channels
package includes a class named Pipe. A
pipe, in the general sense, is a conduit
through which data can be passed in a single direction between two
entities. The notion of a pipe has long been familiar to users of
Unix (and Unix-like) operating systems. Pipes
are used on Unix systems to connect the output of one process to the
input of another. The Pipe class implements a
pipe paradigm, but the pipes it creates are intraprocess (within the
JVM process) rather than interprocess (between
processes). See Figure 3-10.
Pipe.
package java.nio.channels;
public abstract class Pipe
{
public static Pipe open( ) throws IOException
public abstract SourceChannel source( );
public abstract SinkChannel sink( );
public static abstract class SourceChannel
extends AbstractSelectableChannel
implements ReadableByteChannel, ScatteringByteChannel
public static abstract class SinkChannel
extends AbstractSelectableChannel
implements WritableByteChannel, GatheringByteChannel
}
java.io classes (an
implementation detail), but the APIs presented by
java.io streams and reader/writers will not be
going away anytime soon (nor should they).
java.nio.channels.Channels, defines several static
factory methods to make it easier for channels to interconnect with
streams and readers/writers. Table 3-2 summarizes
these methods.
|
Method
|
Returns
|
Description
|
|---|---|---|
|
newChannel (InputStream
in)
|
ReadableByteChannel
|
Returns a channel that will read bytes from the provided input stream.
|
|
newChannel (OutputStream out)
|
WritableByteChannel
|
Returns a channel that will write bytes to the provided output stream.
|
|
newInputStream (ReadableByteChannel
ch)
|
InputStream
|
package java.nio.channels;
public abstract class SelectionKey
{
public static final int OP_READ
public static final int OP_WRITE
public static final int OP_CONNECT
public static final int OP_ACCEPT
public abstract SelectableChannel channel( );
public abstract Selector selector( );
public abstract void cancel( );
public abstract boolean isValid( );
public abstract int interestOps( );
public abstract void interestOps (int ops);
public abstract int readyOps( );
public final boolean isReadable( )
public final boolean isWritable( )
public final boolean isConnectable( )
public final boolean isAcceptable( )
public final Object attach (Object ob)
public final Object attachment( )
}