Class SerializableInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Serializable, java.lang.AutoCloseable

    public class SerializableInputStream
    extends java.io.InputStream
    implements java.io.Serializable
    An InputStream that is serializable
    Author:
    jschwarz
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected SerializableInputStream()  
        SerializableInputStream​(java.io.InputStream inputStream)
      Creates a SerializableInputStream for a given underlying InputStream
        SerializableInputStream​(java.io.InputStream in, int bufferSize, int inMemoryBytes, java.io.File tempFileDirectory)
      Creates a SerializableInputStream for a given underlying InputStream
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int available()  
      void close()  
      java.io.InputStream getInputStream()
      This method returns the wrapped input stream that is serialized by this stream.
      void mark​(int readlimit)  
      boolean markSupported()  
      int read()  
      int read​(byte[] b)  
      int read​(byte[] b, int off, int len)  
      static java.lang.Iterable<byte[]> readChunksFromObjectStream​(java.io.ObjectInputStream inputStream)
      This method expects the given ObjectInputStream to hold the data of an InputStream serialized by writeStreamAsChunksToObjectStream(InputStream, int, ObjectOutputStream) and returns an Iterable for the serialized chunks of the stream.
      void reset()  
      long skip​(long n)  
      static void writeStreamAsChunksToObjectStream​(java.io.InputStream inputStream, int chunkSize, java.io.ObjectOutputStream outputStream)
      This method serializes a given InputStream to a given ObjectOutputStream.
      • Methods inherited from class java.io.InputStream

        nullInputStream, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SerializableInputStream

        protected SerializableInputStream()
      • SerializableInputStream

        public SerializableInputStream​(java.io.InputStream inputStream)
        Creates a SerializableInputStream for a given underlying InputStream
        Parameters:
        inputStream - the underlying input stream. Must not be null.
      • SerializableInputStream

        public SerializableInputStream​(java.io.InputStream in,
                                       int bufferSize,
                                       int inMemoryBytes,
                                       java.io.File tempFileDirectory)
        Creates a SerializableInputStream for a given underlying InputStream
        Parameters:
        in - the underlying input stream. Must not be null.
        bufferSize - the size of the chunks used for serialization
        inMemoryBytes - number of bytes to keep in memory before using a temp file when deserializing the stream
        tempFileDirectory - directory to store temporary files in when deserializing the stream
    • Method Detail

      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long n)
                  throws java.io.IOException
        Overrides:
        skip in class java.io.InputStream
        Throws:
        java.io.IOException
      • available

        public int available()
                      throws java.io.IOException
        Overrides:
        available in class java.io.InputStream
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • mark

        public void mark​(int readlimit)
        Overrides:
        mark in class java.io.InputStream
      • reset

        public void reset()
                   throws java.io.IOException
        Overrides:
        reset in class java.io.InputStream
        Throws:
        java.io.IOException
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.InputStream
      • getInputStream

        public java.io.InputStream getInputStream()
        This method returns the wrapped input stream that is serialized by this stream.
        Returns:
        the wrapped input stream that is serialized by this stream
      • writeStreamAsChunksToObjectStream

        public static void writeStreamAsChunksToObjectStream​(java.io.InputStream inputStream,
                                                             int chunkSize,
                                                             java.io.ObjectOutputStream outputStream)
                                                      throws java.io.IOException
        This method serializes a given InputStream to a given ObjectOutputStream.
        Parameters:
        inputStream - the stream to serialize
        chunkSize - the size of the chunks the stream is serialized as
        outputStream - the output stream to serialize to
        Throws:
        java.io.IOException
      • readChunksFromObjectStream

        public static java.lang.Iterable<byte[]> readChunksFromObjectStream​(java.io.ObjectInputStream inputStream)
        This method expects the given ObjectInputStream to hold the data of an InputStream serialized by writeStreamAsChunksToObjectStream(InputStream, int, ObjectOutputStream) and returns an Iterable for the serialized chunks of the stream.

        NOTE: be aware that for performance reasons the Iterator returned by the iterable will return the same byte array for most chunks. So if it is necessary - for whatever reason - to store the chunks one needs to copy them first

        Parameters:
        inputStream - an object input stream holding the data of an InputStream serialized by writeStreamAsChunksToObjectStream(InputStream, int, ObjectOutputStream)
        Returns:
        an Iterable for the serialized chunks of the stream