FileDocCategorySizeDatePackage
Channels.javaAPI DocAndroid 1.5 API20232Wed May 06 22:41:04 BST 2009java.nio.channels

Channels

public final class Channels extends Object
This class provides several utilities to get I/O streams from channels.
since
Android 1.0

Fields Summary
Constructors Summary
private Channels()

        super();
    
Methods Summary
public static java.nio.channels.ReadableByteChannelnewChannel(java.io.InputStream inputStream)
Returns a readable channel on the given input stream. The resulting channel has the following properties:
  • If the channel is closed, then the underlying stream is closed as well.
  • It is not buffered.

param
inputStream the stream to be wrapped by a byte channel.
return
a byte channel that reads bytes from the input stream.
since
Android 1.0

        return new ReadableByteChannelImpl(inputStream);
    
public static java.nio.channels.WritableByteChannelnewChannel(java.io.OutputStream outputStream)
Returns a writable channel on the given output stream. The resulting channel has following properties:
  • If the channel is closed, then the underlying stream is closed as well.
  • It is not buffered.

param
outputStream the stream to be wrapped by a byte channel.
return
a byte channel that writes bytes to the output stream.
since
Android 1.0

        return new WritableByteChannelImpl(outputStream);
    
public static java.io.InputStreamnewInputStream(java.nio.channels.ReadableByteChannel channel)
Returns an input stream on the given channel. The resulting stream has the following properties:
  • If the stream is closed, then the underlying channel is closed as well.
  • It is thread safe.
  • It throws an {@link IllegalBlockingModeException} if the channel is in non-blocking mode and {@code read} is called.
  • Neither {@code mark} nor {@code reset} is supported.
  • It is not buffered.

param
channel the channel to be wrapped by an InputStream.
return
an InputStream that takes bytes from the given byte channel.
since
Android 1.0

        return new ReadableByteChannelInputStream(channel);
    
public static java.io.OutputStreamnewOutputStream(java.nio.channels.WritableByteChannel channel)
Returns an output stream on the given channel. The resulting stream has the following properties:
  • If the stream is closed, then the underlying channel is closed as well.
  • It is thread safe.
  • It throws an {@link IllegalBlockingModeException} if the channel is in non-blocking mode and {@code write} is called.
  • It is not buffered.

param
channel the channel to be wrapped by an OutputStream.
return
an OutputStream that puts bytes onto the given byte channel.
since
Android 1.0

        return new WritableByteChannelOutputStream(channel);
    
public static java.io.ReadernewReader(java.nio.channels.ReadableByteChannel channel, java.nio.charset.CharsetDecoder decoder, int minBufferCapacity)
Returns a reader that decodes bytes from a channel.

param
channel the Channel to be read.
param
decoder the Charset decoder to be used.
param
minBufferCapacity The minimum size of the byte buffer, -1 means to use the default size.
return
the reader.
since
Android 1.0

        return new ByteChannelReader(
                new ReaderInputStream(channel), decoder,
                minBufferCapacity);
    
public static java.io.ReadernewReader(java.nio.channels.ReadableByteChannel channel, java.lang.String charsetName)
Returns a reader that decodes bytes from a channel. This method creates a reader with a buffer of default size.

param
channel the Channel to be read.
param
charsetName the name of the charset.
return
the reader.
throws
java.nio.charset.UnsupportedCharsetException if the given charset name is not supported.
since
Android 1.0

        return newReader(channel, Charset.forName(charsetName).newDecoder(), -1);
    
public static java.io.WriternewWriter(java.nio.channels.WritableByteChannel channel, java.nio.charset.CharsetEncoder encoder, int minBufferCapacity)
Returns a writer that encodes characters with the specified {@code encoder} and sends the bytes to the specified channel.

param
channel the Channel to write to.
param
encoder the CharsetEncoder to be used.
param
minBufferCapacity the minimum size of the byte buffer, -1 means to use the default size.
return
the writer.
since
Android 1.0

        return new ByteChannelWriter(new WritableByteChannelOutputStream(
                channel), encoder, minBufferCapacity);
    
public static java.io.WriternewWriter(java.nio.channels.WritableByteChannel channel, java.lang.String charsetName)
Returns a writer that encodes characters with the specified {@code encoder} and sends the bytes to the specified channel. This method creates a writer with a buffer of default size.

param
channel the Channel to be written to.
param
charsetName the name of the charset.
return
the writer.
throws
java.nio.charset.UnsupportedCharsetException if the given charset name is not supported.
since
Android 1.0

        return newWriter(channel, Charset.forName(charsetName).newEncoder(), -1);
    
static java.nio.ByteBufferwrapByteBuffer(byte[] bytes, int offset, int length)

        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        int newLimit = offset + length <= buffer.capacity() ? offset + length
                : buffer.capacity();
        buffer.limit(newLimit);
        buffer.position(offset);
        return buffer;