This class defines static methods that support the interoperation of the
stream classes of the java. package with the channel classes
of this package.
| Modifier and Type | Class and Description |
|---|---|
| private static class | |
| private static class |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static ReadableByteChannel | Returns: A new readable byte channelThe stream from which bytes are to be read in)Constructs a channel that reads bytes from the given stream. |
| public static WritableByteChannel | Returns: A new writable byte channelThe stream to which bytes are to be written out)Constructs a channel that writes bytes to the given stream. |
| public static InputStream | Returns: A new input streamThe channel from which bytes will be read ch)Constructs a stream that reads bytes from the given channel. |
| public static InputStream | Returns: A new input streamThe channel from which bytes will be read ch)Constructs a stream that reads bytes from the given channel. |
| public static OutputStream | Returns: A new output streamThe channel to which bytes will be written ch)Constructs a stream that writes bytes to the given channel. |
| public static OutputStream | Returns: A new output streamThe channel to which bytes will be written ch)Constructs a stream that writes bytes to the given channel. |
| public static Reader | Returns: A new readerThe channel from which bytes will be read ch, CharsetDecoder The charset decoder to be used dec, int The minimum capacity of the internal byte buffer,
or minBufferCap)-1 if an implementation-dependent
default capacity is to be usedConstructs a reader that decodes bytes from the given channel using the given decoder. |
| public static Reader | Returns: A new readerThe channel from which bytes will be read ch, String The name of the charset to be used csName)Constructs a reader that decodes bytes from the given channel according to the named charset. |
| public static Reader | Returns: A new readerThe channel from which bytes will be read ch, Charset The charset to be used charset)Constructs a reader that decodes bytes from the given channel according to the given charset. |
| public static Writer | Returns: A new writerThe channel to which bytes will be written ch, CharsetEncoder The charset encoder to be used enc, int The minimum capacity of the internal byte buffer,
or minBufferCap)-1 if an implementation-dependent
default capacity is to be used. The value of
minBufferCap may be ignoredConstructs a writer that encodes characters using the given encoder and writes the resulting bytes to the given channel. |
| public static Writer | Returns: A new writerThe channel to which bytes will be written ch, String The name of the charset to be used csName)Constructs a writer that encodes characters according to the named charset and writes the resulting bytes to the given channel. |
| public static Writer | Returns: A new writerThe channel to which bytes will be written ch, Charset The charset to be used charset)Constructs a writer that encodes characters according to the given charset and writes the resulting bytes to the given channel. |
| Channels | back to summary |
|---|---|
| private Channels() | |
| newChannel | back to summary |
|---|---|
| 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. Reading from the resulting channel will read from the input stream and thus block until input is available or end of file is reached. Closing the channel will in turn cause the stream to be closed.
| |
| newChannel | back to summary |
|---|---|
| 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.
| |
| newInputStream | back to summary |
|---|---|
| public static InputStream newInputStream(ReadableByteChannel ch) Constructs a stream that reads bytes from the given channel. The
| |
| newInputStream | back to summary |
|---|---|
| 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
| |
| newOutputStream | back to summary |
|---|---|
| public static OutputStream newOutputStream(WritableByteChannel ch) Constructs a stream that writes bytes to the given channel. The
| |
| newOutputStream | back to summary |
|---|---|
| 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.
| |
| newReader | back to summary |
|---|---|
| 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
| |
| newReader | back to summary |
|---|---|
| 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) Channels.newReader(ch, Charset.forName(csName))
| |
| newReader | back to summary |
|---|---|
| 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) 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 java.nio.charset.CharsetDecoder should be used.
| |
| newWriter | back to summary |
|---|---|
| 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 Implementation Note The value of
| |
| newWriter | back to summary |
|---|---|
| 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) Channels.newWriter(ch, Charset.forName(csName))
| |
| newWriter | back to summary |
|---|---|
| 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) 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 java.nio.charset.CharsetEncoder should be used.
| |
| Modifier and Type | Field and Description |
|---|---|
| private byte[] | |
| private final InputStream | |
| private final Object | |
| private static final int |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| protected void | implCloseChannel()
Implements abstract java. Closes this channel. |
| public int | read(ByteBuffer dst)
Implements java. Reads a sequence of bytes from this channel into the given buffer. |
| buf | back to summary |
|---|---|
| private byte[] buf | |
| in | back to summary |
|---|---|
| private final InputStream in | |
| readLock | back to summary |
|---|---|
| private final Object readLock | |
| TRANSFER_SIZE | back to summary |
|---|---|
| private static final int TRANSFER_SIZE | |
| ReadableByteChannelImpl | back to summary |
|---|---|
| pack-priv ReadableByteChannelImpl(InputStream in) | |
| implCloseChannel | back to summary |
|---|---|
| protected void implCloseChannel() throws IOException Implements abstract java. Doc from java. Closes this channel. This method is invoked by the An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.
| |
| read | back to summary |
|---|---|
| public int read(ByteBuffer dst) throws IOException Implements java. Doc from java. Reads a sequence of bytes from this channel into the given buffer. An attempt is made to read up to r bytes from the channel,
where r is the number of bytes remaining in the buffer, that is,
Suppose that a byte sequence of length n is read, where
A read operation might not fill the buffer, and in fact it might not read any bytes at all. Whether or not it does so depends upon the nature and state of the channel. A socket channel in non-blocking mode, for example, cannot read any more bytes than are immediately available from the socket's input buffer; similarly, a file channel cannot read any more bytes than remain in the file. It is guaranteed, however, that if a channel is in blocking mode and there is at least one byte remaining in the buffer then this method will block until at least one byte is read. This method may be invoked at any time. If another thread has already initiated a read operation upon this channel, however, then an invocation of this method will block until the first operation is complete.
| |
| Modifier and Type | Field and Description |
|---|---|
| private byte[] | |
| private final OutputStream | |
| private static final int | |
| private final Object |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| protected void | implCloseChannel()
Implements abstract java. Closes this channel. |
| public int | write(ByteBuffer src)
Implements java. Writes a sequence of bytes to this channel from the given buffer. |
| buf | back to summary |
|---|---|
| private byte[] buf | |
| out | back to summary |
|---|---|
| private final OutputStream out | |
| TRANSFER_SIZE | back to summary |
|---|---|
| private static final int TRANSFER_SIZE | |
| writeLock | back to summary |
|---|---|
| private final Object writeLock | |
| WritableByteChannelImpl | back to summary |
|---|---|
| pack-priv WritableByteChannelImpl(OutputStream out) | |
| implCloseChannel | back to summary |
|---|---|
| protected void implCloseChannel() throws IOException Implements abstract java. Doc from java. Closes this channel. This method is invoked by the An implementation of this method must arrange for any other thread that is blocked in an I/O operation upon this channel to return immediately, either by throwing an exception or by returning normally.
| |
| write | back to summary |
|---|---|
| public int write(ByteBuffer src) throws IOException Implements java. Doc from java. Writes a sequence of bytes to this channel from the given buffer. An attempt is made to write up to r bytes to the channel,
where r is the number of bytes remaining in the buffer, that is,
Suppose that a byte sequence of length n is written, where
For many types of channels, a write operation will return only after writing all of the r requested bytes. Some types of channels, depending upon their state, may write only some of the bytes or possibly none at all. A socket channel in non-blocking mode, for example, cannot write any more bytes than are free in the socket's output buffer. The write method may need to be invoked more than once to ensure that all remaining bytes are written. This method may be invoked at any time. If another thread has already initiated a write operation upon this channel, however, then an invocation of this method will block until the first operation is complete.
| |