Class SerializableInputStream

  • All Implemented Interfaces:
    Closeable, Serializable, AutoCloseable

    public class SerializableInputStream
    extends InputStream
    implements Serializable
    An InputStream which wraps another InputStream, 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 Detail

      • SerializableInputStream

        public SerializableInputStream​(Blob blob)
                                throws IOException
        Wraps the given blob, and calls forceCopyToFile(), as blobs can only be accessed from within a database transaction
        Throws:
        IOException
      • SerializableInputStream

        public SerializableInputStream​(File file)
        Reads the given file, deleting it when close() is invoked
      • SerializableInputStream

        public SerializableInputStream​(InputStream inputStream)
        Wraps the given stream
    • Method Detail

      • file

        public File file()
                  throws IOException,
                         IllegalStateException
        Returns a temporary File with 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 file
        IllegalStateException - When this stream was already closed
      • 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 the file() method), so, subsequent invocation of close() method will delete the file
        Throws:
        IOException - Error writing to file
      • reOpen

        public void reOpen()
                    throws IOException
        Closes the underlying stream and opens a new one from file to start reading from
        Throws:
        IOException - if the file is missing