# Channels

public final class **Channels** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
Utility methods for channels and streams.

This class defines static methods that support the interoperation of the
stream classes of the `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/package-summary.html` package with the channel
classes of this package.

<br />

### Public Method Summary

|---|---|
| static [ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html) | [newChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newChannel(java.io.InputStream))([InputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html) in) Constructs a channel that reads bytes from the given stream. |
| static [WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html) | [newChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newChannel(java.io.OutputStream))([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) out) Constructs a channel that writes bytes to the given stream. |
| static [InputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html) | [newInputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newInputStream(java.nio.channels.ReadableByteChannel))([ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html) ch) Constructs a stream that reads bytes from the given channel. |
| static [InputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html) | [newInputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newInputStream(java.nio.channels.AsynchronousByteChannel))([AsynchronousByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html) ch) Constructs a stream that reads bytes from the given channel. |
| static [OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) | [newOutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newOutputStream(java.nio.channels.AsynchronousByteChannel))([AsynchronousByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html) ch) Constructs a stream that writes bytes to the given channel. |
| static [OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) | [newOutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newOutputStream(java.nio.channels.WritableByteChannel))([WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html) ch) Constructs a stream that writes bytes to the given channel. |
| static [Reader](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Reader.html) | [newReader](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newReader(java.nio.channels.ReadableByteChannel,%20java.nio.charset.CharsetDecoder,%20int))([ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html) ch, [CharsetDecoder](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/CharsetDecoder.html) dec, int minBufferCap) Constructs a reader that decodes bytes from the given channel using the given decoder. |
| static [Reader](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Reader.html) | [newReader](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newReader(java.nio.channels.ReadableByteChannel,%20java.lang.String))([ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html) ch, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) csName) Constructs a reader that decodes bytes from the given channel according to the named charset. |
| static [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) | [newWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newWriter(java.nio.channels.WritableByteChannel,%20java.nio.charset.CharsetEncoder,%20int))([WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html) ch, [CharsetEncoder](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/CharsetEncoder.html) enc, int minBufferCap) Constructs a writer that encodes characters using the given encoder and writes the resulting bytes to the given channel. |
| static [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html) | [newWriter](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channels.html#newWriter(java.nio.channels.WritableByteChannel,%20java.lang.String))([WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html) ch, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) csName) Constructs a writer that encodes characters according to the named charset and writes the resulting bytes to the given channel. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

## Public Methods

#### public static [ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html)
**newChannel**
([InputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html) 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.

<br />

##### Parameters

| in | The stream from which bytes are to be read |
|---|---|

##### Returns

- A new readable byte channel

#### public static [WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html)
**newChannel**
([OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html) 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.

<br />

##### Parameters

| out | The stream to which bytes are to be written |
|---|---|

##### Returns

- A new writable byte channel

#### public static [InputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html)
**newInputStream**
([ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html) ch)

Constructs a stream that reads bytes from the given channel.

The read methods of the resulting stream will throw an
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/IllegalBlockingModeException.html` if invoked while the underlying
channel is in non-blocking mode. The stream will not be buffered, and
it will not support the `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html#mark(int)` or `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html#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.

<br />

##### Parameters

| ch | The channel from which bytes will be read |
|---|---|

##### Returns

- A new input stream

#### public static [InputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html)
**newInputStream**
([AsynchronousByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html) ch)

Constructs a stream that reads bytes from the given channel.

The stream will not be buffered, and it will not support the `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html#mark(int)` or `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/InputStream.html#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.

<br />

##### Parameters

| ch | The channel from which bytes will be read |
|---|---|

##### Returns

- A new input stream

#### public static [OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html)
**newOutputStream**
([AsynchronousByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html) ch)

Constructs a stream that writes bytes to the given channel.

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.

<br />

##### Parameters

| ch | The channel to which bytes will be written |
|---|---|

##### Returns

- A new output stream

#### public static [OutputStream](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/OutputStream.html)
**newOutputStream**
([WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html) ch)

Constructs a stream that writes bytes to the given channel.

The write methods of the resulting stream will throw an
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/IllegalBlockingModeException.html` 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.

<br />

##### Parameters

| ch | The channel to which bytes will be written |
|---|---|

##### Returns

- A new output stream

#### public static [Reader](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Reader.html)
**newReader**
([ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html) ch, [CharsetDecoder](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/CharsetDecoder.html) 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/IllegalBlockingModeException.html` will be thrown. The
resulting stream will not otherwise be buffered, and it will not support
the `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Reader.html#mark(int)` or `https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Reader.html#reset()` methods.
Closing the stream will in turn cause the channel to be closed.

<br />

##### 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

#### public static [Reader](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Reader.html)
**newReader**
([ReadableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ReadableByteChannel.html) ch, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) 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);
> ```

<br />

##### 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/UnsupportedCharsetException.html) | If no support for the named charset is available in this instance of the Java virtual machine |
|---|---|

#### public static [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html)
**newWriter**
([WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html) ch, [CharsetEncoder](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/CharsetEncoder.html) 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/IllegalBlockingModeException.html` will be thrown.
The resulting stream will not otherwise be buffered. Closing the stream
will in turn cause the channel to be closed.

<br />

##### 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

#### public static [Writer](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Writer.html)
**newWriter**
([WritableByteChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/WritableByteChannel.html) ch, [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) 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);
> ```

<br />

##### 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](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/charset/UnsupportedCharsetException.html) | If no support for the named charset is available in this instance of the Java virtual machine |
|---|---|