java.nio.channels

Class Channels


  • public final class Channels
    extends Object
    Utility methods for channels and streams.

    This class defines static methods that support the interoperation of the stream classes of the java.io package with the channel classes of this package.

    Since:
    1.4
    • Method Detail

      • newInputStream

        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 IllegalBlockingModeException if invoked while the underlying channel is in non-blocking mode. The stream will not be buffered, and it will not support the mark or 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.

        Parameters:
        ch - The channel from which bytes will be read
        Returns:
        A new input stream
      • newOutputStream

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

        Parameters:
        ch - The channel to which bytes will be written
        Returns:
        A new output stream
      • newInputStream

        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 mark or 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.

        Parameters:
        ch - The channel from which bytes will be read
        Returns:
        A new input stream
        Since:
        1.7
      • newOutputStream

        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.

        Parameters:
        ch - The channel to which bytes will be written
        Returns:
        A new output stream
        Since:
        1.7
      • newChannel

        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.

        Parameters:
        in - The stream from which bytes are to be read
        Returns:
        A new readable byte channel
      • newChannel

        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.

        Parameters:
        out - The stream to which bytes are to be written
        Returns:
        A new writable byte channel
      • newReader

        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 IllegalBlockingModeException will be thrown. The resulting stream will not otherwise be buffered, and it will not support the mark or reset methods. Closing the stream will in turn cause the channel to be closed.

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

        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)
                                .newDecoder(),
                            -1);
        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
      • 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)
                                .newEncoder(),
                            -1);
        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 - If no support for the named charset is available in this instance of the Java virtual machine

Document created the 11/06/2005, last modified the 04/03/2020
Source of the printed document:https://www.gaudry.be/en/java-api-rf-java/nio/channels/Channels.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : https://docs.oracle.com

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut