org.restlet.engine.io

## Class BioUtils

- java.lang.Object
- - org.restlet.engine.io.BioUtils

    public final class BioUtils
    extends java.lang.Object

Basic IO manipulation utilities.

Author:
  Jerome Louvel

### Method Summary

Methods
`static void` `copy(java.io.InputStream inputStream, java.io.OutputStream outputStream)` Copies an input stream to an output stream.
`static void` `copy(java.io.InputStream inputStream, java.io.RandomAccessFile randomAccessFile)` Copies an input stream to a random access file.
`static void` `copy(java.io.Reader reader, java.io.Writer writer)` Copies characters from a reader to a writer.
`static boolean` `delete(java.io.File file)` Deletes an individual file or an empty directory.
`static boolean` `delete(java.io.File file, boolean recursive)` Deletes an individual file or a directory.
`static boolean` `delete(java.io.File file, boolean recursive, boolean garbageCollect)` Deletes an individual file or a directory.
`static long` `exhaust(java.io.InputStream input)` Exhaust the content of the representation by reading it and silently discarding anything read.
`static long` `getAvailableSize(Representation representation)` Returns the size effectively available.
`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.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.io.Writer writer)` Returns an output stream based on a given writer.
`static java.lang.String` `getText(Representation representation)` Converts the representation to a string value.
`static byte[]` `toByteArray(char[] chars)` Converts a char array into a byte array using the default character set.
`static byte[]` `toByteArray(char[] chars, java.lang.String charsetName)` Converts a char array into a byte array using the default character set.
`static char[]` `toCharArray(byte[] bytes)` Converts a byte array into a character array using the default character set.
`static char[]` `toCharArray(byte[] bytes, java.lang.String charsetName)` Converts a byte array into a character array using the default character set.
`static java.lang.String` `toHexString(byte[] byteArray)` Converts a byte array into an hexadecimal string.
`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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### copy

    public static void copy(java.io.InputStream inputStream,
            java.io.OutputStream outputStream)
                     throws java.io.IOException

Copies 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`

#### copy

    public static void copy(java.io.InputStream inputStream,
            java.io.RandomAccessFile randomAccessFile)
                     throws java.io.IOException

Copies 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`

#### copy

    public static void copy(java.io.Reader reader,
            java.io.Writer writer)
                     throws java.io.IOException

Copies 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`

#### delete

    public static boolean delete(java.io.File file)

Deletes an individual file or an empty directory.

Parameters:
  `file` - The individual file or directory to delete.
Returns:
  True if the deletion was successful.

#### delete

    public static boolean delete(java.io.File file,
                 boolean recursive)

Deletes an individual file or a directory. A recursive deletion can be forced as well. Under Windows operating systems, the garbage collector will be invoked once before attempting to delete in order to prevent locking issues.

Parameters:
  `file` - The individual file or directory to delete.
  `recursive` - Indicates if directory with content should be deleted recursively as well.
Returns:
  True if the deletion was successful or if the file or directory didn't exist.

#### delete

    public static boolean delete(java.io.File file,
                 boolean recursive,
                 boolean garbageCollect)

Deletes an individual file or a directory. A recursive deletion can be forced as well. The garbage collector can be run once before attempting to delete, to workaround lock issues under Windows operating systems.

Parameters:
  `file` - The individual file or directory to delete.
  `recursive` - Indicates if directory with content should be deleted recursively as well.
  `garbageCollect` - Indicates if the garbage collector should be run.
Returns:
  True if the deletion was successful or if the file or directory didn't exist.

#### exhaust

    public static long exhaust(java.io.InputStream input)
                        throws java.io.IOException

Exhaust 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`

#### getAvailableSize

    public static long getAvailableSize(Representation representation)

Returns the size effectively available. This returns the same value as `Representation.getSize()` if no range is defined, otherwise it returns the size of the range using `Range.getSize()`.

Parameters:
  `representation` - The representation to evaluate.
Returns:
  The available size.

#### 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.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

#### 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.

#### getText

    public static java.lang.String getText(Representation representation)
                                    throws java.io.IOException

Converts the representation to a string value. Be careful when using this method as the conversion of large content to a string fully stored in memory can result in OutOfMemoryErrors being thrown.

Parameters:
  `representation` - The representation to convert.
Returns:
  The representation as a string value.
Throws:
  `java.io.IOException`

#### toByteArray

    public static byte[] toByteArray(char[] chars)

Converts a char array into a byte array using the default character set.

Parameters:
  `chars` - The source characters.
Returns:
  The result bytes.

#### toByteArray

    public static byte[] toByteArray(char[] chars,
                     java.lang.String charsetName)

Converts a char array into a byte array using the default character set.

Parameters:
  `chars` - The source characters.
  `charsetName` - The character set to use.
Returns:
  The result bytes.

#### toCharArray

    public static char[] toCharArray(byte[] bytes)

Converts a byte array into a character array using the default character set.

Parameters:
  `bytes` - The source bytes.
Returns:
  The result characters.

#### toCharArray

    public static char[] toCharArray(byte[] bytes,
                     java.lang.String charsetName)

Converts a byte array into a character array using the default character set.

Parameters:
  `bytes` - The source bytes.
  `charsetName` - The character set to use.
Returns:
  The result characters.

#### toHexString

    public static java.lang.String toHexString(byte[] byteArray)

Converts a byte array into an hexadecimal string.

Parameters:
  `byteArray` - The byte array to convert.
Returns:
  The hexadecimal string.

#### 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, `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. Once read, the input stream is closed.

Parameters:
  `inputStream` - The input stream.
  `characterSet` - The character set
Returns:
  The converted string.
See Also:
  InputStreamReader class

#### 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