Class IOUtils


  • public final class IOUtils
    extends Object
    • Method Detail

      • discardStream

        public static void discardStream​(InputStream is)
                                  throws IOException
        Copies all data from the input stream. Does NOT close the input stream.
        Throws:
        IOException
      • deleteRecursively

        public static void deleteRecursively​(File file)
        Recursively delete a file or directory.
      • deleteAllBelow

        public static void deleteAllBelow​(File file)
        Recursively delete everything below a given directory, but don't remove the directory itself.
      • determineLength

        public static long determineLength​(SeekableInputStream is)
                                    throws IOException
        Determines and returns the stream length either by checking whether the stream returns a reasonable length (some instances may not be able to do that) or just reading over it, until an EOF is hit. Users of given stream need to deal with synchronization against other users of the same stream instance.
        Parameters:
        is - the stream to determine its length
        Returns:
        the length, the length returned by SeekableInputStream.length() if positive, or the length of the stream determined by reading over it. the length is measured in bytes.
        Throws:
        IOException - if an I/O error occurs.
      • forcedDetermineLength

        public static long forcedDetermineLength​(SeekableInputStream is)
                                          throws IOException
        Determines and returns the stream length by reading over the complete stream until an EOF was found.

        Note: Users of given stream need to deal with synchronization against other users of the same stream instance.

        Parameters:
        is - the stream to determine its length
        Returns:
        the length of the stream determined by reading over it. The length is measured in bytes.
        Throws:
        IOException - if an I/O error occurs.
      • drainSynchronously

        public static void drainSynchronously​(InputStream is)
        Eagerly drain the given input stream. This method will block until all data has been read and the stream's EOF has been reached.
      • drainAsynchronously

        public static void drainAsynchronously​(InputStream is)
        Eagerly drain the given input stream. Draining will be performed in the background. This method will synchronize on the InputStream while doing block-wise reads.
      • wrap

        public static final SeekableInputStream wrap​(InputStream inputStream,
                                                     boolean tryLocalTmpFileAccess)
                                              throws IOException
        Wraps the given stream into SeekableInputStream. Checks the given input stream and if necessary wraps it with an appropriate SeekableInputStream, which is returned. If tryLocalTmpFileAccess is true, this method will try to buffer all data into a temporary file if possible. If additionally a BufferManager is available on the classpath and temporary buffering is possible, the buffering of data will be handled by the BufferManager. If temporary buffering is not possible (e.g. due to security restrictions), all data will be buffered in memory.
        Parameters:
        inputStream - the stream to wrap
        tryLocalTmpFileAccess - enable/disable temporary file data buffering.
        Returns:
        a SeekableInputStream instance wrapping the original InputStream
        Throws:
        IOException - if an i/o error occurred
        IllegalArgumentException - if the given InputStream is null
      • wrap

        public static final SeekableInputStream wrap​(InputStream inputStream,
                                                     boolean tryLocalTmpFileAccess,
                                                     boolean ignoreSeekableStream)
                                              throws IOException
        Wraps the given stream into SeekableInputStream. Checks the given input stream and if necessary wraps it with an appropriate SeekableInputStream, which is returned. If tryLocalTmpFileAccess is true, this method will try to buffer all data into a temporary file if possible. If additionally a BufferManager is available on the classpath and temporary buffering is possible, the buffering of data will be handled by the BufferManager. If temporary buffering is not possible (e.g. due to security restrictions), all data will be buffered in memory.
        Parameters:
        inputStream - the stream to wrap
        tryLocalTmpFileAccess - enable/disable temporary file data buffering.
        ignoreSeekableStream - if the given inputStream is already an instance of SeekableInputStream it will be ignored and not wrapped any further if this flag is set to true
        Returns:
        a SeekableInputStream instance wrapping the original InputStream
        Throws:
        IOException - if an i/o error occurred
        IllegalArgumentException - if the given InputStream is null
      • setTryFileCaching

        public static void setTryFileCaching​(boolean tryFileCaching)
        Set whether to try to cache data in local files by default. This value is used by wrap(InputStream).
        Parameters:
        tryFileCaching - true if local file caching should be tried by default
      • isTryFileCaching

        public static boolean isTryFileCaching()
        Return whether to try to cache data in local files by default.
        Returns:
        true if local file caching is tried by default
      • isBufferManagerActive

        public static boolean isBufferManagerActive()
        Checks if BufferManager is available and enabled
        Returns:
        true if BufferManager is available and enabled