net.sf.sevenzipjbinding.util
Class ByteArrayStream

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

public class ByteArrayStream
extends java.lang.Object
implements IInStream, IOutStream

A byte array based implementation of

Provide read/write access to the content represented as a byte array. Provide 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
 
Fields inherited from interface net.sf.sevenzipjbinding.ISeekableStream
SEEK_CUR, SEEK_END, SEEK_SET
 
Constructor Summary
ByteArrayStream(byte[] content, boolean copyContentArray)
          Create new empty instance of ByteArrayStream with content 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 new empty instance of ByteArrayStream with content 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
 void close()
          Empty method.
 byte[] getBytes()
          Return the content of the byte array stream in a new byte array.
 int getCurrentPosition()
          Return current position in the byte array stream.
 java.io.InputStream getDetachedInputStream()
          Get a detached input stream associated with the entire byte stream content.
 java.io.InputStream getInputStream()
          Get an attached input stream associated with the byte stream content.
 java.io.OutputStream getOutputStream()
          Get an attached output stream associated with the byte stream content.
 int getSize()
          Return the size of the byte array stream content in bytes.
 boolean isEOF()
          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 rewind()
          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 truncate()
          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(java.io.InputStream inputStream, boolean closeStreamAfterReading)
          Write entire data from InputStream inputStream into byte array stream.
 void writeToOutputStream(java.io.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 Detail

ByteArrayStream

public ByteArrayStream(byte[] content,
                       boolean copyContentArray,
                       int maxSize)
Create new empty instance of ByteArrayStream with content 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 newContent byte array, so the original array can be modified safely, without affecting the byte stream
false - don't copy byte array newContent. Any change to the byte array newContent 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 new empty instance of ByteArrayStream with content 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 newContent byte array, so the original array can be modified safely, without affecting the byte stream
false - don't copy byte array newContent. Any change to the byte array newContent 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 Detail

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 less than number of remaining bytes in stream and less then data.length. You must call read() function in loop, if you need 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:
amount of bytes written in the data array. 0 - represents 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 are no guarantee, that there are no further call back methods will get called. The first and last thrown exceptions will be saved and thrown later on from the originally called method such as ISevenZipInArchive.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:
amount of bytes written in the data array. 0 - represents end of stream.
Throws:
java.lang.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 - on of three possible seek origins:
  • ISeekableStream.SEEK_SET - offset is an absolute offset to move to,
  • ISeekableStream.SEEK_CUR - offset is a relative offset to the current position in stream,
  • ISeekableStream.SEEK_END - offset is an offset from the end of the stream (offset <= 0).
    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 are no guarantee, that there are no further call back methods will get called. The first and last thrown exceptions will be saved and thrown later on from the originally called method such as ISevenZipInArchive.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 less than data.length bytes. You must call Write function in loop, if you need to write 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:
    java.lang.IllegalStateException - will be thrown, if startPosition is an invalid index for the array data or if startPosition + length > data.length.

    getDetachedInputStream

    public java.io.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.

    Returns:
    detached input stream

    getInputStream

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

    Returns:
    InputStream implementation for this byte array stream

    getOutputStream

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

    Returns:
    OutputStream implementation for this byte array stream

    writeToOutputStream

    public void writeToOutputStream(java.io.OutputStream outputStream,
                                    boolean closeStreamAfterWriting)
                             throws java.io.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:
    java.io.IOException - if I/O exception occurs

    writeFromInputStream

    public void writeFromInputStream(java.io.InputStream inputStream,
                                     boolean closeStreamAfterReading)
                              throws java.io.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:
    java.io.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 newContent byte array, so the original array can be modified safely, without affecting the byte stream
    false - don't copy byte array newContent. Any change to the byte array newContent will be reflected by the byte array stream.

    close

    public void close()
               throws java.io.IOException
    Empty method. No closing required.

    Specified by:
    close in interface java.io.Closeable
    Throws:
    java.io.IOException - never
    See Also:
    Closeable