Class InputStreamWrapper

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    AesFileInputStream

    public class InputStreamWrapper
    extends java.io.InputStream
    InputStream wrapper for RandomAccessStream. Use this class to wrap any RandomAccessStream to a Java InputStream to use with 3rd party libraries.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_BACK_OFFSET
      The default backwards buffer offset
      static int DEFAULT_BUFFER_SIZE
      Default cache buffer should be high enough for some mpeg videos to work the cache buffers should be aligned to the AesFile chunk size for efficiency
      static int DEFAULT_BUFFERS
      The default buffer count
      protected static int MAX_BUFFERS
      The maximum allowed buffer count
    • Constructor Summary

      Constructors 
      Constructor Description
      InputStreamWrapper​(RandomAccessStream stream)
      Instantiates an InputStreamWrapper from a RandomAccessStream.
      InputStreamWrapper​(RandomAccessStream stream, int buffersCount, int bufferSize, int backOffset)
      Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options.
      InputStreamWrapper​(RandomAccessStream stream, int buffersCount, int bufferSize, int backOffset, int alignSize)
      Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the stream and associated backed streams and clear buffers.
      protected int fillBuffer​(Buffer cacheBuffer, long startPosition, int length)
      Fills a cache buffer with the decrypted data from the encrypted source file.
      protected int fillBufferPart​(Buffer cacheBuffer, long start, int offset, int length, RandomAccessStream stream)
      Fills a cache buffer with the decrypted data from a part of an encrypted file
      int getAlignSize()
      Get the align size
      int getBackOffset()  
      protected int getBuffersCount()
      Get the buffers count
      int getBufferSize()
      Get the buffer size
      long getLength()
      Get the size of the stream.
      long getPositionEnd()
      Get the end position for the stream.
      long getPositionStart()
      Get the start position for the stream.
      long getTotalSize()
      Get the total size
      int read()
      Read a byte from the stream.
      int read​(byte[] buffer, int offset, int count)
      Reads and decrypts the contents of an encrypted file
      void reset()
      Reset the stream.
      protected void setBackOffset​(int backOffset)  
      protected void setBuffersCount​(int buffersCount)
      Set the buffers count
      void setPositionEnd​(long pos)
      Set the end position for the stream.
      void setPositionStart​(long pos)
      Set the start position for the stream.
      protected void setTotalSize​(long totalSize)
      Set the total size
      long skip​(long bytes)
      Skip a number of bytes.
      • Methods inherited from class java.io.InputStream

        available, mark, markSupported, nullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        Default cache buffer should be high enough for some mpeg videos to work the cache buffers should be aligned to the AesFile chunk size for efficiency
        See Also:
        Constant Field Values
      • DEFAULT_BUFFERS

        public static final int DEFAULT_BUFFERS
        The default buffer count
        See Also:
        Constant Field Values
      • DEFAULT_BACK_OFFSET

        public static final int DEFAULT_BACK_OFFSET
        The default backwards buffer offset
        See Also:
        Constant Field Values
      • MAX_BUFFERS

        protected static final int MAX_BUFFERS
        The maximum allowed buffer count
        See Also:
        Constant Field Values
    • Constructor Detail

      • InputStreamWrapper

        public InputStreamWrapper​(RandomAccessStream stream)
        Instantiates an InputStreamWrapper from a RandomAccessStream.
        Parameters:
        stream - The stream that you want to wrap.
      • InputStreamWrapper

        public InputStreamWrapper​(RandomAccessStream stream,
                                  int buffersCount,
                                  int bufferSize,
                                  int backOffset)
        Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options.
        Parameters:
        stream - The stream that you want to wrap.
        buffersCount - The number of buffers to use
        bufferSize - The buffer size
        backOffset - The back offset
      • InputStreamWrapper

        public InputStreamWrapper​(RandomAccessStream stream,
                                  int buffersCount,
                                  int bufferSize,
                                  int backOffset,
                                  int alignSize)
        Instantiates an InputStreamWrapper from a RandomAccessStream with buffer options.
        Parameters:
        stream - The stream that you want to wrap.
        buffersCount - The number of buffers to use
        bufferSize - The buffer size
        backOffset - The back offset
        alignSize - The align size
    • Method Detail

      • getBufferSize

        public int getBufferSize()
        Get the buffer size
        Returns:
        The buffer size
      • getTotalSize

        public long getTotalSize()
        Get the total size
        Returns:
        The total size
      • setTotalSize

        protected void setTotalSize​(long totalSize)
        Set the total size
        Parameters:
        totalSize - The total size
      • getAlignSize

        public int getAlignSize()
        Get the align size
        Returns:
        The align size
      • skip

        public long skip​(long bytes)
        Skip a number of bytes.
        Overrides:
        skip in class java.io.InputStream
        Parameters:
        bytes - the number of bytes to be skipped.
        Returns:
        The byte skipped
      • reset

        public void reset()
        Reset the stream.
        Overrides:
        reset in class java.io.InputStream
      • read

        public int read()
                 throws java.io.IOException
        Read a byte from the stream.
        Specified by:
        read in class java.io.InputStream
        Returns:
        The bytes read
        Throws:
        java.io.IOException - Thrown if there is an IO error.
      • getBackOffset

        public int getBackOffset()
      • setBackOffset

        protected void setBackOffset​(int backOffset)
      • read

        public int read​(byte[] buffer,
                        int offset,
                        int count)
                 throws java.io.IOException
        Reads and decrypts the contents of an encrypted file
        Overrides:
        read in class java.io.InputStream
        Parameters:
        buffer - The buffer that will store the decrypted contents
        offset - The position on the buffer that the decrypted data will start
        count - The length of the data requested
        Throws:
        java.io.IOException
      • fillBuffer

        protected int fillBuffer​(Buffer cacheBuffer,
                                 long startPosition,
                                 int length)
                          throws java.io.IOException
        Fills a cache buffer with the decrypted data from the encrypted source file.
        Parameters:
        cacheBuffer - The cache buffer that will store the decrypted contents
        startPosition - The start position
        length - The length of the data requested
        Returns:
        The number of bytes read
        Throws:
        java.io.IOException - When IO error occurs
      • fillBufferPart

        protected int fillBufferPart​(Buffer cacheBuffer,
                                     long start,
                                     int offset,
                                     int length,
                                     RandomAccessStream stream)
                              throws java.io.IOException
        Fills a cache buffer with the decrypted data from a part of an encrypted file
        Parameters:
        cacheBuffer - The cache buffer that will store the decrypted contents
        start - The start position
        offset - The offset
        length - The length of the data requested
        stream - The stream that will be used to read from
        Returns:
        The number of total bytes read.
        Throws:
        java.io.IOException - When IO error occurs
      • getLength

        public long getLength()
        Get the size of the stream.
        Returns:
        The size
      • getPositionStart

        public long getPositionStart()
        Get the start position for the stream.
        Returns:
        The start position.
      • setPositionStart

        public void setPositionStart​(long pos)
        Set the start position for the stream.
        Parameters:
        pos - The start position.
      • getPositionEnd

        public long getPositionEnd()
        Get the end position for the stream.
        Returns:
        The end position.
      • setPositionEnd

        public void setPositionEnd​(long pos)
        Set the end position for the stream.
        Parameters:
        pos - The end position.
      • getBuffersCount

        protected int getBuffersCount()
        Get the buffers count
        Returns:
        The buffers count
      • setBuffersCount

        protected void setBuffersCount​(int buffersCount)
        Set the buffers count
        Parameters:
        buffersCount - The buffers count
      • close

        public void close()
                   throws java.io.IOException
        Close the stream and associated backed streams and clear buffers.
        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 - Thrown if there is an IO error.