util.io

## Class IOUtilities

- java.lang.Object
- - util.io.IOUtilities

-     public class IOUtilities
      extends java.lang.Object

  A utilities class for I/O stuff. It constists of serveral static methods that perform some usefull things.
  Author:
    Til Schneider, www.murfman.de

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `IOUtilities()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static void` | `append(java.lang.StringBuffer buffer, int number, int minChars)` Appends an integer to a StringBuffer.
    `static java.lang.String` | `checkForRelativePath(java.lang.String path)` Check given path if it is a relative path of the TV-Browser settings home directory.
    `static void` | `clear(java.lang.StringBuffer buffer)` Clears an StringBuffer
    `static void` | `copy(java.io.File[] src, java.io.File targetDir)` Copies files given in source to the target directory.
    `static void` | `copy(java.io.File[] src, java.io.File targetDir, boolean onlyNew)` Copies files given in source to the target directory.
    `static void` | `copy(java.io.File src, java.io.File target)` Copies a file.
    `static void` | `copy(java.io.File src, java.io.File target, boolean onlyNew)` Copies a file.
    `static java.lang.Integer[]` | `decodeSingleFieldValueToMultipleEpisodeNumers(int fieldValue)` Decode the given value into an array of episode numbers.
    `static java.lang.String` | `decodeSingleFieldValueToMultipleEpisodeString(int fieldValue)` Decode the given value into a String of episode numbers.
    `static void` | `deleteDirectory(java.io.File dir)` Deletes a directory with all its files and subdirectories.
    `static void` | `download(java.net.URL url, java.io.File targetFile)` Downloads a file from a HTTP server.
    `static boolean` | `download(java.net.URL url, java.io.File targetFile, int timeout)` Downloads a file from a HTTP server.
    `static int` | `encodeMultipleEpisodeNumersToSingleFieldValue(int[] episodeNumbers)` Convert an array with episode numbers into a single integer value.
    `static byte[]` | `getBytesFromFile(java.io.File file)` Reads all Bytes from a File into an byte array
    `static int` | `getMinutesAfterMidnight()` Gets the number of minutes since midnight This method does not create any objects.
    `static java.io.InputStream` | `getStream(java.net.URL page)` Gets an InputStream to the given URL.
    `static java.io.InputStream` | `getStream(java.net.URL page, boolean followRedirects)` Gets an InputStream to the given URL.
    `static java.io.InputStream` | `getStream(java.net.URL page, boolean followRedirects, int timeout)` Originally copied from javax.swing.JEditorPane.
    `static java.io.InputStream` | `getStream(java.net.URL page, boolean followRedirects, int timeout, java.lang.String userName, java.lang.String userPassword)` Originally copied from javax.swing.JEditorPane.
    `static java.io.InputStream` | `getStream(java.net.URL page, int timeout)` Gets an InputStream to the given URL.
    `static byte[]` | `loadFileFromHttpServer(java.net.URL url)` Loads a file from a Http server.
    `static byte[]` | `loadFileFromHttpServer(java.net.URL url, int timeout)` Loads a file from a Http server with the given read timeout.
    `static byte[]` | `loadFileFromJar(java.lang.String fileName, java.lang.Class srcClass)` Lädt eine Datei aus einem Jar-File und gibt sie zurück.
    `static java.io.InputStream` | `openSaveGZipInputStream(java.io.InputStream is)` This method tries to open an inputstream as gzip and uncompresses it.
    `static void` | `pipe(java.io.Reader reader, java.io.Writer writer)` Pipes all data from the specified Reader to the specified Writer, until the Reader has no more data.
    `static boolean` | `pipeStreams(java.io.InputStream from, java.io.OutputStream to)` Pipes all data from the specified InputStream to the specified OutputStream, until the InputStream has no more data.
    `static boolean` | `pipeStreams(java.io.InputStream from, java.io.OutputStream to, int timeout)` Pipes all data from the specified InputStream to the specified OutputStream, until the InputStream has no more data.
    `static byte[]` | `readBinaryData(java.io.InputStream stream, int length)` Reads a number of bytes into an array.
    `static javax.swing.ImageIcon` | `readImageIconFromFile(java.io.File srcFile)` Read the image from the given file to an icon image.
    `static void` | `replace(java.lang.StringBuffer buffer, java.lang.String pattern, java.lang.String str)` Replaces in `buffer` the `pattern` by `str`.
    `static java.lang.String` | `replace(java.lang.String original, java.lang.String pattern, java.lang.String str)` Replaces a substring in the specified String.
    `static boolean` | `saveStream(java.io.InputStream stream, java.io.File targetFile)` Saves the data from an InputStream into a file.
    `static boolean` | `saveStream(java.io.InputStream stream, java.io.File targetFile, int timeout)` Saves the data from an InputStream into a file.
    `static java.lang.String` | `timeToString(int minutesAfterMidnight)` Gets a String representation in the format h:mm for a time in minutes after midnight.
    `static java.lang.String` | `translateRelativePath(java.lang.String path)` Translates a given path that can be relative or absolute to an absolute path.
    `static void` | `ungzip(java.io.File srcFile, java.io.File targetFile)` Unzips a GZIP-file (.gz).
    `static void` | `unzip(java.io.File srcFile, java.lang.String entryName, java.io.File targetFile)` Unzips a file from a ZIP-Archive (.zip, .jar).
    `static boolean` | `writeImageIconToFile(javax.swing.ImageIcon icon, java.lang.String imageType, java.io.File targetFile)` Writes the given image icon to the given file in the given imageType.
    `static java.lang.String` | `xorDecode(java.lang.String text, long seed)` Decodes the specified String using a simple XOR encryption.
    `static java.lang.String` | `xorEncode(java.lang.String text, long seed)` Encodes the specified String using a simple XOR encryption.

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

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

