Package de.aristaflow.adept2.util.io
Class BufferedRereadableContent
- java.lang.Object
-
- de.aristaflow.adept2.util.io.BufferedRereadableContent
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class BufferedRereadableContent extends Object implements Closeable
TheBufferedRereadableContent
can be used when the content of anInputStream
needs to be processed multiple times. Depending on its eventual size the whole content of the input stream is buffered in-memory - up to a certain threshold - or in a temporary file.The
BufferedRereadableContent
as well as the provided input streams must beclosed
after use! This can be ensured by adhering to the following template:try (TemporaryRereadableInput input = new TemporaryRereadableInput(inputStream)) { ... try (InputStream is = input.getInputStream()) { ... } ... try (InputStream is = input.getInputStream()) { ... } ... }
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_THRESHOLD
-
Constructor Summary
Constructors Constructor Description BufferedRereadableContent(InputStream inputStream)
Creates a newBufferedRereadableContent
and immediately reads the givenInputStream
.BufferedRereadableContent(InputStream inputStream, int threshold, String tempFileTag, File tempDir)
Creates a newBufferedRereadableContent
and immediately reads the givenInputStream
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Releases all resources and deletes the temporary file if one has been created.InputStream
getInputStream()
Returns an input stream for the data.long
getLength()
Returns the total length of the content.
-
-
-
Field Detail
-
DEFAULT_THRESHOLD
public static final int DEFAULT_THRESHOLD
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BufferedRereadableContent
public BufferedRereadableContent(InputStream inputStream) throws IOException
Creates a newBufferedRereadableContent
and immediately reads the givenInputStream
.- Parameters:
inputStream
- the input stream to read- Throws:
IOException
- if an I/O error occurs
-
BufferedRereadableContent
public BufferedRereadableContent(InputStream inputStream, int threshold, String tempFileTag, File tempDir) throws IOException
Creates a newBufferedRereadableContent
and immediately reads the givenInputStream
.ByteArrayInputStreams
will receive special treatment to avoid creating a duplicate of their content.- Parameters:
inputStream
- the input stream to readthreshold
- the number of bytes after which to store the content in a file instead; negative values will result in thedefault threshold
tempFileTag
- an optional tag that will be incorporated into the name of the temp file if one is created; may be nulltempDir
- the directory where temp files should be created; may be null to use the default temp directory of the JVM; will be created if it doesn't exist- Throws:
IOException
- if an I/O error occurs
-
-
Method Detail
-
getLength
public long getLength()
Returns the total length of the content.- Returns:
- the total length of the content
-
getInputStream
public InputStream getInputStream() throws IOException
Returns an input stream for the data. This method may be called multiple times. But not while previously acquired streams are still being used. The returned stream must be closed properly after use!- Returns:
- an input stream for the data
- Throws:
IOException
- if an I/O error occurs
-
close
public void close() throws IOException
Releases all resources and deletes the temporary file if one has been created.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if an I/O error occurs
-
-