Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/java/nio/channels/Channels#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/nio/channels/Channels#inhmethods)

# Channels

` public final class Channels `   
` extends https://developer.android.com/reference/java/lang/Object ` ` `

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.nio.channels.Channels |

<br />

*** ** * ** ***

Utility methods for channels and streams.

This class defines static methods that support the interoperation of the stream classes of the `https://developer.android.com/reference/java/io/package-summary` package with the channel classes of this package.

<br />

## Summary

| ### Public methods ||
|---|---|
| ` static https://developer.android.com/reference/java/nio/channels/ReadableByteChannel` | ` https://developer.android.com/reference/java/nio/channels/Channels#newChannel(java.io.InputStream)(https://developer.android.com/reference/java/io/InputStream in) ` Constructs a channel that reads bytes from the given stream. |
| ` static https://developer.android.com/reference/java/nio/channels/WritableByteChannel` | ` https://developer.android.com/reference/java/nio/channels/Channels#newChannel(java.io.OutputStream)(https://developer.android.com/reference/java/io/OutputStream out) ` Constructs a channel that writes bytes to the given stream. |
| ` static https://developer.android.com/reference/java/io/InputStream` | ` https://developer.android.com/reference/java/nio/channels/Channels#newInputStream(java.nio.channels.AsynchronousByteChannel)(https://developer.android.com/reference/java/nio/channels/AsynchronousByteChannel ch) ` Constructs a stream that reads bytes from the given channel. |
| ` static https://developer.android.com/reference/java/io/InputStream` | ` https://developer.android.com/reference/java/nio/channels/Channels#newInputStream(java.nio.channels.ReadableByteChannel)(https://developer.android.com/reference/java/nio/channels/ReadableByteChannel ch) ` Constructs a stream that reads bytes from the given channel. |
| ` static https://developer.android.com/reference/java/io/OutputStream` | ` https://developer.android.com/reference/java/nio/channels/Channels#newOutputStream(java.nio.channels.AsynchronousByteChannel)(https://developer.android.com/reference/java/nio/channels/AsynchronousByteChannel ch) ` Constructs a stream that writes bytes to the given channel. |
| ` static https://developer.android.com/reference/java/io/OutputStream` | ` https://developer.android.com/reference/java/nio/channels/Channels#newOutputStream(java.nio.channels.WritableByteChannel)(https://developer.android.com/reference/java/nio/channels/WritableByteChannel ch) ` Constructs a stream that writes bytes to the given channel. |
| ` static https://developer.android.com/reference/java/io/Reader` | ` https://developer.android.com/reference/java/nio/channels/Channels#newReader(java.nio.channels.ReadableByteChannel,%20java.nio.charset.Charset)(https://developer.android.com/reference/java/nio/channels/ReadableByteChannel ch, https://developer.android.com/reference/java/nio/charset/Charset charset) ` Constructs a reader that decodes bytes from the given channel according to the given charset. |
| ` static https://developer.android.com/reference/java/io/Reader` | ` https://developer.android.com/reference/java/nio/channels/Channels#newReader(java.nio.channels.ReadableByteChannel,%20java.lang.String)(https://developer.android.com/reference/java/nio/channels/ReadableByteChannel ch, https://developer.android.com/reference/java/lang/String csName) ` Constructs a reader that decodes bytes from the given channel according to the named charset. |
| ` static https://developer.android.com/reference/java/io/Reader` | ` https://developer.android.com/reference/java/nio/channels/Channels#newReader(java.nio.channels.ReadableByteChannel,%20java.nio.charset.CharsetDecoder,%20int)(https://developer.android.com/reference/java/nio/channels/ReadableByteChannel ch, https://developer.android.com/reference/java/nio/charset/CharsetDecoder dec, int minBufferCap) ` Constructs a reader that decodes bytes from the given channel using the given decoder. |
| ` static https://developer.android.com/reference/java/io/Writer` | ` https://developer.android.com/reference/java/nio/channels/Channels#newWriter(java.nio.channels.WritableByteChannel,%20java.nio.charset.CharsetEncoder,%20int)(https://developer.android.com/reference/java/nio/channels/WritableByteChannel ch, https://developer.android.com/reference/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 https://developer.android.com/reference/java/io/Writer` | ` https://developer.android.com/reference/java/nio/channels/Channels#newWriter(java.nio.channels.WritableByteChannel,%20java.nio.charset.Charset)(https://developer.android.com/reference/java/nio/channels/WritableByteChannel ch, https://developer.android.com/reference/java/nio/charset/Charset charset) ` Constructs a writer that encodes characters according to the given charset and writes the resulting bytes to the given channel. |
| ` static https://developer.android.com/reference/java/io/Writer` | ` https://developer.android.com/reference/java/nio/channels/Channels#newWriter(java.nio.channels.WritableByteChannel,%20java.lang.String)(https://developer.android.com/reference/java/nio/channels/WritableByteChannel ch, https://developer.android.com/reference/java/lang/String csName) ` Constructs a writer that encodes characters according to the named charset and writes the resulting bytes to the given channel. |

