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.
-
-
Constructor Summary
Constructors Constructor Description StreamTools()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends Serializable>
TcloneViaByteArray(Serializable object, Class<T> clazz)
Clone the designated object by converting it to a byte array withtoByteArray(Serializable)
and deserialise it again withfromByteArray(byte[])
.static byte[]
copyStreamToByteArray(InputStream in)
Copy all content of the given InputStream to a new byte array.static void
copyStreamToStream(InputStream in, OutputStream out)
Copy all content of the given InputStream to the given OutputStream.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 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.
-
-
-
Method Detail
-
copyStreamToStream
public static void copyStreamToStream(InputStream in, OutputStream out) throws IOException
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- 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).- 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.
-
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 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.
-
-