Package org.cyclos.server.utils
Class SerializableInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.cyclos.server.utils.SerializableInputStream
-
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable
public class SerializableInputStream extends InputStream implements Serializable
AnInputStreamwhich wraps anotherInputStream, so that intra-jvm access is done directly on that stream. However, when it's serialized, the data is sent on chunks and, when read on the other side, those chunks are written in a temporary file. So, it's safe to serialize large amount of data without consuming large memory.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description SerializableInputStream(byte[] bytes)Shortcut toSerializableInputStream(InputStream)using aByteArrayInputStreamSerializableInputStream(File file)Reads the given file, deleting it whenclose()is invokedSerializableInputStream(InputStream inputStream)Wraps the given streamSerializableInputStream(Blob blob)Wraps the given blob, and callsforceCopyToFile(), as blobs can only be accessed from within a database transaction
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()voidclose()Filefile()Returns a temporaryFilewith the contents of this stream.byte[]forceCopyToBytes()Exhausts the open stream and copy the contents to a byte array.FileforceCopyToFile()Exhausts the open stream and copy the contents to a temporary file.intread()intread(byte[] b)intread(byte[] b, int off, int len)voidreOpen()Closes the underlying stream and re-reads the file or byte array-
Methods inherited from class java.io.InputStream
mark, markSupported, nullInputStream, readAllBytes, readNBytes, readNBytes, reset, skip, transferTo
-
-
-
-
Constructor Detail
-
SerializableInputStream
public SerializableInputStream(Blob blob) throws IOException
Wraps the given blob, and callsforceCopyToFile(), as blobs can only be accessed from within a database transaction- Throws:
IOException
-
SerializableInputStream
public SerializableInputStream(byte[] bytes)
Shortcut toSerializableInputStream(InputStream)using aByteArrayInputStream
-
SerializableInputStream
public SerializableInputStream(File file)
Reads the given file, deleting it whenclose()is invoked
-
SerializableInputStream
public SerializableInputStream(InputStream inputStream)
Wraps the given stream
-
-
Method Detail
-
available
public int available() throws IOException- Overrides:
availablein classInputStream- Throws:
IOException
-
close
public void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
file
public File file() throws IOException, IllegalStateException
Returns a temporaryFilewith the contents of this stream. When the stream has been serialized, the contents is already on a temporary file, so it is returned. Otherwise, copy the wrapped stream into a temporary file and return it. It is important to state that after this method is invoked, the temporary file will no longer be deleted when this stream is closed, and it's the callers responsibility to remove the temporary file.- Throws:
IOException- When there was an error reading to the temporary fileIllegalStateException- When this stream was already closed
-
forceCopyToBytes
public byte[] forceCopyToBytes() throws IOExceptionExhausts the open stream and copy the contents to a byte array. No-op if already on a byte[].- Throws:
IOException- Error reading the underlying stream
-
forceCopyToFile
public File forceCopyToFile() throws IOException
Exhausts the open stream and copy the contents to a temporary file. No-op if already on a file. The file is not marked as to being kept (opposed to what happens on thefile()method), so, subsequent invocation ofclose()method will delete the file- Throws:
IOException- Error reading the underlying stream or writing to file
-
read
public int read() throws IOException- Specified by:
readin classInputStream- Throws:
IOException
-
read
public int read(byte[] b) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
read
public int read(byte[] b, int off, int len) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
reOpen
public void reOpen() throws IOExceptionCloses the underlying stream and re-reads the file or byte array- Throws:
IOException- if either no file or byte[]
-
-