### Inherited methods

From class ` https://developer.android.com/reference/java/lang/Object ` ` https://developer.android.com/reference/java/lang/Object` ` https://developer.android.com/reference/java/lang/Object#clone()() `

Creates and returns a copy of this object.
` boolean` ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) `

Indicates whether some other object is "equal to" this one.
` void` ` https://developer.android.com/reference/java/lang/Object#finalize()() `

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
` final https://developer.android.com/reference/java/lang/Class<?>` ` https://developer.android.com/reference/java/lang/Object#getClass()() `

Returns the runtime class of this `Object`.
` int` ` https://developer.android.com/reference/java/lang/Object#hashCode()() `

Returns a hash code value for the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#notify()() `

Wakes up a single thread that is waiting on this object's monitor.
` final void` ` https://developer.android.com/reference/java/lang/Object#notifyAll()() `

Wakes up all threads that are waiting on this object's monitor.
` https://developer.android.com/reference/java/lang/String` ` https://developer.android.com/reference/java/lang/Object#toString()() `

Returns a string representation of the object.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed.
` final void` ` https://developer.android.com/reference/java/lang/Object#wait()() `

Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*.

## Public methods

### newChannel

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static ReadableByteChannel newChannel (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.

<br />

<br />

| Parameters ||
|---|---|
| `in` | `InputStream`: The stream from which bytes are to be read <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/ReadableByteChannel` | A new readable byte channel <br /> |

### newChannel

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static WritableByteChannel newChannel (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.

<br />

<br />

| Parameters ||
|---|---|
| `out` | `OutputStream`: The stream to which bytes are to be written <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/channels/WritableByteChannel` | A new writable byte channel <br /> |

### newInputStream

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static InputStream newInputStream (AsynchronousByteChannel ch)
```

Constructs a stream that reads bytes from the given channel.

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

<br />

<br />

| Parameters ||
|---|---|
| `ch` | `AsynchronousByteChannel`: The channel from which bytes will be read <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/InputStream` | A new input stream <br /> |

### newInputStream

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static InputStream newInputStream (ReadableByteChannel ch)
```

Constructs a stream that reads bytes from the given channel.

The `read` methods of the resulting stream will throw an `https://developer.android.com/reference/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 `https://developer.android.com/reference/java/io/InputStream#mark(int)` or `https://developer.android.com/reference/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.

<br />

<br />

| Parameters ||
|---|---|
| `ch` | `ReadableByteChannel`: The channel from which bytes will be read <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/InputStream` | A new input stream <br /> |

### newOutputStream

Added in [API level 26](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static OutputStream newOutputStream (AsynchronousByteChannel 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 />

<br />

| Parameters ||
|---|---|
| `ch` | `AsynchronousByteChannel`: The channel to which bytes will be written <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/OutputStream` | A new output stream <br /> |

### newOutputStream

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static OutputStream newOutputStream (WritableByteChannel ch)
```

Constructs a stream that writes bytes to the given channel.

The `write` methods of the resulting stream will throw an `https://developer.android.com/reference/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.

<br />

<br />

| Parameters ||
|---|---|
| `ch` | `WritableByteChannel`: The channel to which bytes will be written <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/OutputStream` | A new output stream <br /> |

### newReader

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static Reader newReader (ReadableByteChannel ch, 
                Charset charset)
```

Constructs a reader that decodes bytes from the given channel according to the given charset.

An invocation of this method of the form

    Channels.newReader(ch, charset)
     
behaves in exactly the same way as the expression

    Channels.newReader(ch, Charset.forName(csName).newDecoder(), -1)
     
The reader's default action for malformed-input and unmappable-character errors is to [report](https://developer.android.com/reference/java/nio/charset/CodingErrorAction#REPORT) them. When more control over the error handling is required, the constructor that takes a [CharsetDecoder](https://developer.android.com/reference/java/nio/charset/CharsetDecoder) should be used.

<br />

| Parameters ||
|---|---|
| `ch` | `ReadableByteChannel`: The channel from which bytes will be read <br /> |
| `charset` | `Charset`: The charset to be used <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Reader` | A new reader <br /> |

### newReader

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static Reader newReader (ReadableByteChannel ch, 
                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))
     
<br />

<br />

| Parameters ||
|---|---|
| `ch` | `ReadableByteChannel`: The channel from which bytes will be read <br /> |
| `csName` | `String`: The name of the charset to be used <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Reader` | A new reader <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/charset/UnsupportedCharsetException` | If no support for the named charset is available in this instance of the Java virtual machine |

### newReader

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

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

<br />

<br />

| Parameters ||
|---|---|
| `ch` | `ReadableByteChannel`: The channel from which bytes will be read <br /> |
| `dec` | `CharsetDecoder`: The charset decoder to be used <br /> |
| `minBufferCap` | `int`: The minimum capacity of the internal byte buffer, or `-1` if an implementation-dependent default capacity is to be used <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Reader` | A new reader <br /> |

### newWriter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static Writer newWriter (WritableByteChannel ch, 
                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 `https://developer.android.com/reference/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.

<br />

<br />

| Parameters ||
|---|---|
| `ch` | `WritableByteChannel`: The channel to which bytes will be written <br /> |
| `enc` | `CharsetEncoder`: The charset encoder to be used <br /> |
| `minBufferCap` | `int`: The minimum capacity of the internal byte buffer, or `-1` if an implementation-dependent default capacity is to be used <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Writer` | A new writer <br /> |

### newWriter

Added in [API level 33](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static Writer newWriter (WritableByteChannel ch, 
                Charset charset)
```

Constructs a writer that encodes characters according to the given charset and writes the resulting bytes to the given channel.

An invocation of this method of the form

    Channels.newWriter(ch, charset)
     
behaves in exactly the same way as the expression

    Channels.newWriter(ch, Charset.forName(csName).newEncoder(), -1)
     
The writer's default action for malformed-input and unmappable-character errors is to [report](https://developer.android.com/reference/java/nio/charset/CodingErrorAction#REPORT) them. When more control over the error handling is required, the constructor that takes a [CharsetEncoder](https://developer.android.com/reference/java/nio/charset/CharsetEncoder) should be used.

<br />

| Parameters ||
|---|---|
| `ch` | `WritableByteChannel`: The channel to which bytes will be written <br /> |
| `charset` | `Charset`: The charset to be used <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Writer` | A new writer <br /> |

### newWriter

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public static Writer newWriter (WritableByteChannel ch, 
                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))
     
<br />

<br />

| Parameters ||
|---|---|
| `ch` | `WritableByteChannel`: The channel to which bytes will be written <br /> |
| `csName` | `String`: The name of the charset to be used <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Writer` | A new writer <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/nio/charset/UnsupportedCharsetException` | If no support for the named charset is available in this instance of the Java virtual machine |