On this page
Class Channels
public final class Channels extends Object
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
Modifier and Type | Method | Description |
---|---|---|
static ReadableByteChannel |
newChannel |
Constructs a channel that reads bytes from the given stream.
|
static WritableByteChannel |
newChannel |
Constructs a channel that writes bytes to the given stream.
|
static InputStream |
newInputStream |
Constructs a stream that reads bytes from the given channel.
|
static InputStream |
newInputStream |
Constructs a stream that reads bytes from the given channel.
|
static OutputStream |
newOutputStream |
Constructs a stream that writes bytes to the given channel.
|
static OutputStream |
newOutputStream |
Constructs a stream that writes bytes to the given channel.
|
static Reader |
newReader |
Constructs a reader that decodes bytes from the given channel according to the named charset.
|
static Reader |
newReader |
Constructs a reader that decodes bytes from the given channel according to the given charset.
|
static Reader |
newReader |
Constructs a reader that decodes bytes from the given channel using the given decoder.
|
static Writer |
newWriter |
Constructs a writer that encodes characters according to the named charset and writes the resulting bytes to the given channel.
|
static Writer |
newWriter |
Constructs a writer that encodes characters according to the given charset and writes the resulting bytes to the given channel.
|
static Writer |
newWriter |
Constructs a writer that encodes characters using the given encoder and writes the resulting bytes to the given channel.
|
Method Details
newInputStream
public static InputStream newInputStream(ReadableByteChannel ch)
The read
and transferTo
methods of the resulting stream will throw an IllegalBlockingModeException
if invoked while the underlying channel is in non-blocking mode. The transferTo
method will also throw an IllegalBlockingModeException
if invoked to transfer bytes to an output stream that writes to an underlying channel in non-blocking mode. The stream will not be buffered, and it will not support the mark
or 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
public static OutputStream newOutputStream(WritableByteChannel ch)
The write
methods of the resulting stream will throw an 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
newInputStream
public static InputStream newInputStream(AsynchronousByteChannel ch)
The stream will not be buffered, and it will not support the mark
or 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
- Since:
- 1.7
newOutputStream
public static OutputStream newOutputStream(AsynchronousByteChannel ch)
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
- Since:
- 1.7
newChannel
public static ReadableByteChannel newChannel(InputStream in)
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
public static WritableByteChannel newChannel(OutputStream out)
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
public static Reader newReader(ReadableByteChannel ch, CharsetDecoder dec, int minBufferCap)
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
will be thrown. The resulting stream will not otherwise be buffered, and it will not support the mark
or reset
methods. Closing the stream will in turn cause the channel to be closed.
- Parameters:
ch
- The channel from which bytes will be readdec
- The charset decoder to be usedminBufferCap
- 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
public static Reader newReader(ReadableByteChannel ch, String csName)
An invocation of this method of the form
Channels.newReader(ch, csname)
Channels.newReader(ch, Charset.forName(csName))
- Parameters:
ch
- The channel from which bytes will be readcsName
- The name of the charset to be used- Returns:
- A new reader
- Throws:
UnsupportedCharsetException
- If no support for the named charset is available in this instance of the Java virtual machine
newReader
public static Reader newReader(ReadableByteChannel ch, Charset charset)
An invocation of this method of the form
Channels.newReader(ch, charset)
Channels.newReader(ch, charset.newDecoder(), -1)
The reader's default action for malformed-input and unmappable-character errors is to report them. When more control over the error handling is required, the constructor that takes a CharsetDecoder should be used.
- Parameters:
ch
- The channel from which bytes will be readcharset
- The charset to be used- Returns:
- A new reader
newWriter
public static Writer newWriter(WritableByteChannel ch, CharsetEncoder enc, int minBufferCap)
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
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 writtenenc
- The charset encoder to be usedminBufferCap
- 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
public static Writer newWriter(WritableByteChannel ch, String csName)
An invocation of this method of the form
Channels.newWriter(ch, csname)
Channels.newWriter(ch, Charset.forName(csName))
- Parameters:
ch
- The channel to which bytes will be writtencsName
- The name of the charset to be used- Returns:
- A new writer
- Throws:
UnsupportedCharsetException
- If no support for the named charset is available in this instance of the Java virtual machine
newWriter
public static Writer newWriter(WritableByteChannel ch, Charset charset)
An invocation of this method of the form
Channels.newWriter(ch, charset)
Channels.newWriter(ch, charset.newEncoder(), -1)
The writer's default action for malformed-input and unmappable-character errors is to report them. When more control over the error handling is required, the constructor that takes a CharsetEncoder should be used.
- Parameters:
ch
- The channel to which bytes will be writtencharset
- The charset to be used- Returns:
- A new writer
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/channels/Channels.html