Class StreamUtils


  • public class StreamUtils
    extends Object
    • Constructor Detail

      • StreamUtils

        public StreamUtils()
    • Method Detail

      • copyToFileAndClose

        public static long copyToFileAndClose​(InputStream is,
                                              File file)
                                       throws IOException
        Copy from an input stream to a file (and buffer it) and close the input stream.

        It is highly recommended to use FileUtils.retryCopy whenever possible, and not use a raw `InputStream`

        Parameters:
        is - The input stream to copy bytes from. `is` is closed regardless of the copy result.
        file - The file to copy bytes to. Any parent directories are automatically created.
        Returns:
        The count of bytes written to the file
        Throws:
        IOException
      • copyAndClose

        public static long copyAndClose​(InputStream is,
                                        OutputStream os)
                                 throws IOException
        Copy from `is` to `os` and close the streams regardless of the result.
        Parameters:
        is - The `InputStream` to copy results from. It is closed
        os - The `OutputStream` to copy results to. It is closed
        Returns:
        The count of bytes written to `os`
        Throws:
        IOException
      • retryCopy

        public static long retryCopy​(com.google.common.io.ByteSource byteSource,
                                     com.google.common.io.ByteSink byteSink,
                                     com.google.common.base.Predicate<Throwable> shouldRetry,
                                     int maxAttempts)
        Retry copy attempts from input stream to output stream. Does *not* check to make sure data was intact during the transfer
        Parameters:
        byteSource - Supplier for input streams to copy from. The stream is closed on every retry.
        byteSink - Supplier for output streams. The stream is closed on every retry.
        shouldRetry - Predicate to determine if the throwable is recoverable for a retry
        maxAttempts - Maximum number of retries before failing