Class RereadableContentBuilder
- java.lang.Object
-
- de.aristaflow.adept2.util.io.RereadableContentBuilder
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class RereadableContentBuilder extends Object implements Closeable
ARereadableContentBuilder
provides the means for creatingRereadableContent
. It accepts data via anOutputStream
or anInputStream
(or both) and provides aRereadableContent
for this. The data is either stored in memory or in a temporary file depending on the size. So the configuration of aRereadableContentBuilder
consists of the (expected) size of the data, directory and hints for the name for creating the temporary file.
When using an output stream, the file configuration cannot be changed after creating the output stream. An input stream can be provided (or changed) until building. If there is data provided via output and input stream, the input stream data will be appended to the output stream. This requires to output stream to be unclosed.A
RereadableContentBuilder
provides wrapping ofRereadableContent
. For this provide an input stream from an existingRereadableContent
without any additional data. This will ignore the file configuration of theRereadableContentBuilde
and return a lightweightRereadableContent
that will use the wrapped one for content handling. When providing additional data via output stream, the input stream will be handled like a normal input stream, that is, its data will be appended and the file configuration will apply.This class is not thread-safe, so build only from the very same thread.
This class is inspired by
DeferredFileOutputStream
.- See Also:
StreamTools.getMemStreamThreshold()
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
RereadableContentBuilder.RcbCleanup
-
Field Summary
Fields Modifier and Type Field Description protected Cleanup<IOException>
cleanup
The clean-up called as post-mortem action of thisRereadableContentBuilder
closing the streams (if not used) and deleting the temporary file if appropriate.protected long
size
The estimated size of (the contents of) the created stream, a negative value if the size is unknown.protected static File
TEMP_DIR
The JVM-wide directory for temporary files.protected static DateTimeFormatter
TIMESTAMP_FORMATTER
The format for the date/time used for the names of temporary files forRereadableContent
.
-
Constructor Summary
Constructors Constructor Description RereadableContentBuilder()
Creates a newRereadableContentBuilder
with the file having a (unique) standard name containing creation date/time and being stored in the system default directory for temporary files unless reconfigured.RereadableContentBuilder(long size)
Creates a newRereadableContentBuilder
with the file having a (unique) standard name containing creation date/time and being stored in the system default directory for temporary files unless reconfigured.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RereadableContent
build()
Builds aRereadableContent
from the current configuration and provided data.void
close()
RereadableContentBuilder
data(InputStream data)
Sets the data to the designated one or appends this data to the output stream when building theRereadableContent
.OutputStream
getOutputStream()
Gets a new existing output stream for writing viaOutputStream
instead or additionally toproviding an input stream
.RereadableContentBuilder
tempDir(File tempDir)
Sets the directory to store the temporary file.RereadableContentBuilder
tempFileTag(String tempFileTag)
Sets a part of the file name of the created temporary file to identify it.
-
-
-
Field Detail
-
TIMESTAMP_FORMATTER
protected static final DateTimeFormatter TIMESTAMP_FORMATTER
The format for the date/time used for the names of temporary files forRereadableContent
. This helps to identify the files.
-
TEMP_DIR
protected static final File TEMP_DIR
The JVM-wide directory for temporary files.
-
size
protected final long size
The estimated size of (the contents of) the created stream, a negative value if the size is unknown.
-
cleanup
protected final Cleanup<IOException> cleanup
The clean-up called as post-mortem action of thisRereadableContentBuilder
closing the streams (if not used) and deleting the temporary file if appropriate.
-
-
Constructor Detail
-
RereadableContentBuilder
public RereadableContentBuilder()
Creates a newRereadableContentBuilder
with the file having a (unique) standard name containing creation date/time and being stored in the system default directory for temporary files unless reconfigured. It will switch from memory data to file storing as soon as its size reaches theStreamTools.getMemStreamThreshold()
.
-
RereadableContentBuilder
public RereadableContentBuilder(long size)
Creates a newRereadableContentBuilder
with the file having a (unique) standard name containing creation date/time and being stored in the system default directory for temporary files unless reconfigured. It is either stored in memory or in file storage depending on the designated size and theStreamTools.getMemStreamThreshold()
. If a valid size (>= 0
) is provided the storage will be used right from the beginning and the threshold will be set to either-1
or0
. Otherwise storage may change when writing the data.- Parameters:
size
- The estimated size of (the contents of) the created stream. Use a negative value if the size is unknown.
-
-
Method Detail
-
tempDir
public RereadableContentBuilder tempDir(File tempDir)
Sets the directory to store the temporary file.null
resets the previously set directory and uses the standard temp directory.- Parameters:
tempDir
- The directory to store the temporary file. If this isnull
, the default directory for temporary files of the system will be used.- Returns:
- This
RereadableContentBuilder
. - Throws:
IllegalArgumentException
- If thisRereadableContenBuilder
has already been closed or anoutput stream
has already been created or this has been already closed or the designated directory refers to an existing file, anIllegalArgumentException
will be thrown.
-
tempFileTag
public RereadableContentBuilder tempFileTag(String tempFileTag)
Sets a part of the file name of the created temporary file to identify it.null
removes the previously set part of the file name. Obviously, the files cannot be distinguished based on their usage then.- Parameters:
tempFileTag
- Part of the file name of the created temporary file. If this isnull
, no special tag will be used.- Returns:
- This
RereadableContentBuilder
. - Throws:
IllegalArgumentException
- If thisRereadableContenBuilder
has already been closed or anoutput stream
has already been created or this has been already closed or the designated directory refers to an existing file, anIllegalArgumentException
will be thrown.
-
data
public RereadableContentBuilder data(InputStream data)
Sets the data to the designated one or appends this data to the output stream when building theRereadableContent
.null
will remove this (additional) data.- Parameters:
data
- The data for theRereadableContent
to be appended to the output stream ornull
to remove any previously set data. The stream will be closed afterbuilding
(or when closing this builder).- Returns:
- This
RereadableContentBuilder
. - Throws:
IllegalArgumentException
- If thisRereadableContenBuilder
has already been closed or anoutput stream
exists and it has been already closed, anIllegalArgumentException
will be thrown.
-
build
public RereadableContent build() throws IOException
Builds aRereadableContent
from the current configuration and provided data. If aRereadableContent
has already been built, this instance will be returned.
Note that after theRereadableContent
has been closed, the returned one here will also be closed.Building will write provided data to a new or an existing output stream. If such an output stream exists, it must not be closed. It may contain data, to which the input stream data will be appended. When building the output stream will be closed and its content (usually
byte[]
orPath
) will be used to create a newRereadableContent
.
A special case is the provision of aRereadableContent.RrcInputStream
which wraps an existingRereadableContent
. In this case the file configuration will be ignored and the createdRereadableContent
will simply wrap the provided one and delegate input stream retrieval. This avoids duplicating the content while correctly tracking input stream creation Obviously, with existing content in the output stream, the content of aRrcInputStream
will be appended and its wrappedRereadableContent
will be ignored.- Returns:
- A new (or previously built)
RereadableContent
containing the content provided to thisRereadableContentBuilder
. - Throws:
IOException
- If the builder is in a state that does not allow for building, creating the file for storing or one of its directories or transferring thedata(java.io.InputStream)
fails or there are problems creating theRereadableContent
, anIOException
will be thrown.
-
getOutputStream
public OutputStream getOutputStream() throws IOException
Gets a new existing output stream for writing viaOutputStream
instead or additionally toproviding an input stream
. This method can only be called once to prevent concurrent writing of the output stream.- Returns:
- The output stream accepting the content of the
RereadableContent
. - Throws:
IOException
- If theRereadableContentBuilder
has already been closed, there are problems creating the output stream or an output stream already exists, anIOException
will be thrown.
-
close
public final void close() throws IOException
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
-