java.nio.channels
Class Channels

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.nio.channels.Channels

public final class Channels
extends Object sample code for java.lang.Object definition code for java.lang.Object

Utility methods for channels and streams.

This class defines static methods that support the interoperation of the stream classes of the java.io package with the channel classes of this package.

Since:
1.4

Method Summary
static ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel newChannel sample code for java.nio.channels.Channels.newChannel(java.io.InputStream) definition code for java.nio.channels.Channels.newChannel(java.io.InputStream) (InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in)
          Constructs a channel that reads bytes from the given stream.
static WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel newChannel sample code for java.nio.channels.Channels.newChannel(java.io.OutputStream) definition code for java.nio.channels.Channels.newChannel(java.io.OutputStream) (OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
          Constructs a channel that writes bytes to the given stream.
static InputStream sample code for java.io.InputStream definition code for java.io.InputStream newInputStream sample code for java.nio.channels.Channels.newInputStream(java.nio.channels.ReadableByteChannel) definition code for java.nio.channels.Channels.newInputStream(java.nio.channels.ReadableByteChannel) (ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  ch)
          Constructs a stream that reads bytes from the given channel.
static OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream newOutputStream sample code for java.nio.channels.Channels.newOutputStream(java.nio.channels.WritableByteChannel) definition code for java.nio.channels.Channels.newOutputStream(java.nio.channels.WritableByteChannel) (WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  ch)
          Constructs a stream that writes bytes to the given channel.
static Reader sample code for java.io.Reader definition code for java.io.Reader newReader sample code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.nio.charset.CharsetDecoder, int) definition code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.nio.charset.CharsetDecoder, int) (ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  ch, CharsetDecoder sample code for java.nio.charset.CharsetDecoder definition code for java.nio.charset.CharsetDecoder  dec, int minBufferCap)
          Constructs a reader that decodes bytes from the given channel using the given decoder.
static Reader sample code for java.io.Reader definition code for java.io.Reader newReader sample code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.lang.String) definition code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.lang.String) (ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  ch, String sample code for java.lang.String definition code for java.lang.String  csName)
          Constructs a reader that decodes bytes from the given channel according to the named charset.
static Writer sample code for java.io.Writer definition code for java.io.Writer newWriter sample code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.nio.charset.CharsetEncoder, int) definition code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.nio.charset.CharsetEncoder, int) (WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  ch, CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  enc, int minBufferCap)
          Constructs a writer that encodes characters using the given encoder and writes the resulting bytes to the given channel.
static Writer sample code for java.io.Writer definition code for java.io.Writer newWriter sample code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.lang.String) definition code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.lang.String) (WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  ch, String sample code for java.lang.String definition code for java.lang.String  csName)
          Constructs a writer that encodes characters according to the named charset and writes the resulting bytes to the given channel.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Method Detail

newInputStream sample code for java.nio.channels.Channels.newInputStream(java.nio.channels.ReadableByteChannel) definition code for java.nio.channels.Channels.newInputStream(java.nio.channels.ReadableByteChannel)

public static InputStream sample code for java.io.InputStream definition code for java.io.InputStream  newInputStream(ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  ch)
Constructs a stream that reads bytes from the given channel.

The read methods of the resulting stream will throw an IllegalBlockingModeException sample code for java.nio.channels.IllegalBlockingModeException definition code for java.nio.channels.IllegalBlockingModeException if invoked while the underlying channel is in non-blocking mode. The stream will not be buffered, and it will not support the mark sample code for java.io.InputStream.mark(int) definition code for java.io.InputStream.mark(int) or reset sample code for java.io.InputStream.reset() definition code for java.io.InputStream.reset() methods. The stream will be safe for access by multiple concurrent threads. Closing the stream will in turn cause the channel to be closed.

Parameters:
ch - The channel from which bytes will be read
Returns:
A new input stream

newOutputStream sample code for java.nio.channels.Channels.newOutputStream(java.nio.channels.WritableByteChannel) definition code for java.nio.channels.Channels.newOutputStream(java.nio.channels.WritableByteChannel)

public static OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  newOutputStream(WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  ch)
Constructs a stream that writes bytes to the given channel.

The write methods of the resulting stream will throw an IllegalBlockingModeException sample code for java.nio.channels.IllegalBlockingModeException definition code for java.nio.channels.IllegalBlockingModeException if invoked while the underlying channel is in non-blocking mode. The stream will not be buffered. The stream will be safe for access by multiple concurrent threads. Closing the stream will in turn cause the channel to be closed.

Parameters:
ch - The channel to which bytes will be written
Returns:
A new output stream

newChannel sample code for java.nio.channels.Channels.newChannel(java.io.InputStream) definition code for java.nio.channels.Channels.newChannel(java.io.InputStream)

public static ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  newChannel(InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in)
Constructs a channel that reads bytes from the given stream.

The resulting channel will not be buffered; it will simply redirect its I/O operations to the given stream. Closing the channel will in turn cause the stream to be closed.

Parameters:
in - The stream from which bytes are to be read
Returns:
A new readable byte channel

