Most visited

Recently visited

Added in API level 1

Channels

public final class Channels
extends Object

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


频道和流的实用方法。

该类定义了支持 java.io包的流类与此包的通道类互操作的静态方法。

Summary

Public methods

static ReadableByteChannel newChannel(InputStream in)

构造一个从给定流中读取字节的通道。

static WritableByteChannel newChannel(OutputStream out)

构造一个将字节写入给定流的通道。

static InputStream newInputStream(ReadableByteChannel ch)

构造一个从给定通道读取字节的流。

static OutputStream newOutputStream(WritableByteChannel ch)

构造一个将字节写入给定通道的流。

static Reader newReader(ReadableByteChannel ch, CharsetDecoder dec, int minBufferCap)

构造一个读取器,使用给定的解码器解码给定通道的字节。

static Reader newReader(ReadableByteChannel ch, String csName)

构造一个读取器,根据指定的字符集从给定通道解码字节。

static Writer newWriter(WritableByteChannel ch, CharsetEncoder enc, int minBufferCap)

构造一个使用给定编码器编码字符的写入器,并将结果字节写入给定通道。

static Writer newWriter(WritableByteChannel ch, String csName)

构造一个编写器,根据指定的字符集编码字符并将结果字节写入给定通道。

Inherited methods

From class java.lang.Object

Public methods

newChannel

Added in API level 1
ReadableByteChannel newChannel (InputStream in)

构造一个从给定流中读取字节的通道。

生成的频道不会被缓冲; 它将简单地将其I / O操作重定向到给定的流。 关闭频道将导致流关闭。

Parameters
in InputStream: The stream from which bytes are to be read
Returns
ReadableByteChannel A new readable byte channel

newChannel

Added in API level 1
WritableByteChannel newChannel (OutputStream out)

构造一个将字节写入给定流的通道。

生成的频道不会被缓冲; 它将简单地将其I / O操作重定向到给定的流。 关闭频道将导致流关闭。

Parameters
out OutputStream: The stream to which bytes are to be written
Returns
WritableByteChannel A new writable byte channel

newInputStream

Added in API level 1
InputStream newInputStream (ReadableByteChannel ch)

构造一个从给定通道读取字节的流。

如果在底层通道处于非阻塞模式时调用,则生成的流的read方法将抛出IllegalBlockingModeException 该流不会被缓冲,并且不会支持markreset方法。 该流对于多个并发线程的访问是安全的。 关闭流将导致通道关闭。

Parameters
ch ReadableByteChannel: The channel from which bytes will be read
Returns
InputStream A new input stream

newOutputStream

Added in API level 1
OutputStream newOutputStream (WritableByteChannel ch)

构造一个将字节写入给定通道的流。

如果调用基础通道处于非阻塞模式时,结果流的write方法将抛出IllegalBlockingModeException 该流不会被缓冲。 该流对于多个并发线程的访问是安全的。 关闭流将导致通道关闭。

Parameters
ch WritableByteChannel: The channel to which bytes will be written
Returns
OutputStream A new output stream

newReader

Added in API level 1
Reader newReader (ReadableByteChannel ch, 
                CharsetDecoder dec, 
                int minBufferCap)

构造一个读取器,使用给定的解码器解码给定通道的字节。

结果流将包含至少minBufferCap字节的内部输入缓冲区。 根据需要,流的read方法将通过从底层通道读取字节来填充缓冲区; 如果通道在读取字节时处于非阻塞模式,则会抛出IllegalBlockingModeException 结果流将不会被缓冲,并且不会支持markreset方法。 关闭流将导致通道关闭。

Parameters
ch ReadableByteChannel: The channel from which bytes will be read
dec CharsetDecoder: The charset decoder to be used
minBufferCap int: The minimum capacity of the internal byte buffer, or -1 if an implementation-dependent default capacity is to be used
Returns
Reader A new reader

newReader

Added in API level 1
Reader newReader (ReadableByteChannel ch, 
                String csName)

构造一个读取器,根据指定的字符集从给定通道解码字节。

这种形式的方法的调用

 Channels.newReader(ch, csname)
behaves in exactly the same way as the expression
 Channels.newReader(ch,
                    Charset.forName(csName)
                        .newDecoder(),
                    -1);

Parameters
ch ReadableByteChannel: The channel from which bytes will be read
csName String: The name of the charset to be used
Returns
Reader A new reader
Throws
UnsupportedCharsetException If no support for the named charset is available in this instance of the Java virtual machine

newWriter

Added in API level 1
Writer newWriter (WritableByteChannel ch, 
                CharsetEncoder enc, 
                int minBufferCap)

构造一个使用给定编码器编码字符的写入器,并将结果字节写入给定通道。

结果流将包含至少minBufferCap字节的内部输出缓冲区。 根据需要,流的write方法将通过将字节写入底层通道来刷新缓冲区; 如果通道在写入字节时处于非阻塞模式,则会引发IllegalBlockingModeException 结果流将不会被缓冲。 关闭流将导致通道关闭。

Parameters
ch WritableByteChannel: The channel to which bytes will be written
enc CharsetEncoder: The charset encoder to be used
minBufferCap int: The minimum capacity of the internal byte buffer, or -1 if an implementation-dependent default capacity is to be used
Returns
Writer A new writer

newWriter

Added in API level 1
Writer newWriter (WritableByteChannel ch, 
                String csName)

构造一个编写器,根据指定的字符集编码字符并将结果字节写入给定通道。

这种形式的方法的调用

 Channels.newWriter(ch, csname)
behaves in exactly the same way as the expression
 Channels.newWriter(ch,
                    Charset.forName(csName)
                        .newEncoder(),
                    -1);

Parameters
ch WritableByteChannel: The channel to which bytes will be written
csName String: The name of the charset to be used
Returns
Writer A new writer
Throws
UnsupportedCharsetException If no support for the named charset is available in this instance of the Java virtual machine

Hooray!