Module java.base

Class Channels

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

public final class Channels extends Object
通道和流的实用方法。

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

自版本:
1.4
  • Method Details

    • newInputStream

      public static InputStream newInputStream(ReadableByteChannel ch)
      构造一个从给定通道读取字节的流。

      如果在底层通道处于非阻塞模式时调用结果流的readtransferTo方法,则会抛出IllegalBlockingModeException。如果调用transferTo方法将字节传输到以非阻塞模式写入底层通道的输出流,则还会抛出IllegalBlockingModeException。该流不会被缓冲,并且不支持markreset方法。该流可安全地被多个并发线程访问。关闭流将导致通道关闭。

      参数:
      ch - 将读取字节的通道
      返回:
      一个新的输入流
    • newOutputStream

      public static OutputStream newOutputStream(WritableByteChannel ch)
      构造一个将字节写入给定通道的流。

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

      参数:
      ch - 将写入字节的通道
      返回:
      一个新的输出流
    • newInputStream

      public static InputStream newInputStream(AsynchronousByteChannel ch)
      构造一个从给定通道读取字节的流。

      该流不会被缓冲,并且不支持markreset方法。该流可安全地被多个并发线程访问。关闭流将导致通道关闭。

      参数:
      ch - 将读取字节的通道
      返回:
      一个新的输入流
      自版本:
      1.7
    • newOutputStream

      public static OutputStream newOutputStream(AsynchronousByteChannel ch)
      构造一个将字节写入给定通道的流。

      该流不会被缓冲。该流可安全地被多个并发线程访问。关闭流将导致通道关闭。

      参数:
      ch - 将写入字节的通道
      返回:
      一个新的输出流
      自版本:
      1.7
    • newChannel

      public static ReadableByteChannel newChannel(InputStream in)
      构造一个从给定流读取字节的通道。

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

      参数:
      in - 要从中读取字节的流
      返回:
      一个新的可读字节通道
    • newChannel

      public static WritableByteChannel newChannel(OutputStream out)
      构造一个将字节写入给定流的通道。

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

      参数:
      out - 要写入字节的流
      返回:
      一个新的可写字节通道
    • newReader

      public static Reader newReader(ReadableByteChannel ch, CharsetDecoder dec, int minBufferCap)
      使用给定解码器从给定通道解码字节构造读取器。

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

      参数:
      ch - 将读取字节的通道
      dec - 要使用的字符集解码器
      minBufferCap - 内部字节缓冲区的最小容量,如果要使用实现相关的默认容量,则为-1
      返回:
      一个新的读取器
    • newReader

      public static Reader newReader(ReadableByteChannel ch, String csName)
      根据命名的字符集从给定通道解码字节构造读取器。

      形式为

          Channels.newReader(ch, csname)
      
      的此方法调用行为与表达式
          Channels.newReader(ch, Charset.forName(csName))
      
      完全相同。
      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 - 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)
      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.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 read
      charset - The charset to be used
      Returns:
      A new reader
    • newWriter

      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 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 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
    • newWriter

      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))
      
      参数:
      ch - 将要写入字节的通道
      csName - 要使用的字符集的名称
      返回值:
      一个新的写入器
      抛出:
      UnsupportedCharsetException - 如果在此Java虚拟机实例中不支持命名字符集
    • newWriter

      public static Writer newWriter(WritableByteChannel ch, Charset charset)
      构造一个编码字符并将结果字节写入给定通道的写入器。

      此方法的调用形式

          Channels.newWriter(ch, charset)
      
      的行为与表达式完全相同
          Channels.newWriter(ch, charset.newEncoder(), -1)
      

      对于格式错误输入和无法映射字符的错误,写入器的默认操作是将其报告。当需要更多控制错误处理时,应使用带有CharsetEncoder的构造函数。

      参数:
      ch - 将要写入字节的通道
      charset - 要使用的字符集
      返回值:
      一个新的写入器