org.restlet.util
Class ByteUtils

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

public final class ByteUtils
extends Object

Byte manipulation utilities.

Author:
Jerome Louvel

Method Summary
static long exhaust(InputStream input)
          Exhauts the content of the representation by reading it and silently discarding anything read.
static ReadableByteChannel getChannel(InputStream inputStream)
          Returns a readable byte channel based on a given inputstream.
static WritableByteChannel getChannel(OutputStream outputStream)
          Returns a writable byte channel based on a given output stream.
static ReadableByteChannel getChannel(Representation representation)
          Returns a readable byte channel based on the given representation's content and its write(WritableByteChannel) method.
static Reader getReader(InputStream stream, CharacterSet characterSet)
          Returns a reader from an input stream and a character set.
static Reader getReader(WriterRepresentation representation)
          Returns a reader from a writer representation.Internally, it uses a writer thread and a pipe stream.
static InputStream getStream(ReadableByteChannel readableChannel)
          Returns an input stream based on a given readable byte channel.
static InputStream getStream(Reader reader, CharacterSet characterSet)
          Returns an input stream based on a given character reader.
static InputStream getStream(Representation representation)
          Returns an input stream based on the given representation's content and its write(OutputStream) method.
static OutputStream getStream(WritableByteChannel writableChannel)
          Returns an output stream based on a given writable byte channel.
static OutputStream getStream(Writer writer)
          Returns an output stream based on a given writer.
static String toString(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 String toString(InputStream inputStream, CharacterSet characterSet)
          Converts an input stream to a string using the specified character set for decoding the input stream.
static String toString(Reader reader)
          Converts a reader to a string.
static void write(FileChannel fileChannel, WritableByteChannel writableChannel)
          Writes the representation to a byte channel.
static void write(InputStream inputStream, OutputStream outputStream)
          Writes an input stream to an output stream.
static void write(InputStream inputStream, RandomAccessFile randomAccessFile)
          Writes an input stream to a random access file.
static void write(ReadableByteChannel readableChannel, WritableByteChannel writableChannel)
          Writes a readable channel to a writable channel.
static void write(Reader reader, 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(InputStream input)
                    throws 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:
IOException

getChannel

public static ReadableByteChannel getChannel(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 WritableByteChannel getChannel(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 ReadableByteChannel getChannel(Representation representation)
                                      throws 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:
IOException

getReader

public static Reader getReader(InputStream stream,
                               CharacterSet characterSet)
                        throws 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:
UnsupportedEncodingException - if a character set is given, but not supported

getReader

public static Reader getReader(WriterRepresentation representation)
                        throws 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:
IOException

getStream

public static InputStream getStream(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 InputStream getStream(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 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 OutputStream getStream(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 OutputStream getStream(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 String toString(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 String toString(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 String toString(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(FileChannel fileChannel,
                         WritableByteChannel writableChannel)
                  throws 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:
IOException

write

public static void write(InputStream inputStream,
                         OutputStream outputStream)
                  throws 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:
IOException

write

public static void write(InputStream inputStream,
                         RandomAccessFile randomAccessFile)
                  throws 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:
IOException

write

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

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

write

public static void write(Reader reader,
                         Writer writer)
                  throws 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:
IOException


Copyright © 2005-2008 Noelios Technologies.