Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Channels

public final class Channels
extends Object

java.lang.Object
   ↳ java.nio.channels.Channels


This class provides several utilities to get I/O streams from channels.

Summary

Public methods

static WritableByteChannel newChannel(OutputStream outputStream)

Returns a writable channel on the given output stream.

static ReadableByteChannel newChannel(InputStream inputStream)

Returns a readable channel on the given input stream.

static InputStream newInputStream(ReadableByteChannel channel)

Returns an input stream on the given channel.

static OutputStream newOutputStream(WritableByteChannel channel)

Returns an output stream on the given channel.

static Reader newReader(ReadableByteChannel channel, CharsetDecoder decoder, int minBufferCapacity)

Returns a reader that decodes bytes from a channel.

static Reader newReader(ReadableByteChannel channel, String charsetName)

Returns a reader that decodes bytes from a channel.

static Writer newWriter(WritableByteChannel channel, String charsetName)

Returns a writer that encodes characters with the specified encoder and sends the bytes to the specified channel.

static Writer newWriter(WritableByteChannel channel, CharsetEncoder encoder, int minBufferCapacity)

Returns a writer that encodes characters with the specified encoder and sends the bytes to the specified channel.

Inherited methods

From class java.lang.Object

Public methods

newChannel

Added in API level 1
WritableByteChannel newChannel (OutputStream outputStream)

Returns a writable channel on the given output stream. The resulting channel has following properties:

  • If the channel is closed, then the underlying stream is closed as well.
  • It is not buffered.

Parameters
outputStream OutputStream: the stream to be wrapped by a byte channel.
Returns
WritableByteChannel a byte channel that writes bytes to the output stream.

newChannel

Added in API level 1
ReadableByteChannel newChannel (InputStream inputStream)

Returns a readable channel on the given input stream. The resulting channel has the following properties:

  • If the channel is closed, then the underlying stream is closed as well.
  • It is not buffered.

Parameters
inputStream InputStream: the stream to be wrapped by a byte channel.
Returns
ReadableByteChannel a byte channel that reads bytes from the input stream.

newInputStream

Added in API level 1
InputStream newInputStream (ReadableByteChannel channel)

Returns an input stream on the given channel. The resulting stream has the following properties:

  • If the stream is closed, then the underlying channel is closed as well.
  • It is thread safe.
  • It throws an IllegalBlockingModeException if the channel is in non-blocking mode and read is called.
  • Neither mark nor reset is supported.
  • It is not buffered.

Parameters
channel ReadableByteChannel: the channel to be wrapped by an InputStream.
Returns
InputStream an InputStream that takes bytes from the given byte channel.

newOutputStream

Added in API level 1
OutputStream newOutputStream (WritableByteChannel channel)

Returns an output stream on the given channel. The resulting stream has the following properties:

  • If the stream is closed, then the underlying channel is closed as well.
  • It is thread safe.
  • It throws an IllegalBlockingModeException if the channel is in non-blocking mode and write is called.
  • It is not buffered.

Parameters
channel WritableByteChannel: the channel to be wrapped by an OutputStream.
Returns
OutputStream an OutputStream that puts bytes onto the given byte channel.

newReader

Added in API level 1
Reader newReader (ReadableByteChannel channel, 
                CharsetDecoder decoder, 
                int minBufferCapacity)

Returns a reader that decodes bytes from a channel.

Parameters
channel ReadableByteChannel: the Channel to be read.
decoder CharsetDecoder: the Charset decoder to be used.
minBufferCapacity int: The minimum size of the byte buffer, -1 means to use the default size.
Returns
Reader the reader.

newReader

Added in API level 1
Reader newReader (ReadableByteChannel channel, 
                String charsetName)

Returns a reader that decodes bytes from a channel. This method creates a reader with a buffer of default size.

Parameters
channel ReadableByteChannel: the Channel to be read.
charsetName String: the name of the charset.
Returns
Reader the reader.
Throws
UnsupportedCharsetException if the given charset name is not supported.

newWriter

Added in API level 1
Writer newWriter (WritableByteChannel channel, 
                String charsetName)

Returns a writer that encodes characters with the specified encoder and sends the bytes to the specified channel. This method creates a writer with a buffer of default size.

Parameters
channel WritableByteChannel: the Channel to be written to.
charsetName String: the name of the charset.
Returns
Writer the writer.
Throws
UnsupportedCharsetException if the given charset name is not supported.

newWriter

Added in API level 1
Writer newWriter (WritableByteChannel channel, 
                CharsetEncoder encoder, 
                int minBufferCapacity)

Returns a writer that encodes characters with the specified encoder and sends the bytes to the specified channel.

Parameters
channel WritableByteChannel: the Channel to write to.
encoder CharsetEncoder: the CharsetEncoder to be used.
minBufferCapacity int: the minimum size of the byte buffer, -1 means to use the default size.
Returns
Writer the writer.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.