Class InputStreamWrapper

java.lang.Object
java.io.InputStream
com.mku.streams.InputStreamWrapper
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
AesFileInputStream

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

    • 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 stream chunk size for efficiency
      See Also:
    • DEFAULT_BUFFERS

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

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

      protected static final int MAX_BUFFERS
      The maximum allowed buffer count
      See Also:
  • Constructor Details

    • 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 Details

    • 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 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 InputStream
    • read

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

      public int getBackOffset()
      Get the current back offset
      Returns:
      The back offset
    • setBackOffset

      protected void setBackOffset(int backOffset)
      Set the back offset
      Parameters:
      backOffset - The back offset
    • read

      public int read(byte[] buffer, int offset, int count) throws IOException
      Reads the contents of the stream
      Overrides:
      read in class InputStream
      Parameters:
      buffer - The buffer that will store the contents
      offset - The position on the buffer that the data will start
      count - The length of the data requested
      Throws:
      IOException
    • fillBuffer

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

      protected int fillBufferPart(Buffer cacheBuffer, long start, int offset, int length, RandomAccessStream stream) throws IOException
      Fills a cache buffer with the data from a part of the stream.
      Parameters:
      cacheBuffer - The cache buffer that will store the 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:
      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 IOException
      Close the stream and associated backed streams and clear buffers.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException - Thrown if there is an IO error.