Class IoUtils

java.lang.Object
org.restlet.engine.io.IoUtils

public class IoUtils extends Object
IO manipulation utilities.
Author:
Thierry Boileau
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The size to use when instantiating buffered items such as instances of the BufferedReader class.
    static final int
    The number of milliseconds after which IO operation will time out.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    copy(InputStream inputStream, OutputStream outputStream)
    Copies an input stream to an output stream.
    static void
    copy(InputStream inputStream, RandomAccessFile randomAccessFile)
    Copies an input stream to a random access file.
    static void
    copy(Reader reader, Writer writer)
    Copies characters from a reader to a writer.
    static boolean
    delete(File file)
    Deletes an individual file or an empty directory.
    static boolean
    delete(File file, boolean recursive)
    Deletes an individual file or a directory.
    static boolean
    delete(File file, boolean recursive, boolean garbageCollect)
    Deletes an individual file or a directory.
    static long
    Exhaust the content of the representation by reading it and silently discarding anything read.
    static long
    Returns the size effectively available.
    static Reader
    getReader(InputStream stream, CharacterSet characterSet)
    Returns a reader from an input stream and a character set.
    static Reader
    Returns a reader from a writer representation.Internally, it uses a writer thread and a pipe stream.
    getStream(Reader reader, CharacterSet characterSet)
    Returns an input stream based on a given character reader.
    getStream(Writer writer, CharacterSet characterSet)
    Returns an output stream based on a given writer.
    getStream(Representation representation)
    Returns an input stream based on the given representation's content and its write(OutputStream) method.
    static String
    getText(Representation representation)
    Converts the representation to a string value.
    static Writer
    getWriter(OutputStream outputStream, CharacterSet characterSet)
    Returns a writer to the given output stream, using the given character set for encoding to bytes.
    static byte[]
    toByteArray(char[] chars)
    Converts a char array into a byte array using the default character set.
    static byte[]
    toByteArray(char[] chars, String charsetName)
    Converts a char array into a byte array using the provided 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, String charsetName)
    Converts a byte array into a character array using the default character set.
    static String
    toHexString(byte[] byteArray)
    Converts a byte array into an hexadecimal string.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BUFFER_SIZE

      public static final int BUFFER_SIZE
      The size to use when instantiating buffered items such as instances of the BufferedReader class. It looks for the System property "org.restlet.engine.io.bufferSize" and if not defined, uses the "8192" default value.
    • TIMEOUT_MS

      public static final int TIMEOUT_MS
      The number of milliseconds after which IO operation will time out. It looks for the System property "org.restlet.engine.io.timeoutMs" and if not defined, uses the "60000" default value.
  • Method Details

    • copy

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

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

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

      public static boolean delete(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(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(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(InputStream input) throws 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:
      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 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 OutputStream getStream(Writer writer, CharacterSet characterSet)
      Returns an output stream based on a given writer.
      Parameters:
      writer - The writer.
      characterSet - The character set used to write on the output stream.
      Returns:
      the output stream of the writer
    • 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.
    • getText

      public static String getText(Representation representation) throws 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:
      IOException
    • getWriter

      public static Writer getWriter(OutputStream outputStream, CharacterSet characterSet)
      Returns a writer to the given output stream, using the given character set for encoding to bytes.
      Parameters:
      outputStream - The target output stream.
      characterSet - The character set for encoding.
      Returns:
      The wrapping writer.
    • 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, String charsetName)
      Converts a char array into a byte array using the provided 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, 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 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 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:
    • 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. Once read, the input stream is closed.
      Parameters:
      inputStream - The input stream.
      characterSet - The character set
      Returns:
      The converted string.
      See Also:
    • toString

      public static String toString(Reader reader)
      Converts a reader to a string.
      Parameters:
      reader - The characters reader.
      Returns:
      The converted string.
      See Also: