FileDocCategorySizeDatePackage
Channels.javaAPI DocJava SE 6 API14256Tue Jun 10 00:25:42 BST 2008java.nio.channels

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 {@link java.io} package with the channel classes of this package.

author
Mark Reinhold
author
Mike McCloskey
author
JSR-51 Expert Group
version
1.25, 05/11/17
since
1.4

Fields Summary
Constructors Summary
private Channels()

 
Methods Summary
public static java.nio.channels.ReadableByteChannelnewChannel(java.io.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.

param
in The stream from which bytes are to be read
return
A new readable byte channel

	if (in == null) {
	    throw new NullPointerException();
	}
	
	if (in instanceof FileInputStream &&
	    FileInputStream.class.equals(in.getClass())) {
	    return ((FileInputStream)in).getChannel();
        }

	return new ReadableByteChannelImpl(in);
    
public static java.nio.channels.WritableByteChannelnewChannel(java.io.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.

param
out The stream to which bytes are to be written
return
A new writable byte channel

	if (out == null) {
	    throw new NullPointerException();
	}

        if (out instanceof FileOutputStream &&
	    FileOutputStream.class.equals(out.getClass())) {
                return ((FileOutputStream)out).getChannel();
        }

	return new WritableByteChannelImpl(out);
    
public static java.io.InputStreamnewInputStream(java.nio.channels.ReadableByteChannel ch)
Constructs a stream that reads bytes from the given channel.

The read methods of the resulting stream will throw an {@link IllegalBlockingModeException} if invoked while the underlying channel is in non-blocking mode. The stream will not be buffered, and it will not support the {@link InputStream#mark mark} or {@link InputStream#reset 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.

param
ch The channel from which bytes will be read
return
A new input stream

	return new sun.nio.ch.ChannelInputStream(ch);
    
public static java.io.OutputStreamnewOutputStream(java.nio.channels.WritableByteChannel ch)
Constructs a stream that writes bytes to the given channel.

The write methods of the resulting stream will throw an {@link 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.

param
ch The channel to which bytes will be written
return
A new output stream

	return new OutputStream() {

		private ByteBuffer bb = null;
		private byte[] bs = null; 	// Invoker's previous array
		private byte[] b1 = null;

                public synchronized void write(int b) throws IOException {
                   if (b1 == null)
                        b1 = new byte[1];
                    b1[0] = (byte)b;
                    this.write(b1);
                }

                public synchronized void write(byte[] bs, int off, int len)
                    throws IOException
                {
                    if ((off < 0) || (off > bs.length) || (len < 0) ||
                        ((off + len) > bs.length) || ((off + len) < 0)) {
                        throw new IndexOutOfBoundsException();
                    } else if (len == 0) {
                        return;
                    }
                    ByteBuffer bb = ((this.bs == bs)
                                     ? this.bb
                                     : ByteBuffer.wrap(bs));
                    bb.limit(Math.min(off + len, bb.capacity()));
                    bb.position(off);
                    this.bb = bb;
                    this.bs = bs;
                    Channels.write(ch, bb);
                }

		public void close() throws IOException {
		    ch.close();
		}

	    };
    
public static java.io.ReadernewReader(java.nio.channels.ReadableByteChannel ch, java.nio.charset.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 {@link IllegalBlockingModeException} will be thrown. The resulting stream will not otherwise be buffered, and it will not support the {@link Reader#mark mark} or {@link Reader#reset reset} methods. Closing the stream will in turn cause the channel to be closed.

param
ch The channel from which bytes will be read
param
dec The charset decoder to be used
param
minBufferCap The minimum capacity of the internal byte buffer, or -1 if an implementation-dependent default capacity is to be used
return
A new reader

	dec.reset();
	return StreamDecoder.forDecoder(ch, dec, minBufferCap);
    
public static java.io.ReadernewReader(java.nio.channels.ReadableByteChannel ch, java.lang.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);

param
ch The channel from which bytes will be read
param
csName The name of the charset to be used
return
A new reader
throws
UnsupportedCharsetException If no support for the named charset is available in this instance of the Java virtual machine

	return newReader(ch, Charset.forName(csName).newDecoder(), -1);
    
public static java.io.WriternewWriter(java.nio.channels.WritableByteChannel ch, java.lang.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);

param
ch The channel to which bytes will be written
param
csName The name of the charset to be used
return
A new writer
throws
UnsupportedCharsetException If no support for the named charset is available in this instance of the Java virtual machine

	return newWriter(ch, Charset.forName(csName).newEncoder(), -1);
    
public static java.io.WriternewWriter(java.nio.channels.WritableByteChannel ch, 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.

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 {@link IllegalBlockingModeException} will be thrown. The resulting stream will not otherwise be buffered. Closing the stream will in turn cause the channel to be closed.

param
ch The channel to which bytes will be written
param
enc The charset encoder to be used
param
minBufferCap The minimum capacity of the internal byte buffer, or -1 if an implementation-dependent default capacity is to be used
return
A new writer

        enc.reset();
	return StreamEncoder.forEncoder(ch, enc, minBufferCap);
    
private static intwrite(java.nio.channels.WritableByteChannel ch, java.nio.ByteBuffer bb)

	if (ch instanceof SelectableChannel) {
	    SelectableChannel sc = (SelectableChannel)ch;
	    synchronized (sc.blockingLock()) {
		if (!sc.isBlocking())
		    throw new IllegalBlockingModeException();
		return ch.write(bb);
	    }
	} else {
	    return ch.write(bb);
	}