/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Utility methods for channels and streams.
*
* <p> This class defines static methods that support the interoperation of the
* stream classes of the <tt>{@link java.io}</tt> package with the channel
* classes of this package. </p>
*
*
* @author Mark Reinhold
* @author Mike McCloskey
* @author JSR-51 Expert Group
* @since 1.4
*/
public final class Channels {
if (o == null)
}
/**
* Write all remaining bytes in buffer to the given channel.
* If the channel is selectable then it must be configured blocking.
*/
throws IOException
{
if (n <= 0)
throw new RuntimeException("no bytes written");
}
}
/**
* Write all remaining bytes in buffer to the given channel.
*
* @throws IllegalBlockingException
* If the channel is selectable and configured non-blocking.
*/
throws IOException
{
if (ch instanceof SelectableChannel) {
synchronized (sc.blockingLock()) {
if (!sc.isBlocking())
throw new IllegalBlockingModeException();
}
} else {
}
}
// -- Byte streams from channels --
/**
* Constructs a stream that reads bytes from the given channel.
*
* <p> The <tt>read</tt> 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. </p>
*
* @param ch
* The channel from which bytes will be read
*
* @return A new input stream
*/
}
/**
* Constructs a stream that writes bytes to the given channel.
*
* <p> The <tt>write</tt> 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. </p>
*
* @param ch
* The channel to which bytes will be written
*
* @return A new output stream
*/
return new OutputStream() {
public synchronized void write(int b) throws IOException {
b1 = new byte[1];
b1[0] = (byte)b;
}
throws IOException
{
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
? this.bb
}
public void close() throws IOException {
}
};
}
/**
* Constructs a stream that reads bytes from the given channel.
*
* <p> 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. </p>
*
* @param ch
* The channel from which bytes will be read
*
* @return A new input stream
*
* @since 1.7
*/
return new InputStream() {
public synchronized int read() throws IOException {
b1 = new byte[1];
if (n == 1)
return -1;
}
throws IOException
{
throw new IndexOutOfBoundsException();
} else if (len == 0)
return 0;
? this.bb
boolean interrupted = false;
try {
for (;;) {
try {
} catch (ExecutionException ee) {
} catch (InterruptedException ie) {
interrupted = true;
}
}
} finally {
if (interrupted)
}
}
public void close() throws IOException {
}
};
}
/**
* Constructs a stream that writes bytes to the given channel.
*
* <p> 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. </p>
*
* @param ch
* The channel to which bytes will be written
*
* @return A new output stream
*
* @since 1.7
*/
return new OutputStream() {
public synchronized void write(int b) throws IOException {
b1 = new byte[1];
b1[0] = (byte)b;
}
throws IOException
{
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
? this.bb
boolean interrupted = false;
try {
try {
} catch (ExecutionException ee) {
} catch (InterruptedException ie) {
interrupted = true;
}
}
} finally {
if (interrupted)
}
}
public void close() throws IOException {
}
};
}
// -- Channels from streams --
/**
* Constructs a channel that reads bytes from the given stream.
*
* <p> 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. </p>
*
* @param in
* The stream from which bytes are to be read
*
* @return A new readable byte channel
*/
if (in instanceof FileInputStream &&
}
return new ReadableByteChannelImpl(in);
}
private static class ReadableByteChannelImpl
extends AbstractInterruptibleChannel // Not really interruptible
implements ReadableByteChannel
{
private boolean open = true;
}
int totalRead = 0;
int bytesRead = 0;
synchronized (readLock) {
buf = new byte[bytesToRead];
break; // block at most once
try {
begin();
} finally {
}
if (bytesRead < 0)
break;
else
}
return -1;
return totalRead;
}
}
open = false;
}
}
/**
* Constructs a channel that writes bytes to the given stream.
*
* <p> 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. </p>
*
* @param out
* The stream to which bytes are to be written
*
* @return A new writable byte channel
*/
if (out instanceof FileOutputStream &&
}
return new WritableByteChannelImpl(out);
}
private static class WritableByteChannelImpl
extends AbstractInterruptibleChannel // Not really interruptible
implements WritableByteChannel
{
private boolean open = true;
}
int totalWritten = 0;
synchronized (writeLock) {
while (totalWritten < len) {
buf = new byte[bytesToWrite];
try {
begin();
} finally {
}
}
return totalWritten;
}
}
open = false;
}
}
// -- Character streams from channels --
/**
* Constructs a reader that decodes bytes from the given channel using the
* given decoder.
*
* <p> The resulting stream will contain an internal input buffer of at
* least <tt>minBufferCap</tt> bytes. The stream's <tt>read</tt> 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. </p>
*
* @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 <tt>-1</tt> if an implementation-dependent
* default capacity is to be used
*
* @return A new reader
*/
int minBufferCap)
{
}
/**
* Constructs a reader that decodes bytes from the given channel according
* to the named charset.
*
* <p> An invocation of this method of the form
*
* <blockquote><pre>
* Channels.newReader(ch, csname)</pre></blockquote>
*
* behaves in exactly the same way as the expression
*
* <blockquote><pre>
* Channels.newReader(ch,
* Charset.forName(csName)
* .newDecoder(),
* -1);</pre></blockquote>
*
* @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
*/
{
}
/**
* Constructs a writer that encodes characters using the given encoder and
* writes the resulting bytes to the given channel.
*
* <p> The resulting stream will contain an internal output buffer of at
* least <tt>minBufferCap</tt> bytes. The stream's <tt>write</tt> 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. </p>
*
* @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 <tt>-1</tt> if an implementation-dependent
* default capacity is to be used
*
* @return A new writer
*/
final CharsetEncoder enc,
final int minBufferCap)
{
}
/**
* Constructs a writer that encodes characters according to the named
* charset and writes the resulting bytes to the given channel.
*
* <p> An invocation of this method of the form
*
* <blockquote><pre>
* Channels.newWriter(ch, csname)</pre></blockquote>
*
* behaves in exactly the same way as the expression
*
* <blockquote><pre>
* Channels.newWriter(ch,
* Charset.forName(csName)
* .newEncoder(),
* -1);</pre></blockquote>
*
* @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
*/
{
}
}