newChannel sample code for java.nio.channels.Channels.newChannel(java.io.OutputStream) definition code for java.nio.channels.Channels.newChannel(java.io.OutputStream)

public static WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  newChannel(OutputStream sample code for java.io.OutputStream definition code for java.io.OutputStream  out)
Constructs a channel that writes bytes to the given stream.

The resulting channel will not be buffered; it will simply redirect its I/O operations to the given stream. Closing the channel will in turn cause the stream to be closed.

Parameters:
out - The stream to which bytes are to be written
Returns:
A new writable byte channel

newReader sample code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.nio.charset.CharsetDecoder, int) definition code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.nio.charset.CharsetDecoder, int)

public static Reader sample code for java.io.Reader definition code for java.io.Reader  newReader(ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  ch,
                               CharsetDecoder sample code for java.nio.charset.CharsetDecoder definition code for java.nio.charset.CharsetDecoder  dec,
                               int minBufferCap)
Constructs a reader that decodes bytes from the given channel using the given decoder.

The resulting stream will contain an internal input buffer of at least minBufferCap bytes. The stream's read methods will, as needed, fill the buffer by reading bytes from the underlying channel; if the channel is in non-blocking mode when bytes are to be read then an IllegalBlockingModeException sample code for java.nio.channels.IllegalBlockingModeException definition code for java.nio.channels.IllegalBlockingModeException will be thrown. The resulting stream will not otherwise be buffered, and it will not support the mark sample code for java.io.Reader.mark(int) definition code for java.io.Reader.mark(int) or reset sample code for java.io.Reader.reset() definition code for java.io.Reader.reset() methods. Closing the stream will in turn cause the channel to be closed.

Parameters:
ch - The channel from which bytes will be read
dec - The charset decoder to be used
minBufferCap - The minimum capacity of the internal byte buffer, or -1 if an implementation-dependent default capacity is to be used
Returns:
A new reader

newReader sample code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.lang.String) definition code for java.nio.channels.Channels.newReader(java.nio.channels.ReadableByteChannel, java.lang.String)

public static Reader sample code for java.io.Reader definition code for java.io.Reader  newReader(ReadableByteChannel sample code for java.nio.channels.ReadableByteChannel definition code for java.nio.channels.ReadableByteChannel  ch,
                               String sample code for java.lang.String definition code for java.lang.String  csName)
Constructs a reader that decodes bytes from the given channel according to the named charset.

An invocation of this method of the form

 Channels.newReader(ch, csname)
behaves in exactly the same way as the expression
 Channels.newReader(ch,
                    Charset.forName(csName)
                        .newDecoder(),
                    -1);

Parameters:
ch - The channel from which bytes will be read
csName - The name of the charset to be used
Returns:
A new reader
Throws:
UnsupportedCharsetException sample code for java.nio.charset.UnsupportedCharsetException definition code for java.nio.charset.UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine

newWriter sample code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.nio.charset.CharsetEncoder, int) definition code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.nio.charset.CharsetEncoder, int)

public static Writer sample code for java.io.Writer definition code for java.io.Writer  newWriter(WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  ch,
                               CharsetEncoder sample code for java.nio.charset.CharsetEncoder definition code for java.nio.charset.CharsetEncoder  enc,
                               int minBufferCap)
Constructs a writer that encodes characters using the given encoder and writes the resulting bytes to the given channel.

The resulting stream will contain an internal output buffer of at least minBufferCap bytes. The stream's write methods will, as needed, flush the buffer by writing bytes to the underlying channel; if the channel is in non-blocking mode when bytes are to be written then an IllegalBlockingModeException sample code for java.nio.channels.IllegalBlockingModeException definition code for java.nio.channels.IllegalBlockingModeException will be thrown. The resulting stream will not otherwise be buffered. Closing the stream will in turn cause the channel to be closed.

Parameters:
ch - The channel to which bytes will be written
enc - The charset encoder to be used
minBufferCap - The minimum capacity of the internal byte buffer, or -1 if an implementation-dependent default capacity is to be used
Returns:
A new writer

newWriter sample code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.lang.String) definition code for java.nio.channels.Channels.newWriter(java.nio.channels.WritableByteChannel, java.lang.String)

public static Writer sample code for java.io.Writer definition code for java.io.Writer  newWriter(WritableByteChannel sample code for java.nio.channels.WritableByteChannel definition code for java.nio.channels.WritableByteChannel  ch,
                               String sample code for java.lang.String definition code for java.lang.String  csName)
Constructs a writer that encodes characters according to the named charset and writes the resulting bytes to the given channel.

An invocation of this method of the form

 Channels.newWriter(ch, csname)
behaves in exactly the same way as the expression
 Channels.newWriter(ch,
                    Charset.forName(csName)
                        .newEncoder(),
                    -1);

Parameters:
ch - The channel to which bytes will be written
csName - The name of the charset to be used
Returns:
A new writer
Throws:
UnsupportedCharsetException sample code for java.nio.charset.UnsupportedCharsetException definition code for java.nio.charset.UnsupportedCharsetException - If no support for the named charset is available in this instance of the Java virtual machine