Methods Summary |
---|
public static java.nio.channels.ReadableByteChannel | newChannel(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.
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.WritableByteChannel | newChannel(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.
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.InputStream | newInputStream(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.
return new sun.nio.ch.ChannelInputStream(ch);
|
public static java.io.OutputStream | newOutputStream(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.
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.Reader | newReader(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.
dec.reset();
return StreamDecoder.forDecoder(ch, dec, minBufferCap);
|
public static java.io.Reader | newReader(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);
return newReader(ch, Charset.forName(csName).newDecoder(), -1);
|
public static java.io.Writer | newWriter(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);
return newWriter(ch, Charset.forName(csName).newEncoder(), -1);
|
public static java.io.Writer | newWriter(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.
enc.reset();
return StreamEncoder.forEncoder(ch, enc, minBufferCap);
|
private static int | write(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);
}
|