Class ByteArrayStream

java.lang.Object
net.sf.sevenzipjbinding.util.ByteArrayStream
All Implemented Interfaces:
Closeable, AutoCloseable, IInStream, IOutStream, ISeekableStream, ISequentialInStream, ISequentialOutStream

public class ByteArrayStream extends Object implements IInStream, IOutStream
A byte array based implementation of Provides read/write access to the content represented as a byte array. Provides a bridge to InputStream and OutputStream through various methods.
Since:
9.20-2.00
Author:
Boris Brodski
  • Field Summary

    Fields inherited from interface net.sf.sevenzipjbinding.ISeekableStream

    SEEK_CUR, SEEK_END, SEEK_SET
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteArrayStream(byte[] content, boolean copyContentArray)
    Create a new instance of ByteArrayStream initialized with the given content without specifying maximal length of the stored data.
    WARNING: The maximal length of the byte array stream will be set to content.length.
    ByteArrayStream(byte[] content, boolean copyContentArray, int maxSize)
    Create a new instance of ByteArrayStream initialized with the given content specifying maximal length of the stored data.
    ByteArrayStream(int maxSize)
    Create new empty instance of ByteArrayStream specifying maximal length of the stored data.
    ByteArrayStream(int initialSize, int maxSize)
    Create new empty instance of ByteArrayStream specifying maximal length of the stored data.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Empty method.
    byte[]
    Return the content of the byte array stream in a new byte array.
    int
    Return current position in the byte array stream.
    Get a detached input stream associated with the entire byte stream content.
    Get an attached input stream associated with the byte stream content.
    Get an attached output stream associated with the byte stream content.
    int
    Return the size of the byte array stream content in bytes.
    boolean
    Retrieve "End Of Stream" status of the byte array stream.
    int
    read(byte[] data)
    Reads at least 1 and maximum data.length bytes from the in-stream.
    int
    read(byte[] data, int startPosition, int length)
    Reads length bytes from the byte array stream.
    void
    Set current pointer back to zero.
    long
    seek(long offset, int seekOrigin)
    Move current location pointer to the new offset depending on seekOrigin.

    Note: depending on the archive format and the data size this method may be called from different threads.
    void
    setBytes(byte[] newContent, boolean copyNewContentArray)
    Reinitialize byte array stream, replace current content with the new content newContent and set the current position to the beginning of the stream.
    void
    setSize(long newSize)
    Set new size for the out stream.

    Note: depending on the archive format and the data size this method may be called from different threads.
    void
    Clear all content of the stream
    int
    write(byte[] data)
    Write data byte array to the stream.
    int
    write(byte[] data, int startPosition, int length)
    Write length byte from the byte array data beginning from the position startPosition.
    void
    writeFromInputStream(InputStream inputStream, boolean closeStreamAfterReading)
    Write entire data from InputStream inputStream into byte array stream.
    void
    writeToOutputStream(OutputStream outputStream, boolean closeStreamAfterWriting)
    Write entire content of the stream to the output stream.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ByteArrayStream

      public ByteArrayStream(byte[] content, boolean copyContentArray, int maxSize)
      Create a new instance of ByteArrayStream initialized with the given content specifying maximal length of the stored data.
      Parameters:
      content - content to initialize byte array stream with. The current position will be set at the beginning of the stream.
      copyContentArray - true - copy the content byte array, so the original array can be modified safely, without affecting the byte stream
      false - don't copy the content byte array. Any change to the byte array content will be reflected by the byte array stream.
      maxSize - maximal length of the stored data. Use Integer.MAX_VALUE to disable maximal length constraint.
    • ByteArrayStream

      public ByteArrayStream(byte[] content, boolean copyContentArray)
      Create a new instance of ByteArrayStream initialized with the given content without specifying maximal length of the stored data.
      WARNING: The maximal length of the byte array stream will be set to content.length. This means, that no more data can be added to such byte array stream. However it's still possible to override or truncate existing data.
      Parameters:
      content - content to initialize byte array stream with. The current position will be set at the beginning of the stream.
      copyContentArray - true - copy the content byte array, so the original array can be modified safely, without affecting the byte stream
      false - don't copy the content byte array. Any change to the byte array content will be reflected by the byte array stream.
    • ByteArrayStream

      public ByteArrayStream(int maxSize)
      Create new empty instance of ByteArrayStream specifying maximal length of the stored data.
      Parameters:
      maxSize - maximal length of the stored data. Use Integer.MAX_VALUE to disable maximal length constraint.
    • ByteArrayStream

      public ByteArrayStream(int initialSize, int maxSize)
      Create new empty instance of ByteArrayStream specifying maximal length of the stored data.
      Parameters:
      maxSize - maximal length of the stored data. Use Integer.MAX_VALUE to disable maximal length constraint.
      initialSize - size of the first data chunk. The first data chunk (byte array) will be allocated after first writing request.
  • Method Details

    • read

      public int read(byte[] data) throws SevenZipException
      Reads at least 1 and maximum data.length bytes from the in-stream. If data.length == 0 0 should be returned. If data.length != 0, then return value 0 indicates end-of-stream (EOF). This means no more bytes can be read from the stream.
      This function is allowed to read fewer than the number of remaining bytes in the stream and fewer than data.length. You must call the read() function in a loop if you need an exact amount of data.

      Note: depending on the archive format and the data size this method may be called from different threads. Synchronized implementation may be required.
      Specified by:
      read in interface ISequentialInStream
      Parameters:
      data - buffer to get read data
      Returns:
      number of bytes read into the data array; 0 indicates end of stream.
      Throws:
      SevenZipException - in error case. If this method ends with an exception, the current operation will be reported to 7-Zip as failed. There is no guarantee that no further callback methods will be called. The first and last thrown exceptions will be saved and thrown later on from the originally called method such as IInArchive.extract() or SevenZip.openInArchive(). Up to four exceptions depending on the situation can be saved for further analysis. See SevenZipException and SevenZipException.printStackTraceExtended() for details.
    • read

      public int read(byte[] data, int startPosition, int length)
      Reads length bytes from the byte array stream. If length == 0 0 is returned. If length != 0, then return value 0 indicates end-of-stream (EOF). This means no more bytes can be read from the stream. The read bytes will be stored in the data array beginning from the position startPosition

      Parameters:
      data - buffer to get read data.
      startPosition - position (index) in the array data to store first read byte.
      length - count of the bytes to read.
      Returns:
      number of bytes read into the data array; 0 represents end of stream.
      Throws:
      IllegalStateException - will be thrown, if startPosition is an invalid index for the array data or if startPosition + length > data.length.
    • isEOF

      public boolean isEOF()
      Retrieve "End Of Stream" status of the byte array stream.
      Returns:
      true the current position is at the end of the stream. The read operation will return 0, the write operation will expand the byte array stream.
      false - the current position is not at the end of the stream.
    • seek

      public long seek(long offset, int seekOrigin) throws SevenZipException
      Move current location pointer to the new offset depending on seekOrigin.

      Note: depending on the archive format and the data size this method may be called from different threads. Synchronized implementation may be required.
      Specified by:
      seek in interface ISeekableStream
      Parameters:
      offset - absolute or relative offset in the stream to move to
      seekOrigin - one of three possible seek origins:
      Returns:
      new absolute position in the stream.
      Throws:
      SevenZipException - in error case. If this method ends with an exception, the current operation will be reported to 7-Zip as failed. There is no guarantee that no further callback methods will be called. The first and last thrown exceptions will be saved and thrown later on from the originally called method such as IInArchive.extract() or SevenZip.openInArchive(). Up to four exceptions depending on the situation can be saved for further analysis. See SevenZipException and SevenZipException.printStackTraceExtended() for details.
    • rewind

      public void rewind()
      Set current pointer back to zero.
    • setSize

      public void setSize(long newSize)
      Set new size for the out stream.

      Note: depending on the archive format and the data size this method may be called from different threads. Synchronized implementation may be required.
      Specified by:
      setSize in interface IOutStream
      Parameters:
      newSize - new size
    • write

      public int write(byte[] data)
      Write data byte array to the stream. If data.length > 0 this function must write at least 1 byte. This function is allowed to write fewer than data.length bytes. You must call the write() function in a loop if you need to write an exact amount of data.

      Note: depending on the archive format and the data size this method may be called from different threads. Synchronized implementation may be required.
      Specified by:
      write in interface ISequentialOutStream
      Parameters:
      data - data to write
      Returns:
      count of written bytes
    • write

      public int write(byte[] data, int startPosition, int length)
      Write length byte from the byte array data beginning from the position startPosition.
      Parameters:
      data - data to write
      startPosition - index of the first byte to write (beginning with 0)
      length - count of bytes to write
      Returns:
      count of written bytes
      Throws:
      IllegalStateException - will be thrown, if startPosition is an invalid index for the array data or if startPosition + length > data.length.
    • getDetachedInputStream

      public InputStream getDetachedInputStream()
      Get a detached input stream associated with the entire byte stream content. Reading from this input stream doesn't affect the current position of the byte array stream.
      Warning: The returned instance of the InputStream is still attached to the content of the byte array stream. That means, that any change of the content will be immediately visible through InputStream.

      Note: this method is not yet implemented and always throws IllegalStateException. Use getBytes() or writeToOutputStream(OutputStream, boolean) instead.

      Returns:
      detached input stream
      Throws:
      IllegalStateException - always, since this method is not yet implemented
    • getInputStream

      public InputStream getInputStream()
      Get an attached input stream associated with the byte stream content. Reading from the returned InputStream is equivalent to reading from the byte array itself. This means that reading from the InputStream starts at the current position of the byte array stream and moves it forward.

      Note: this method is not yet implemented and always throws IllegalStateException. Use read(byte[]) instead.

      Returns:
      InputStream implementation for this byte array stream
      Throws:
      IllegalStateException - always, since this method is not yet implemented
    • getOutputStream

      public OutputStream getOutputStream()
      Get an attached output stream associated with the byte stream content. Writing to the returned OutputStream is equivalent to writing to the byte array itself. This means that writing to the OutputStream affects the current position of the byte array stream.

      Note: this method is not yet implemented and always throws IllegalStateException. Use write(byte[]) or writeFromInputStream(InputStream, boolean) instead.

      Returns:
      OutputStream implementation for this byte array stream
      Throws:
      IllegalStateException - always, since this method is not yet implemented
    • writeToOutputStream

      public void writeToOutputStream(OutputStream outputStream, boolean closeStreamAfterWriting) throws IOException
      Write entire content of the stream to the output stream.
      Parameters:
      outputStream - output stream to write the entire content to
      closeStreamAfterWriting - true close output stream outputStream by calling OutputStream.close() method, false don't close output stream
      Throws:
      IOException - if I/O exception occurs
    • writeFromInputStream

      public void writeFromInputStream(InputStream inputStream, boolean closeStreamAfterReading) throws IOException
      Write entire data from InputStream inputStream into byte array stream. The new data will be written at the current position of the byte array stream.
      Parameters:
      inputStream - input stream to read from.
      closeStreamAfterReading - close input stream after reading.
      Throws:
      IOException - exceptions during reading and optional closing of input stream.
    • truncate

      public void truncate()
      Clear all content of the stream
    • getSize

      public int getSize()
      Return the size of the byte array stream content in bytes.
      Returns:
      the size of the byte array stream content in bytes.
    • getCurrentPosition

      public int getCurrentPosition()
      Return current position in the byte array stream. The current position determines which part of the data will be affected by next read or write operation. The current position can be changed explicitly by calling seek(long, int) method.
      Returns:
      current position in the stream beginning with 0. if current position is equal to the size of the stream getSize(), that means, that end of stream (EOF) was reached. All subsequent read operations will return EOF. All subsequent write operation will expand the stream until maximal size of stream will be reached. (See ByteArrayStream(int)).
    • getBytes

      public byte[] getBytes()
      Return the content of the byte array stream in a new byte array. The current content of the byte array stream copied to the new byte array.
      Returns:
      new array with the entire content of the byte array stream
    • setBytes

      public void setBytes(byte[] newContent, boolean copyNewContentArray)
      Reinitialize byte array stream, replace current content with the new content newContent and set the current position to the beginning of the stream.
      Parameters:
      newContent - new content of the byte array stream
      copyNewContentArray - true - copy the content byte array, so the original array can be modified safely, without affecting the byte stream
      false - don't copy the content byte array. Any change to the byte array content will be reflected by the byte array stream.
    • close

      public void close() throws IOException
      Empty method. No closing required.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - never
      See Also: