org.restlet.util
Class ByteUtils

java.lang.Object
  extended by org.restlet.util.ByteUtils

public final class ByteUtils
extends java.lang.Object

Byte manipulation utilities.

Author:
Jerome Louvel

Method Summary
static long exhaust(java.io.InputStream input)
          Exhauts the content of the representation by reading it and silently discarding anything read.
static java.nio.channels.ReadableByteChannel getChannel(java.io.InputStream inputStream)
          Returns a readable byte channel based on a given inputstream.
static java.nio.channels.WritableByteChannel getChannel(java.io.OutputStream outputStream)
          Returns a writable byte channel based on a given output stream.
static java.nio.channels.ReadableByteChannel getChannel(Representation representation)
          Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method.
static java.io.Reader getReader(java.io.InputStream stream, CharacterSet characterSet)
          Returns a reader from an input stream and a character set.
static java.io.Reader getReader(WriterRepresentation representation)
          Returns a reader from a writer representation.Internally, it uses a writer thread and a pipe stream.
static java.io.InputStream getStream(java.nio.channels.ReadableByteChannel readableChannel)
          Returns an input stream based on a given readable byte channel.
static java.io.InputStream getStream(java.io.Reader reader, CharacterSet characterSet)
          Returns an input stream based on a given character reader.
static java.io.InputStream getStream(Representation representation)
          Returns an input stream based on the given representation's content and its write(OutputStream) method.
static java.io.OutputStream getStream(java.nio.channels.WritableByteChannel writableChannel)
          Returns an output stream based on a given writable byte channel.
static java.io.OutputStream getStream(java.io.Writer writer)
          Returns an output stream based on a given writer.
static java.lang.String toString(java.io.InputStream inputStream)
          Converts an input stream to a string.
As this method uses the InputstreamReader class, the default character set is used for decoding the input stream.
static java.lang.String toString(java.io.InputStream inputStream, CharacterSet characterSet)
          Converts an input stream to a string using the specified character set for decoding the input stream.
static java.lang.String toString(java.io.Reader reader)
          Converts a reader to a string.
static void write(java.nio.channels.FileChannel fileChannel, java.nio.channels.WritableByteChannel writableChannel)
          Writes the representation to a byte channel.
static void write(java.io.InputStream inputStream, java.io.OutputStream outputStream)
          Writes an input stream to an output stream.
static void write(java.io.InputStream inputStream, java.io.RandomAccessFile randomAccessFile)
          Writes an input stream to a random access file.
static void write(java.nio.channels.ReadableByteChannel readableChannel, java.nio.channels.WritableByteChannel writableChannel)
          Writes a readable channel to a writable channel.
static void write(java.io.Reader reader, java.io.Writer writer)
          Writes characters from a reader to a writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

exhaust

public static long exhaust(java.io.InputStream input)
                    throws java.io.IOException
Exhauts the content of the representation by reading it and silently discarding anything read.

Parameters:
input - The input stream to exhaust.
Returns:
The number of bytes consumed or -1 if unknown.
Throws:
java.io.IOException

getChannel

public static java.nio.channels.ReadableByteChannel getChannel(java.io.InputStream inputStream)
Returns a readable byte channel based on a given inputstream. If it is supported by a file a read-only instance of FileChannel is returned.

Parameters:
inputStream - The input stream to convert.
Returns:
A readable byte channel.

getChannel

public static java.nio.channels.WritableByteChannel getChannel(java.io.OutputStream outputStream)
Returns a writable byte channel based on a given output stream.

Parameters:
outputStream - The output stream.
Returns:
A writable byte channel.

getChannel

public static java.nio.channels.ReadableByteChannel getChannel(Representation representation)
                                                        throws java.io.IOException
Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method. Internally, it uses a writer thread and a pipe channel.

Parameters:
representation - the representation to get the OutputStream from.
Returns:
A readable byte channel.
Throws:
java.io.IOException

getReader

public static java.io.Reader getReader(java.io.InputStream stream,
                                       CharacterSet characterSet)
                                throws java.io.UnsupportedEncodingException
Returns a reader from an input stream and a character set.

Parameters:
stream - The input stream.
characterSet - The character set. May be null.
Returns:
The equivalent reader.
Throws:
java.io.UnsupportedEncodingException - if a character set is given, but not supported

getReader

public static java.io.Reader getReader(WriterRepresentation representation)
                                throws java.io.IOException
Returns a reader from a writer representation.Internally, it uses a writer thread and a pipe stream.

Parameters:
representation - The representation to read from.
Returns:
The character reader.
Throws:
java.io.IOException

getStream

public static java.io.InputStream getStream(java.nio.channels.ReadableByteChannel readableChannel)
Returns an input stream based on a given readable byte channel.

Parameters:
readableChannel - The readable byte channel.
Returns:
An input stream based on a given readable byte channel.

getStream

public static java.io.InputStream getStream(java.io.Reader reader,
                                            CharacterSet characterSet)
Returns an input stream based on a given character reader.

Parameters:
reader - The character reader.
characterSet - The stream character set.
Returns:
An input stream based on a given character reader.

getStream

public static java.io.InputStream getStream(Representation representation)
Returns an input stream based on the given representation's content and its write(OutputStream) method. Internally, it uses a writer thread and a pipe stream.

Parameters:
representation - the representation to get the OutputStream from.
Returns:
A stream with the representation's content.

getStream

public static java.io.OutputStream getStream(java.nio.channels.WritableByteChannel writableChannel)
Returns an output stream based on a given writable byte channel.

Parameters:
writableChannel - The writable byte channel.
Returns:
An output stream based on a given writable byte channel.

getStream

public static java.io.OutputStream getStream(java.io.Writer writer)
Returns an output stream based on a given writer.

Parameters:
writer - The writer.
Returns:
the output stream of the writer

toString

public static java.lang.String toString(java.io.InputStream inputStream)
Converts an input stream to a string.
As this method uses the InputstreamReader class, the default character set is used for decoding the input stream.

Parameters:
inputStream - The input stream.
Returns:
The converted string.
See Also:
InputStreamReader class< /a>, toString(InputStream, CharacterSet)

toString

public static java.lang.String toString(java.io.InputStream inputStream,
                                        CharacterSet characterSet)
Converts an input stream to a string using the specified character set for decoding the input stream.

Parameters:
inputStream - The input stream.
characterSet - The character set
Returns:
The converted string.
See Also:
InputStreamReader class< /a>

toString

public static java.lang.String toString(java.io.Reader reader)
Converts a reader to a string.

Parameters:
reader - The characters reader.
Returns:
The converted string.
See Also:
InputStreamReader class

write

public static void write(java.nio.channels.FileChannel fileChannel,
                         java.nio.channels.WritableByteChannel writableChannel)
                  throws java.io.IOException
Writes the representation to a byte channel. Optimizes using the file channel transferTo method.

Parameters:
fileChannel - The readable file channel.
writableChannel - A writable byte channel.
Throws:
java.io.IOException

write

public static void write(java.io.InputStream inputStream,
                         java.io.OutputStream outputStream)
                  throws java.io.IOException
Writes an input stream to an output stream. When the reading is done, the input stream is closed.

Parameters:
inputStream - The input stream.
outputStream - The output stream.
Throws:
java.io.IOException

write

public static void write(java.io.InputStream inputStream,
                         java.io.RandomAccessFile randomAccessFile)
                  throws java.io.IOException
Writes an input stream to a random access file. When the reading is done, the input stream is closed.

Parameters:
inputStream - The input stream.
randomAccessFile - The random access file.
Throws:
java.io.IOException

write

public static void write(java.nio.channels.ReadableByteChannel readableChannel,
                         java.nio.channels.WritableByteChannel writableChannel)
                  throws java.io.IOException
Writes a readable channel to a writable channel.

Parameters:
readableChannel - The readable channel.
writableChannel - The writable channel.
Throws:
java.io.IOException

write

public static void write(java.io.Reader reader,
                         java.io.Writer writer)
                  throws java.io.IOException
Writes characters from a reader to a writer. When the reading is done, the reader is closed.

Parameters:
reader - The reader.
writer - The writer.
Throws:
java.io.IOException


Copyright © 2005-2008 Noelios Technologies.