- - ### Constructor Detail

    - #### IOUtilities

          public IOUtilities()

  - ### Method Detail

    - #### download

          public static void download(java.net.URL url,
                      java.io.File targetFile)
                               throws java.io.IOException

      Downloads a file from a HTTP server.
      Parameters:
        `url` - The URL of the file to download.
        `targetFile` - The file where to store the downloaded data.
      Throws:
        `java.io.IOException` - When download or saving failed.
      See Also:
        `loadFileFromHttpServer(URL)`

    - #### download

          public static boolean download(java.net.URL url,
                         java.io.File targetFile,
                         int timeout)
                                  throws java.io.IOException

      Downloads a file from a HTTP server.
      Parameters:
        `url` - The URL of the file to download.
        `targetFile` - The file where to store the downloaded data.
        `timeout` - Timeout in milliseconds.
      Returns:
        `true` the file was successfully downloaded, `false` otherwise.
      Throws:
        `java.io.IOException` - When download or saving failed.
      See Also:
        `loadFileFromHttpServer(URL)`

    - #### saveStream

          public static boolean saveStream(java.io.InputStream stream,
                           java.io.File targetFile)
                                    throws java.io.IOException

      Saves the data from an InputStream into a file.
      Parameters:
        `stream` - The stream to read the data from.
        `targetFile` - The file where to store the data.
      Returns:
        `true` if stream could be saved, `false` otherwise.
      Throws:
        `java.io.IOException` - When saving failed or when the InputStream throws an IOException.

    - #### saveStream

          public static boolean saveStream(java.io.InputStream stream,
                           java.io.File targetFile,
                           int timeout)
                                    throws java.io.IOException

      Saves the data from an InputStream into a file.
      Parameters:
        `stream` - The stream to read the data from.
        `targetFile` - The file where to store the data.
        `timeout` - The timeout in milliseconds.
      Returns:
        `true` if stream could be saved, `false` otherwise.
      Throws:
        `java.io.IOException` - When saving failed or when the InputStream throws an IOException.

    - #### getStream

          public static java.io.InputStream getStream(java.net.URL page,
                                      boolean followRedirects)
                                               throws java.io.IOException

      Gets an InputStream to the given URL.

      The connection has the Settings.propDefaultNetworkConnectionTimeout as connection timeout.

      Parameters:
        `page` - The page to get the stream to.
        `followRedirects` - If the stream should be also established if the page not exists at the location but contains a redirect to an other location.
      Returns:
        The stream to the page.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### getStream

          public static java.io.InputStream getStream(java.net.URL page,
                                      boolean followRedirects,
                                      int timeout)
                                               throws java.io.IOException

      Originally copied from javax.swing.JEditorPane.

      Fetches a stream for the given URL, which is about to be loaded by the `setPage` method. By default, this simply opens the URL and returns the stream. This can be reimplemented to do useful things like fetch the stream from a cache, monitor the progress of the stream, etc.

      This method is expected to have the the side effect of establishing the content type, and therefore setting the appropriate `EditorKit` to use for loading the stream.

      If this the stream was an http connection, redirects will be followed and the resulting URL will be set as the `Document.StreamDescriptionProperty` so that relative URL's can be properly resolved.

      Parameters:
        `page` - the URL of the page
        `followRedirects` - Follow redirects.
        `timeout` - The read timeout.
      Returns:
        a stream reading data from the specified URL.
      Throws:
        `java.io.IOException` - if something went wrong.

    - #### getStream

          public static java.io.InputStream getStream(java.net.URL page,
                                      boolean followRedirects,
                                      int timeout,
                                      java.lang.String userName,
                                      java.lang.String userPassword)
                                               throws java.io.IOException

      Originally copied from javax.swing.JEditorPane.

      Fetches a stream for the given URL, which is about to be loaded by the `setPage` method. By default, this simply opens the URL and returns the stream. This can be reimplemented to do useful things like fetch the stream from a cache, monitor the progress of the stream, etc.

      This method is expected to have the the side effect of establishing the content type, and therefore setting the appropriate `EditorKit` to use for loading the stream.

      If this the stream was an http connection, redirects will be followed and the resulting URL will be set as the `Document.StreamDescriptionProperty` so that relative URL's can be properly resolved.

      Parameters:
        `page` - the URL of the page
        `followRedirects` - Follow redirects.
        `timeout` - The read timeout.
        `userName` - The user name to use for the connection.
        `userPassword` - The password to use for the connection.
      Returns:
        a stream reading data from the specified URL.
      Throws:
        `java.io.IOException` - if something went wrong.

    - #### getStream

          public static java.io.InputStream getStream(java.net.URL page)
                                               throws java.io.IOException

      Gets an InputStream to the given URL.

      The connection has the Settings.propDefaultNetworkConnectionTimeout as connection timeout.

      Parameters:
        `page` - The page to get the stream to.
      Returns:
        The stream to the page.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### getStream

          public static java.io.InputStream getStream(java.net.URL page,
                                      int timeout)
                                               throws java.io.IOException

      Gets an InputStream to the given URL.

      The connection has the given timeout as connection timeout.

      Parameters:
        `page` - The page to get the stream to.
        `timeout` - The timeout for the connection, use 0 for no timeout.
      Returns:
        The stream to the page.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### loadFileFromHttpServer

          public static byte[] loadFileFromHttpServer(java.net.URL url)
                                               throws java.io.IOException

      Loads a file from a Http server.

      The connection has the Settings.propDefaultNetworkConnectionTimeout as connection timeout.

      Parameters:
        `url` - The URL of the file
      Returns:
        The content of the file
      Throws:
        `java.io.IOException` - When the download failed
      See Also:
        `download(URL, File)`

    - #### loadFileFromHttpServer

          public static byte[] loadFileFromHttpServer(java.net.URL url,
                                      int timeout)
                                               throws java.io.IOException

      Loads a file from a Http server with the given read timeout.
      Parameters:
        `url` - The URL of the file
        `timeout` - The read timeout for the connection.
      Returns:
        The content of the file
      Throws:
        `java.io.IOException` - When the download failed
      See Also:
        `download(URL, File)`

    - #### pipeStreams

          public static boolean pipeStreams(java.io.InputStream from,
                            java.io.OutputStream to)
                                     throws java.io.IOException

      Pipes all data from the specified InputStream to the specified OutputStream, until the InputStream has no more data.

      Note: None of the streams is closed! You have to do that for yourself!

      Parameters:
        `from` - The stream to read the data from.
        `to` - The stream to write the data to.
      Returns:
        `true` if stream were successfully piped, `false` otherwise.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### pipeStreams

          public static boolean pipeStreams(java.io.InputStream from,
                            java.io.OutputStream to,
                            int timeout)
                                     throws java.io.IOException

      Pipes all data from the specified InputStream to the specified OutputStream, until the InputStream has no more data.

      Note: None of the streams is closed! You have to do that for yourself!

      Parameters:
        `from` - The stream to read the data from.
        `to` - The stream to write the data to.
        `timeout` - Timeout in milliseconds.
      Returns:
        `true` if stream were successfully piped, `false` otherwise.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### pipe

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

      Pipes all data from the specified Reader to the specified Writer, until the Reader has no more data.

      Note: The Reader and the Writer are not closed! You have to do that for yourself!

      Parameters:
        `reader` - The Reader to read the data from.
        `writer` - The Writer to write the data to.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### copy

          public static void copy(java.io.File[] src,
                  java.io.File targetDir)
                           throws java.io.IOException

      Copies files given in source to the target directory.
      Parameters:
        `src` - The files to copy.
        `targetDir` - The target dir of the files.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### copy

          public static void copy(java.io.File[] src,
                  java.io.File targetDir,
                  boolean onlyNew)
                           throws java.io.IOException

      Copies files given in source to the target directory.
      Parameters:
        `src` - The files to copy.
        `targetDir` - The target dir of the files.
        `onlyNew` - Overwrite only older files.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.
      Since:
        2.2.2/2.5.1

    - #### copy

          public static void copy(java.io.File src,
                  java.io.File target)
                           throws java.io.IOException

      Copies a file.
      Parameters:
        `src` - The file to read from
        `target` - The file to write to
      Throws:
        `java.io.IOException` - If copying failed

    - #### copy

          public static void copy(java.io.File src,
                  java.io.File target,
                  boolean onlyNew)
                           throws java.io.IOException

      Copies a file.
      Parameters:
        `src` - The file to read from
        `target` - The file to write to
        `onlyNew` - Overwrite only older files.
      Throws:
        `java.io.IOException` - If copying failed
      Since:
        2.2.2/2.5.1

    - #### deleteDirectory

          public static void deleteDirectory(java.io.File dir)
                                      throws java.io.IOException

      Deletes a directory with all its files and subdirectories.
      Parameters:
        `dir` - The directory to delete.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### loadFileFromJar

          public static byte[] loadFileFromJar(java.lang.String fileName,
                               java.lang.Class srcClass)
                                        throws java.io.IOException

      Lädt eine Datei aus einem Jar-File und gibt sie zurück.

      Ist keine Datei mit diesem Namen im Jar-File, so wird versucht, sie vom Dateisystem zu laden.

      Parameters:
        `fileName` - Der Name der Datei. (Ist case-sensitive!).
        `srcClass` - Eine Klasse, aus deren Jar-File das Image geladen werden soll.
      Returns:
        The file loaded from the jar file as byte array.
      Throws:
        `java.io.IOException` - Wenn ein Fehler beim Laden der Datei auftrat.

    - #### unzip

          public static void unzip(java.io.File srcFile,
                   java.lang.String entryName,
                   java.io.File targetFile)
                            throws java.io.IOException

      Unzips a file from a ZIP-Archive (.zip, .jar).

      Currently not used.

      Parameters:
        `srcFile` - The ZIP-File to read the data from.
        `entryName` - The name of the file in the ZIP-archive to unzip.
        `targetFile` - The file where to store the data.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### ungzip

          public static void ungzip(java.io.File srcFile,
                    java.io.File targetFile)
                             throws java.io.IOException

      Unzips a GZIP-file (.gz).
      Parameters:
        `srcFile` - The GZIP-File to unzip
        `targetFile` - The file where to store the data.
      Throws:
        `java.io.IOException` - Thrown if something goes wrong.

    - #### append

          public static void append(java.lang.StringBuffer buffer,
                    int number,
                    int minChars)

      Appends an integer to a StringBuffer. If the length of the integer's String representation is smaller than minChars, the missing chars will be filled as nulls ('0') as postfix.
      Parameters:
        `buffer` - The buffer where to append the integer.
        `number` - The integer to append.
        `minChars` - The minimum number of chars.

    - #### replace

          public static java.lang.String replace(java.lang.String original,
                                 java.lang.String pattern,
                                 java.lang.String str)

      Replaces a substring in the specified String.
      Parameters:
        `original` - The String to replace the substring in.
        `pattern` - The pattern to replace.
        `str` - The String to replace. This string may contain the pattern.
      Returns:
        The result.

    - #### replace

          public static void replace(java.lang.StringBuffer buffer,
                     java.lang.String pattern,
                     java.lang.String str)

      Replaces in `buffer` the `pattern` by `str`.
      Parameters:
        `buffer` - The buffer to replace in.
        `pattern` - The pattern to replace.
        `str` - The str that should replace the pattern.

    - #### clear

          public static void clear(java.lang.StringBuffer buffer)

      Clears an StringBuffer
      Parameters:
        `buffer` - The buffer to clear

    - #### getMinutesAfterMidnight

          public static int getMinutesAfterMidnight()

      Gets the number of minutes since midnight

      This method does not create any objects.

      Returns:
        The number of minutes since midnight as integer

    - #### timeToString

          public static java.lang.String timeToString(int minutesAfterMidnight)

      Gets a String representation in the format h:mm for a time in minutes after midnight.
      Parameters:
        `minutesAfterMidnight` - The time to get the String for
      Returns:
        A String for the time

    - #### xorEncode

          public static java.lang.String xorEncode(java.lang.String text,
                                   long seed)

      Encodes the specified String using a simple XOR encryption.
      Parameters:
        `text` - The text to encode
        `seed` - The seed of the Random object to use for getting the keys
      Returns:
        The encoded String

    - #### xorDecode

          public static java.lang.String xorDecode(java.lang.String text,
                                   long seed)

      Decodes the specified String using a simple XOR encryption.
      Parameters:
        `text` - The text to encode
        `seed` - The seed of the Random object to use for getting the keys
      Returns:
        The decoded String

    - #### readBinaryData

          public static byte[] readBinaryData(java.io.InputStream stream,
                              int length)
                                       throws java.io.IOException

      Reads a number of bytes into an array.
      Parameters:
        `stream` - The stream to read from
        `length` - The number of bytes to read
      Returns:
        An array containing the read bytes
      Throws:
        `java.io.IOException` - When the end of the stream has been reached or reading failed

    - #### getBytesFromFile

          public static byte[] getBytesFromFile(java.io.File file)
                                         throws java.io.IOException

      Reads all Bytes from a File into an byte array
      Parameters:
        `file` - Read Bytes from this File
      Returns:
        Byte-Array or NULL if too large
      Throws:
        `java.io.IOException` - Read-Exception
      Since:
        2.5

    - #### writeImageIconToFile

          public static boolean writeImageIconToFile(javax.swing.ImageIcon icon,
                                     java.lang.String imageType,
                                     java.io.File targetFile)

      Writes the given image icon to the given file in the given imageType.
      Parameters:
        `icon` - The icon to write.
        `imageType` - The image type.
        `targetFile` - The file to write the image to.
      Returns:
        `True` if the file could be written, `false` if something went wrong.
      Since:
        2.6

    - #### readImageIconFromFile

          public static javax.swing.ImageIcon readImageIconFromFile(java.io.File srcFile)

      Read the image from the given file to an icon image.
      Parameters:
        `srcFile` - The file to read from.
      Returns:
        The read icon image or `null` if something went wrong.
      Since:
        2.6

    - #### openSaveGZipInputStream

          public static java.io.InputStream openSaveGZipInputStream(java.io.InputStream is)
                                                             throws java.io.IOException

      This method tries to open an inputstream as gzip and uncompresses it. If it fails, a normal inputstream is returned
      Parameters:
        `is` - Inputstream that could be compressed
      Returns:
        uncompressed inputstream
      Throws:
        `java.io.IOException` - Problems during opening of the Stream
      Since:
        3.0

    - #### encodeMultipleEpisodeNumersToSingleFieldValue

          public static int encodeMultipleEpisodeNumersToSingleFieldValue(int[] episodeNumbers)
                                                                   throws javax.activation.UnsupportedDataTypeException

      Convert an array with episode numbers into a single integer value. The array can have a length of 1 with a maximum number of 536870912, or a length of 2 with a maximum number of 16384 for the first and second episode, or a length of 3 with a maximum number of 16384 for the first episode and a maximum difference of 128 between following episodes, or a length of 4 with a maximum number of 16384 for the first episode and a maximum difference of 16 between following episodes, or a length of 5 with a maximum number of 16384 for the first episode and a maximum difference of 8 between following episodes.

      Parameters:
        `episodeNumbers` - The array with the episode number to encode.
      Returns:
        The encoded episode number.
      Throws:
        `javax.activation.UnsupportedDataTypeException` - Is thrown if the given array didn't match the given conditions.
      Since:
        3.3.3.

    - #### decodeSingleFieldValueToMultipleEpisodeNumers

          public static java.lang.Integer[] decodeSingleFieldValueToMultipleEpisodeNumers(int fieldValue)

      Decode the given value into an array of episode numbers.

      Parameters:
        `fieldValue` - The field value to decode.
      Returns:
        An array with the contained episode numbers.
      Since:
        3.3.3.

    - #### decodeSingleFieldValueToMultipleEpisodeString

          public static java.lang.String decodeSingleFieldValueToMultipleEpisodeString(int fieldValue)

      Decode the given value into a String of episode numbers.

      Parameters:
        `fieldValue` - The field value to decode.
      Returns:
        A String of episode numbers.
      Since:
        3.3.3.

    - #### checkForRelativePath

          public static java.lang.String checkForRelativePath(java.lang.String path)

      Check given path if it is a relative path of the TV-Browser settings home directory.

      Parameters:
        `path` - The path to check.
      Returns:
        A relative path if given path is in subdirectory of TV-Browser settings or the given path if not.

    - #### translateRelativePath

          public static java.lang.String translateRelativePath(java.lang.String path)

      Translates a given path that can be relative or absolute to an absolute path.

      Parameters:
        `path` - The path to check and translate to an absolute path.
      Returns:
        A full path.
      Since:
        3.3.4

TV-Browser 3.4.3, Copyright (C) 2004-2008 TV-Browser Team (dev@tvbrowser.org)