public class StreamUtils extends Object
| Constructor and Description |
|---|
StreamUtils() |
| Modifier and Type | Method and Description |
|---|---|
static long |
copyAndClose(InputStream is,
OutputStream os)
Copy from `is` to `os` and close the streams regardless of the result.
|
static long |
copyToFileAndClose(InputStream is,
File file)
Copy from an input stream to a file (and buffer it) and close the input stream.
|
static long |
copyToFileAndClose(InputStream is,
File file,
long timeout)
Copy bytes from `is` to `file` but timeout if the copy takes too long.
|
static long |
copyWithTimeout(InputStream is,
OutputStream os,
long timeout)
Copy from the input stream to the output stream and tries to exit if the copy exceeds the timeout.
|
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.
|
public static long copyToFileAndClose(InputStream is, File file) throws IOException
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.IOExceptionpublic static long copyToFileAndClose(InputStream is, File file, long timeout) throws IOException, TimeoutException
is - The `InputStream` to copy bytes from. It is closed regardless of copy results.file - The `File` to copy bytes totimeout - The timeout (in ms) of the copy.IOExceptionTimeoutException - If `timeout` is exceededpublic static long copyAndClose(InputStream is, OutputStream os) throws IOException
is - The `InputStream` to copy results from. It is closedos - The `OutputStream` to copy results to. It is closedIOExceptionpublic static long copyWithTimeout(InputStream is, OutputStream os, long timeout) throws IOException, TimeoutException
is - The input stream to read bytes from.os - The output stream to write bytes to.timeout - The timeout (in ms) for the copy operationIOExceptionTimeoutException - If `tiemout` is exceededpublic static long retryCopy(com.google.common.io.ByteSource byteSource,
com.google.common.io.ByteSink byteSink,
com.google.common.base.Predicate<Throwable> shouldRetry,
int maxAttempts)
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 retrymaxAttempts - Maximum number of retries before failingCopyright © 2011–2017. All rights reserved.