Class StreamTools

java.lang.Object
de.aristaflow.adept2.util.StreamTools

public class StreamTools extends Object
InputStream and OutputStream related tool methods.
  • Field Details

    • 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:
    • 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:
    • 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 various AutoCloseable allowing to bypass bugs that have been introduced with closing, for instance closing too early.
      See Also:
  • Constructor Details

    • StreamTools

      public StreamTools()
  • Method Details

    • 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 a ConfigurationException. 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, an Exception 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.
    • 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, a IOException 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, a IOException 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, an IOException will be thrown.
      ClassNotFoundException - If the class of the designated object cannot be found for deserialisation, a ClassNotFoundException 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 an ExecutableComponent! 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, an IOException will be thrown.
      ClassNotFoundException - If the class of the designated object cannot be found for deserialisation, a ClassNotFoundException 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 via InputStream
      Parameters:
      object - A serialisable object which to provide from an InputStream.
      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, an IOException will be thrown.
    • wrapGzipStream

      public static final InputStream wrapGzipStream(InputStream is) throws IOException
      Wraps the designated input stream in a GZIPInputStream if the corresponding header bytes are present. If not, the input stream wrapped in a BufferdInputStream will be returned.
      Do not use the designated input stream but only the returned one!
      Parameters:
      is - The input stream which to wrap in a GZIPInputStream 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 the GZIPInputStream, an IOException 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.
      Clone the designated object by converting it to a byte array with toByteArray(Serializable) and deserialise it again with fromByteArray(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, an IOException will be thrown.
      ClassNotFoundException - If the class of the designated object cannot be found for deserialisation, a ClassNotFoundException will be thrown.
    • clone

      public static final <T> T clone(T object, Class<T> cls) throws IOException
      Clone the designated object either via TypedCloneable.clone() or in case of incompatible type via cloneViaStream(Serializable, Class). If the designated object is null or neither a TypedCloneable nor Serializable, 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, an IOException will be thrown (wrapping the CloneNotSupportedException if appropriate).
    • cloneViaStream

      public static final <T> T cloneViaStream(Serializable object, Class<T> clazz) throws IOException
      Clone the designated object by serialising it to stream and deserialising 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, an IOException 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.
      Use getSHA512DigestAndClose(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, an IOException will be thrown.
      NoSuchAlgorithmException - If the registered security provider does not support SHA-512, a NoSuchAlgorithmException 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 at RereadableContent.
      Returns:
      The SHA-512 digest for the contents of the designated stream.
      Throws:
      IOException - If there are problems reading from the designated stream, an IOException will be thrown.
      NoSuchAlgorithmException - If the registered security provider does not support SHA-512, a NoSuchAlgorithmException 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, an IOException 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 an InputStream.
      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, an IOException will be thrown.
    • writeBase64To

      public static void writeBase64To(InputStream is, OutputStream os, boolean gzip) throws IOException
      Writes the content of the designated InputStream to the designated OutputStream 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 designated InputStream. The caller is responsible for closing.
      Throws:
      IOException - If there are problems reading and encoding the contents of the designated input stream, an IOException 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). Use 0 or less to get a single-line string
      lineSeparator - The line separator to use for splitting the Base64 encoding. This byte sequence will be added after lineLength characters. Obviously, a single-line string or null 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, an IOException 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 designated InputStream to the designated OutputStream 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). Use 0 or less to get a single-line string
      lineSeparator - The line separator to use for splitting the Base64 encoding. This byte sequence will be added after lineLength characters. Obviously, a single-line string or null will not split lines.
      os - The output stream to which to write the optionally gzipped and Base64-encoded content of the designated InputStream. The caller is responsible for closing.
      Throws:
      IOException - If there are problems reading and encoding the contents of the designated input stream, an IOException 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, an IOException will be thrown.
    • fromBase64

      public static InputStream fromBase64(InputStream base64) throws IOException
      Gets an input stream decoding Base64 content from the designated InputStream 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 returned InputStream.
      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 a GZIPInputStream, an IOException will be thrown.