Package de.aristaflow.adept2.util
Class StreamTools
- java.lang.Object
-
- de.aristaflow.adept2.util.StreamTools
-
public class StreamTools extends Object
InputStream
andOutputStream
related tool methods.
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_STREAM_THRESHOLD_TOTAL_MEM_PERTHOUSAND
The default value for the per thousand of total memory that is used as threshold distinguishing between memory and file storage for a stream.static String
PROPERTY_SKIP_CLOSING
Deprecated, for removal: This API element is subject to removal in a future version.Do only use if absolutely required.static String
PROPERTY_TOTAL_MEM_PERTHOUSAND_STREAM_THRESHOLD
The string for the name of the property to set the per thousand of total memory that is used as threshold distinguishing between memory and file storage for a stream.
-
Constructor Summary
Constructors Constructor Description StreamTools()
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <T> T
clone(T object, Class<T> cls)
Clone the designated object either viaTypedCloneable.clone()
or in case of incompatible type viacloneViaStream(Serializable, Class)
.static <T extends Serializable>
TcloneViaByteArray(Serializable object, Class<T> clazz)
Deprecated, for removal: This API element is subject to removal in a future version.UsecloneViaStream(Serializable, Class)
instead.static <T> T
cloneViaStream(Serializable object, Class<T> clazz)
Clone the designated object byserialising it to stream
anddeserialising
it again.static byte[]
copyStreamToByteArray(InputStream in)
Copy all content of the given InputStream to a new byte array.static void
copyStreamToStream(InputStream in, OutputStream out)
Deprecated.UseInputStream.transferTo(OutputStream)
instead.static InputStream
fromBase64(InputStream base64)
Gets an input stream decoding Base64 content from the designatedInputStream
and deflating it from gzip if appropriate.static InputStream
fromBase64(String base64)
Gets an input stream containing the decoded (and possibly deflated) content of the designated string which is expected to be Base64 possibly gzipped before encoding.static Serializable
fromByteArray(byte[] object)
Deserialises the object contained in the designated byte array.static Serializable
fromInputStream(InputStream object)
Deserialises the object contained in the designated input stream.static long
getMemStreamThreshold()
Gets the amount of memory that is used as threshold distinguishing between memory and file storage for a stream.static byte[]
getSHA512Digest(InputStream stream)
Deprecated, for removal: This API element is subject to removal in a future version.UsegetSHA512DigestAndClose(InputStream)
instead which closes the designated stream.static byte[]
getSHA512DigestAndClose(InputStream stream)
Gets the SHA-512 digest for the contents of the designated stream.static void
initialise(Map<String,String> properties, boolean allowMultiInit)
Initialises the stream threshold providers by parsing the corresponding properties from the designated map: "totalMemPerthousandStreamThreshold" "skipClosing" This method will be called when bootstrapping and must only be called once.static String
readUtf8String(InputStream is)
Reads the byte contents of the designated stream as an UTF-8 encoded string.static boolean
skipClosing()
Deprecated, for removal: This API element is subject to removal in a future version.Do only use if absolutely required.static void
terminate()
Signals the termination to the stream tools.static String
toBase64(InputStream is, boolean gzip)
Gets a string representing the contents of the designated stream Base64-encoded and optionally gzipped before.static String
toBase64(InputStream is, boolean gzip, int lineLength, byte[] lineSeparator)
Gets a string representing the contents of the designated stream Base64-encoded and optionally gzipped before.static byte[]
toByteArray(Serializable object)
Converts the designated serialisable object to a byte array.static byte[]
toGZippedByteArray(Serializable object)
Converts the designated serialisable object to a byte array.static AttributedInputStream
toInputStream(Serializable object, boolean gzip)
Stores the designated serialisable in a stream and provides it viaInputStream
static InputStream
wrapGzipStream(InputStream is)
Wraps the designated input stream in aGZIPInputStream
if the corresponding header bytes are present.static void
writeBase64To(InputStream is, OutputStream os, boolean gzip)
Writes the content of the designatedInputStream
to the designatedOutputStream
gzips the content if desired and Base64-encodes it.static void
writeBase64To(InputStream is, OutputStream os, boolean gzip, int lineLength, byte[] lineSeparator)
Writes the content of the designatedInputStream
to the designatedOutputStream
gzips the content if desired and Base64-encodes it.static InputStream
writeUtf8String(String s)
Writes the designated string as UTF-8 byte content to a stream and returns it.
-
-
-
Field Detail
-
PROPERTY_TOTAL_MEM_PERTHOUSAND_STREAM_THRESHOLD
public static final String PROPERTY_TOTAL_MEM_PERTHOUSAND_STREAM_THRESHOLD
The string for the name of the property to set the per thousand of total memory that is used as threshold distinguishing between memory and file storage for a stream.- See Also:
- Constant Field Values
-
DEFAULT_STREAM_THRESHOLD_TOTAL_MEM_PERTHOUSAND
public static final int DEFAULT_STREAM_THRESHOLD_TOTAL_MEM_PERTHOUSAND
The default value for the per thousand of total memory that is used as threshold distinguishing between memory and file storage for a stream.- See Also:
- Constant Field Values
-
PROPERTY_SKIP_CLOSING
@Deprecated(since="15.0.0", forRemoval=true) public static final String PROPERTY_SKIP_CLOSING
Deprecated, for removal: This API element is subject to removal in a future version.Do only use if absolutely required. This will be removed as soon as closing works reliable.The string for the name of the property to skip closing. This prevents closing variousAutoCloseable
allowing to bypass bugs that have been introduced with closing, for instance closing too early.- See Also:
- Constant Field Values
-
-
Method Detail
-
initialise
public static void initialise(Map<String,String> properties, boolean allowMultiInit) throws Exception
Initialises the stream threshold providers by parsing the corresponding properties from the designated map: This method will be called when bootstrapping and must only be called once. So only call this method in case you do not need to bootstrap the AristaFlow-platform.- Parameters:
properties
- The map providing the necessary global properties.allowMultiInit
- Whether multiple initialisations should not lead to aConfigurationException
. If set, there will only be a warning in the log.- Throws:
Exception
- If the stream tools have already been initialised (this method has been called before) and no multi instances are allowed, anException
will be thrown.
-
terminate
public static void terminate()
Signals the termination to the stream tools.
-
getMemStreamThreshold
public static long getMemStreamThreshold()
Gets the amount of memory that is used as threshold distinguishing between memory and file storage for a stream.- Returns:
- The amount of memory that is used as threshold distinguishing between memory and file storage for a stream.
-
skipClosing
@Deprecated(since="15.0.0", forRemoval=true) public static boolean skipClosing()
Deprecated, for removal: This API element is subject to removal in a future version.Do only use if absolutely required. This will be removed as soon as closing works reliable.Gets whether to skip closing introduced with 15.0.0. This helps in case of buggy closing, e. early or inappropriate closing.- Returns:
- Whether to skip closing introduced with 15.0.0.
-
copyStreamToStream
@Deprecated public static void copyStreamToStream(InputStream in, OutputStream out) throws IOException
Deprecated.UseInputStream.transferTo(OutputStream)
instead.Copy all content of the given InputStream to the given OutputStream. This does not close the streams!- Parameters:
in
- input streamout
- output stream- Throws:
IOException
- if an I/O error occurs
-
copyStreamToByteArray
public static byte[] copyStreamToByteArray(InputStream in) throws IOException
Copy all content of the given InputStream to a new byte array.- Parameters:
in
- input stream. The stream will be closed after reading. The stream will be closed after the creation.- Returns:
- the content of the input stream in a byte array
- Throws:
IOException
- if an I/O error occurs
-
toByteArray
public static final byte[] toByteArray(Serializable object) throws IOException
Converts the designated serialisable object to a byte array.- Parameters:
object
- A serialisable object to convert to a byte array.- Returns:
- A byte array representing the serialised designated object.
- Throws:
IOException
- If there are problems when writing the designated object, aIOException
will be thrown.
-
toGZippedByteArray
public static final byte[] toGZippedByteArray(Serializable object) throws IOException
Converts the designated serialisable object to a byte array.- Parameters:
object
- A serialisable object to convert to a byte array.- Returns:
- A byte array representing the serialised designated object.
- Throws:
IOException
- If there are problems when writing the designated object, aIOException
will be thrown.
-
fromByteArray
public static final Serializable fromByteArray(byte[] object) throws IOException, ClassNotFoundException
Deserialises the object contained in the designated byte array. This will try GZip, but switch to uncompressed deserialisation if not successful.- Parameters:
object
- A byte array representing a serialised object (may be gzipped).- Returns:
- The serialisable object from the designated byte array.
- Throws:
IOException
- If there are problems when reading from the designated byte array, anIOException
will be thrown.ClassNotFoundException
- If the class of the designated object cannot be found for deserialisation, aClassNotFoundException
will be thrown.
-
fromInputStream
public static final Serializable fromInputStream(InputStream object) throws IOException, ClassNotFoundException
Deserialises the object contained in the designated input stream. This will try GZip, but switch to uncompressed deserialisation if not successful.Use
ComponentObjectInputStream.fromInputStream(InputStream)
from anExecutableComponent
! Otherwise the wrong classloader will be used for deserialising.- Parameters:
object
- An input stream containing a serialised object (may be gzipped). The stream will be closed after reading.- Returns:
- The serialisable object from the designated input stream.
- Throws:
IOException
- If there are problems when reading from the designated input stream, anIOException
will be thrown.ClassNotFoundException
- If the class of the designated object cannot be found for deserialisation, aClassNotFoundException
will be thrown.
-
toInputStream
public static final AttributedInputStream toInputStream(Serializable object, boolean gzip) throws IOException
Stores the designated serialisable in a stream and provides it viaInputStream
- Parameters:
object
- A serialisable object which to provide from anInputStream
.gzip
- Whether to apply GZip to the stream when serialising the designated object.- Returns:
- An input stream containing the bytes for the serialised designated object. The caller is responsible for closing.
- Throws:
IOException
- If there are problems when writing the designated object, anIOException
will be thrown.
-
wrapGzipStream
public static final InputStream wrapGzipStream(InputStream is) throws IOException
Wraps the designated input stream in aGZIPInputStream
if the corresponding header bytes are present. If not, the input stream wrapped in aBufferdInputStream
will be returned.
Do not use the designated input stream but only the returned one!- Parameters:
is
- The input stream which to wrap in aGZIPInputStream
if appropriate. Do not use this stream any more but the returned one.- Returns:
- The designated input stream wrapped appropriately.
- Throws:
IOException
- If there are problems reading the header bytes or creating theGZIPInputStream
, anIOException
will be thrown.
-
cloneViaByteArray
@Deprecated(since="15.0.0", forRemoval=true) public static final <T extends Serializable> T cloneViaByteArray(Serializable object, Class<T> clazz) throws IOException, ClassNotFoundException
Deprecated, for removal: This API element is subject to removal in a future version.UsecloneViaStream(Serializable, Class)
instead.Clone the designated object by converting it to a byte array withtoByteArray(Serializable)
and deserialise it again withfromByteArray(byte[])
.- Parameters:
object
- A serialisable object to be copied.clazz
- The class of the serialisable object.- Returns:
- A copy of the serialisable object.
- Throws:
IOException
- If there are problems when writing the designated object or reading from the designated byte array, anIOException
will be thrown.ClassNotFoundException
- If the class of the designated object cannot be found for deserialisation, aClassNotFoundException
will be thrown.
-
clone
public static final <T> T clone(T object, Class<T> cls) throws IOException
Clone the designated object either viaTypedCloneable.clone()
or in case of incompatible type viacloneViaStream(Serializable, Class)
. If the designated object isnull
or neither aTypedCloneable
norSerializable
,null
will be returned.- Parameters:
object
- A cloneable or serialisable object to be cloned/copied.cls
- The class of the object (or a superclass thereof).- Returns:
- A clone/copy of the designated cloneable or serialisable object or
null
if it is neither cloneable nor serialisable. - Throws:
IOException
- If there are problems when serialising or deserialising the designated object, anIOException
will be thrown (wrapping theCloneNotSupportedException
if appropriate).
-
cloneViaStream
public static final <T> T cloneViaStream(Serializable object, Class<T> clazz) throws IOException
Clone the designated object byserialising it to stream
anddeserialising
it again.- Parameters:
object
- A serialisable object to be copied.clazz
- The class of the serialisable object.- Returns:
- A copy of the serialisable object.
- Throws:
IOException
- If there are problems when serialising or deserialising the designated object, anIOException
will be thrown.
-
getSHA512Digest
@Deprecated(since="15.0.0", forRemoval=true) public static byte[] getSHA512Digest(InputStream stream) throws IOException, NoSuchAlgorithmException
Deprecated, for removal: This API element is subject to removal in a future version.UsegetSHA512DigestAndClose(InputStream)
instead which closes the designated stream. Resetting big streams does not work so prepare the stream for reusage before.Gets the SHA-512 digest for the contents of the designated stream. The designated stream will be read completely for this but it will not be closed!- Parameters:
stream
- The stream for which to determine the SHA-512 digest. This stream will be read completely but not be closed. You may reuse it, so use marking appropriately.- Returns:
- The SHA-512 digest for the contents of the designated stream.
- Throws:
IOException
- If there are problems reading from the designated stream, anIOException
will be thrown.NoSuchAlgorithmException
- If the registered security provider does not support SHA-512, aNoSuchAlgorithmException
will be thrown.
-
getSHA512DigestAndClose
public static byte[] getSHA512DigestAndClose(InputStream stream) throws IOException, NoSuchAlgorithmException
Gets the SHA-512 digest for the contents of the designated stream. The designated stream will be read completely and closed afterwards! Reusing it does not work for big streams so closing is appropriate.- Parameters:
stream
- The stream for which to determine the SHA-512 digest. This stream will be read completely and then closed. You may not reuse it, so if you need it, have a look atRereadableContent
.- Returns:
- The SHA-512 digest for the contents of the designated stream.
- Throws:
IOException
- If there are problems reading from the designated stream, anIOException
will be thrown.NoSuchAlgorithmException
- If the registered security provider does not support SHA-512, aNoSuchAlgorithmException
will be thrown.
-
readUtf8String
public static String readUtf8String(InputStream is) throws IOException
Reads the byte contents of the designated stream as an UTF-8 encoded string.- Parameters:
is
- The input stream of which to read the content as UTF-8 encoded string. The stream will be closed after reading.- Returns:
- The byte contents of the designated stream as string.
- Throws:
IOException
- If there are problems reading the contents of the designated stream as string, anIOException
will be thrown.
-
writeUtf8String
public static InputStream writeUtf8String(String s)
Writes the designated string as UTF-8 byte content to a stream and returns it.- Parameters:
s
- The string which should be wrapped by anInputStream
.- Returns:
- An input stream providing the designated string as UTF-8 bytes.
-
toBase64
public static String toBase64(InputStream is, boolean gzip) throws IOException
Gets a string representing the contents of the designated stream Base64-encoded and optionally gzipped before.- Parameters:
is
- The input stream of which to encode the contents with Base64. The stream will be closed after reading.gzip
- Whether to gzip the contents of the stream before encoding it.- Returns:
- A string representing of the Base64-encoded and optionally gzipped contents of the designated stream.
- Throws:
IOException
- If there are problems reading and encoding the contents of the designated input stream, anIOException
will be thrown.
-
writeBase64To
public static void writeBase64To(InputStream is, OutputStream os, boolean gzip) throws IOException
Writes the content of the designatedInputStream
to the designatedOutputStream
gzips the content if desired and Base64-encodes it.- Parameters:
is
- The input stream of which to encode the contents with Base64. The caller is responsible for closing.gzip
- Whether to gzip the contents of the stream before encoding it.os
- The output stream to which to write the optionally gzipped and Base64-encoded content of the designatedInputStream
. The caller is responsible for closing.- Throws:
IOException
- If there are problems reading and encoding the contents of the designated input stream, anIOException
will be thrown.
-
toBase64
public static String toBase64(InputStream is, boolean gzip, int lineLength, byte[] lineSeparator) throws IOException
Gets a string representing the contents of the designated stream Base64-encoded and optionally gzipped before.- Parameters:
is
- The input stream of which to encode the contents with Base64. The stream will be closed after reading.gzip
- Whether to gzip the contents of the stream before encoding it.lineLength
- The length of each line of the Base64 encoding (rounded down to the nearest multiple of 4). Use0
or less to get a single-line stringlineSeparator
- The line separator to use for splitting the Base64 encoding. This byte sequence will be added afterlineLength
characters. Obviously, a single-line string ornull
will not split lines.- Returns:
- A string representing of the Base64-encoded and optionally gzipped contents of the designated stream.
- Throws:
IOException
- If there are problems reading and encoding the contents of the designated input stream, anIOException
will be thrown.
-
writeBase64To
public static void writeBase64To(InputStream is, OutputStream os, boolean gzip, int lineLength, byte[] lineSeparator) throws IOException
Writes the content of the designatedInputStream
to the designatedOutputStream
gzips the content if desired and Base64-encodes it.- Parameters:
is
- The input stream of which to encode the contents with Base64. The caller is responsible for closing.gzip
- Whether to gzip the contents of the stream before encoding it.lineLength
- The length of each line of the Base64 encoding (rounded down to the nearest multiple of 4). Use0
or less to get a single-line stringlineSeparator
- The line separator to use for splitting the Base64 encoding. This byte sequence will be added afterlineLength
characters. Obviously, a single-line string ornull
will not split lines.os
- The output stream to which to write the optionally gzipped and Base64-encoded content of the designatedInputStream
. The caller is responsible for closing.- Throws:
IOException
- If there are problems reading and encoding the contents of the designated input stream, anIOException
will be thrown.
-
fromBase64
public static InputStream fromBase64(String base64) throws IOException
Gets an input stream containing the decoded (and possibly deflated) content of the designated string which is expected to be Base64 possibly gzipped before encoding.- Parameters:
base64
- The string representing a Base64 encoding with a possibly gzipped content.- Returns:
- The content of the designated string decoded and possibly deflated.
- Throws:
IOException
- If there are problems creating the reader for the string or wrapping the input stream with a gzip stream, anIOException
will be thrown.
-
fromBase64
public static InputStream fromBase64(InputStream base64) throws IOException
Gets an input stream decoding Base64 content from the designatedInputStream
and deflating it from gzip if appropriate.- Parameters:
base64
- An input stream providing Base64-encoded (and possibly gzipped) content. The caller is responsible for closing. Closing will be forwarded from the returnedInputStream
.- Returns:
- A stream decoding the designated Base64-content and deflating it if appropriate. The
caller is responsible for closing. Closing will be
forwarded to the designated
InputStream
. - Throws:
IOException
- If there are problems reading the header bytes for detecting whether to use aGZIPInputStream
, anIOException
will be thrown.
-
-