Class RereadableContentBuilder
- All Implemented Interfaces:
Closeable,AutoCloseable
RereadableContentBuilder provides the means for creating
RereadableContent. It accepts data via an OutputStream or an
InputStream (or both) and provides a RereadableContent for this. The data is
either stored in memory or in a temporary file depending on the size. So the configuration of a
RereadableContentBuilder 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 of RereadableContent. For
this provide an input stream from an existing RereadableContent without any
additional data. This will ignore the file configuration of the
RereadableContentBuilde and return a lightweight RereadableContent 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Cleanup<IOException>The clean-up called as post-mortem action of thisRereadableContentBuilderclosing the streams (if not used) and deleting the temporary file if appropriate.protected final longThe estimated size of (the contents of) the created stream, a negative value if the size is unknown.protected static final FileThe JVM-wide directory for temporary files.protected static final DateTimeFormatterThe format for the date/time used for the names of temporary files forRereadableContent. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newRereadableContentBuilderwith 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 newRereadableContentBuilderwith 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
Modifier and TypeMethodDescriptionbuild()Builds aRereadableContentfrom the current configuration and provided data.final voidclose()data(InputStream data) Sets the data to the designated one or appends this data to the output stream when building theRereadableContent.Gets a new existing output stream for writing viaOutputStreaminstead or additionally toproviding an input stream.Sets the directory to store the temporary file.tempFileTag(String tempFileTag) Sets a part of the file name of the created temporary file to identify it.
-
Field Details
-
TIMESTAMP_FORMATTER
The format for the date/time used for the names of temporary files forRereadableContent. This helps to identify the files. -
TEMP_DIR
The JVM-wide directory for temporary files. -
size
protected final long sizeThe estimated size of (the contents of) the created stream, a negative value if the size is unknown. -
cleanup
The clean-up called as post-mortem action of thisRereadableContentBuilderclosing the streams (if not used) and deleting the temporary file if appropriate.
-
-
Constructor Details
-
RereadableContentBuilder
public RereadableContentBuilder()Creates a newRereadableContentBuilderwith 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 newRereadableContentBuilderwith 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-1or0. 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 Details
-
tempDir
Sets the directory to store the temporary file.nullresets 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 thisRereadableContenBuilderhas already been closed or anoutput streamhas already been created or this has been already closed or the designated directory refers to an existing file, anIllegalArgumentExceptionwill be thrown.
-
tempFileTag
Sets a part of the file name of the created temporary file to identify it.nullremoves 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 thisRereadableContenBuilderhas already been closed or anoutput streamhas already been created or this has been already closed or the designated directory refers to an existing file, anIllegalArgumentExceptionwill be thrown.
-
data
Sets the data to the designated one or appends this data to the output stream when building theRereadableContent.nullwill remove this (additional) data.- Parameters:
data- The data for theRereadableContentto be appended to the output stream ornullto remove any previously set data. The stream will be closed afterbuilding(or when closing this builder).- Returns:
- This
RereadableContentBuilder. - Throws:
IllegalArgumentException- If thisRereadableContenBuilderhas already been closed or anoutput streamexists and it has been already closed, anIllegalArgumentExceptionwill be thrown.
-
build
Builds aRereadableContentfrom the current configuration and provided data. If aRereadableContenthas already been built, this instance will be returned.
Note that after theRereadableContenthas 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.RrcInputStreamwhich wraps an existingRereadableContent. In this case the file configuration will be ignored and the createdRereadableContentwill 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 aRrcInputStreamwill be appended and its wrappedRereadableContentwill be ignored.- Returns:
- A new (or previously built)
RereadableContentcontaining 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, anIOExceptionwill be thrown.
-
getOutputStream
Gets a new existing output stream for writing viaOutputStreaminstead 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 theRereadableContentBuilderhas already been closed, there are problems creating the output stream or an output stream already exists, anIOExceptionwill be thrown.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-