Class StreamTools

    • Constructor Detail

      • StreamTools

        public StreamTools()
    • Method Detail

      • 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
        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).
        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.
      • cloneViaByteArray

        public static final <T extends Serializable> T cloneViaByteArray​(Serializable object,
                                                                         Class<T> clazz)
                                                                  throws IOException,
                                                                         ClassNotFoundException
        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.
      • getSHA512Digest

        public static byte[] getSHA512Digest​(InputStream stream)
                                      throws IOException,
                                             NoSuchAlgorithmException
        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.