Class RereadableContentBuilder
- java.lang.Object
-
- de.aristaflow.adept2.util.io.RereadableContentBuilder
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class RereadableContentBuilder extends Object implements Closeable
ARereadableContentBuilderprovides the means for creatingRereadableContent. It accepts data via anOutputStreamor anInputStream(or both) and provides aRereadableContentfor this. The data is either stored in memory or in a temporary file depending on the size. So the configuration of aRereadableContentBuilderconsists 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
RereadableContentBuilderprovides wrapping ofRereadableContent. For this provide an input stream from an existingRereadableContentwithout any additional data. This will ignore the file configuration of theRereadableContentBuildeand return a lightweightRereadableContentthat 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 classRereadableContentBuilder.RcbCleanup
-
Field Summary
Fields Modifier and Type Field Description protected Cleanup<IOException>cleanupThe clean-up called as post-mortem action of thisRereadableContentBuilderclosing the streams (if not used) and deleting the temporary file if appropriate.protected longsizeThe estimated size of (the contents of) the created stream, a negative value if the size is unknown.protected static FileTEMP_DIRThe JVM-wide directory for temporary files.protected static DateTimeFormatterTIMESTAMP_FORMATTERThe format for the date/time used for the names of temporary files forRereadableContent.
-
Constructor Summary
Constructors Constructor Description 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.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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RereadableContentbuild()Builds aRereadableContentfrom the current configuration and provided data.voidclose()RereadableContentBuilderdata(InputStream data)Sets the data to the designated one or appends this data to the output stream when building theRereadableContent.OutputStreamgetOutputStream()Gets a new existing output stream for writing viaOutputStreaminstead or additionally toproviding an input stream.RereadableContentBuildertempDir(File tempDir)Sets the directory to store the temporary file.RereadableContentBuildertempFileTag(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 thisRereadableContentBuilderclosing the streams (if not used) and deleting the temporary file if appropriate.
-
-
Constructor Detail
-
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 Detail
-
tempDir
public RereadableContentBuilder tempDir(File 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
public RereadableContentBuilder tempFileTag(String 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
public RereadableContentBuilder data(InputStream 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
public RereadableContent build() throws IOException
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
public OutputStream getOutputStream() throws IOException
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
public final void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
-