Interface DataFetcher
-
- All Known Implementing Classes:
AbstractBufferedDataFetcher,AbstractDataFetcher,BufferedDataFetcher
public interface DataFetcherADataFetchercan fetch the entire content of a givenInputStreaminto a freshbyte[]or copy it into a givenOutputStream.It is, among other things, intended as a replacement for
IOUtils.toByteArray()to perform the everyday and menial task of reading an entireInputStream.Since the class
sun.misc.IOUtils, which provides this functionality, is not a part of the official Java specification, usages of this class will usually yield a compiler warning like "The type 'IOUtils' is not API" and might break at runtime, if a runtime environment other than the Oracle JRE is used.All methods take optional parameters to specify the buffer size and to specify, whether the provided streams should be closed afterwards. This allows to write compact code like
{ foo.setContent(fetcher.fetch(new FileInputStream(file), true)); }instead of unnecessarily verbose code like{ InputStream in = new FileInputStream(file); foo.setContent(fetcher.fetch(in)); in.close(); }- Since:
- 4.0.0
- Author:
- Torsten Krause (tk at markenwerk dot net)
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidcopy(InputStream in, OutputStream out)Copies the content of a givenInputStreaminto a givenOutputStream.voidcopy(InputStream in, OutputStream out, boolean closeIn, boolean closeOut)Copies the content of a givenInputStreaminto a givenOutputStream.voidcopy(InputStream in, OutputStream out, DataFetchProgressListener listener)Copies the content of a givenInputStreaminto a givenOutputStream.voidcopy(InputStream in, OutputStream out, DataFetchProgressListener listener, boolean closeIn, boolean closeOut)Copies the content of a givenInputStreaminto a givenOutputStream.byte[]fetch(InputStream in)Fetches the content of a givenInputStreaminto a fresh byte[].byte[]fetch(InputStream in, boolean close)Fetches the content of a givenInputStreaminto a fresh byte[].byte[]fetch(InputStream in, DataFetchProgressListener listener)Fetches the content of a givenInputStreaminto a fresh byte[].byte[]fetch(InputStream in, DataFetchProgressListener listener, boolean close)Fetches the content of a givenInputStreaminto a fresh byte[].
-
-
-
Method Detail
-
fetch
byte[] fetch(InputStream in) throws DataFetchException
Fetches the content of a givenInputStreaminto a fresh byte[].See
copy(InputStream, OutputStream, boolean, boolean)for the handling of missing or invalid arguments.- Parameters:
in- TheInputStreamto read from.- Returns:
- A new
byte[], containing the content of the givenInputStream. - Throws:
DataFetchException- If anything went wrong while reading from the givenInputStream.
-
fetch
byte[] fetch(InputStream in, boolean close) throws DataFetchException
Fetches the content of a givenInputStreaminto a fresh byte[].See
copy(InputStream, OutputStream, boolean, boolean)for the handling of missing or invalid arguments.- Parameters:
in- TheInputStreamto read from.close- Whether to close the givenInputStream, after reading from it.- Returns:
- A new
byte[], containing the content of the givenInputStream. - Throws:
DataFetchException- If anything went wrong while reading from the givenInputStream.FetchExceptionsthrown while trying to close the givenInputStream, if requested, are ignored.
-
fetch
byte[] fetch(InputStream in, DataFetchProgressListener listener, boolean close) throws DataFetchException
Fetches the content of a givenInputStreaminto a fresh byte[].See
copy(InputStream, OutputStream, boolean, boolean)for the handling of missing or invalid arguments.- Parameters:
in- TheInputStreamto read from.listener- TheDataFetchProgressListenerto report to.close- Whether to close the givenInputStream, after reading from it.- Returns:
- A new
byte[], containing the content of the givenInputStream. - Throws:
DataFetchException- If anything went wrong while reading from the givenInputStream.FetchExceptionsthrown while trying to close the givenInputStream, if requested, are ignored.
-
fetch
byte[] fetch(InputStream in, DataFetchProgressListener listener) throws DataFetchException
Fetches the content of a givenInputStreaminto a fresh byte[].See
copy(InputStream, OutputStream, boolean, boolean)for the handling of missing or invalid arguments.- Parameters:
in- TheInputStreamto read from.listener- TheDataFetchProgressListenerto report to.- Returns:
- A new
byte[], containing the content of the givenInputStream. - Throws:
DataFetchException- If anything went wrong while reading from the givenInputStream.
-
copy
void copy(InputStream in, OutputStream out) throws DataFetchException
Copies the content of a givenInputStreaminto a givenOutputStream.See
copy(InputStream, OutputStream, boolean, boolean)for the handling of missing or invalid arguments.- Parameters:
in- TheInputStreamto read from.out- TheOutputStreamto write to.- Throws:
DataFetchException- If anything went wrong while reading from the givenInputStreamor writing to the givenOutputStream.
-
copy
void copy(InputStream in, OutputStream out, boolean closeIn, boolean closeOut) throws DataFetchException
Copies the content of a givenInputStreaminto a givenOutputStream.Missing or invalid arguments are handled gracefully with the following behaviour.
A
nullis given as anInputStream, it is simply ignored and handled as if there was nothing to read. IfcloseOutis true, the givenOutputStreamwill be closed anywayA
nullis given as anOutputStream, it is simply ignored, but the content of givenInputStreamis fetched anyway. IfcloseInis true, the givenInputStreamwill be closed anyway- Parameters:
in- TheInputStreamto read from.out- TheOutputStreamto write to.closeIn- Whether to close the givenInputStream, after reading from it.closeOut- Whether to close the givenOutputStream, after writing to it.- Throws:
DataFetchException- If anything went wrong while reading from the givenInputStreamor writing to the givenOutputStream.FetchExceptionsthrown while trying to close one of the given streams, if requested, are ignored.
-
copy
void copy(InputStream in, OutputStream out, DataFetchProgressListener listener) throws DataFetchException
Copies the content of a givenInputStreaminto a givenOutputStream.See
copy(InputStream, OutputStream, boolean, boolean)for the handling of missing or invalid arguments.- Parameters:
in- TheInputStreamto read from.out- TheOutputStreamto write to.listener- TheDataFetchProgressListenerto report to.- Throws:
DataFetchException- If anything went wrong while reading from the givenInputStreamor writing to the givenOutputStream.
-
copy
void copy(InputStream in, OutputStream out, DataFetchProgressListener listener, boolean closeIn, boolean closeOut) throws DataFetchException
Copies the content of a givenInputStreaminto a givenOutputStream.Missing or invalid arguments are handled gracefully with the following behaviour.
A
nullis given as anInputStream, it is simply ignored and handled as if there was nothing to read. IfcloseOutis true, the givenOutputStreamwill be closed anywayA
nullis given as anOutputStream, it is simply ignored, but the content of givenInputStreamis fetched anyway. IfcloseInis true, the givenInputStreamwill be closed anyway- Parameters:
in- TheInputStreamto read from.out- TheOutputStreamto write to.listener- TheDataFetchProgressListenerto report to.closeIn- Whether to close the givenInputStream, after reading from it.closeOut- Whether to close the givenOutputStream, after writing to it.- Throws:
DataFetchException- If anything went wrong while reading from the givenInputStreamor writing to the givenOutputStream.FetchExceptionsthrown while trying to close one of the given streams, if requested, are ignored.
-
-