Package net.sf.sevenzipjbinding.util
Class ByteArrayStream
java.lang.Object
net.sf.sevenzipjbinding.util.ByteArrayStream
- All Implemented Interfaces:
Closeable,AutoCloseable,IInStream,IOutStream,ISeekableStream,ISequentialInStream,ISequentialOutStream
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
ConstructorsConstructorDescriptionByteArrayStream(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 tocontent.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 TypeMethodDescriptionvoidclose()Empty method.byte[]getBytes()Return the content of the byte array stream in a new byte array.intReturn 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.intgetSize()Return the size of the byte array stream content in bytes.booleanisEOF()Retrieve "End Of Stream" status of the byte array stream.intread(byte[] data) Reads at least 1 and maximumdata.lengthbytes from the in-stream.intread(byte[] data, int startPosition, int length) Readslengthbytes from the byte array stream.voidrewind()Set current pointer back to zero.longseek(long offset, int seekOrigin) Move current location pointer to the new offset depending onseekOrigin.
Note: depending on the archive format and the data size this method may be called from different threads.voidsetBytes(byte[] newContent, boolean copyNewContentArray) Reinitialize byte array stream, replace current content with the new contentnewContentand set the current position to the beginning of the stream.voidsetSize(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.voidtruncate()Clear all content of the streamintwrite(byte[] data) Writedatabyte array to the stream.intwrite(byte[] data, int startPosition, int length) Writelengthbyte from the byte arraydatabeginning from the positionstartPosition.voidwriteFromInputStream(InputStream inputStream, boolean closeStreamAfterReading) Write entire data fromInputStreaminputStreaminto byte array stream.voidwriteToOutputStream(OutputStream outputStream, boolean closeStreamAfterWriting) Write entire content of the stream to the output stream.
-
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 thecontentbyte array, so the original array can be modified safely, without affecting the byte stream
false- don't copy thecontentbyte array. Any change to the byte arraycontentwill be reflected by the byte array stream.maxSize- maximal length of the stored data. UseInteger.MAX_VALUEto 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 tocontent.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 thecontentbyte array, so the original array can be modified safely, without affecting the byte stream
false- don't copy thecontentbyte array. Any change to the byte arraycontentwill 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. UseInteger.MAX_VALUEto 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. UseInteger.MAX_VALUEto 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
Reads at least 1 and maximumdata.lengthbytes from the in-stream. Ifdata.length == 00 should be returned. Ifdata.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 thandata.length. You must call theread()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:
readin interfaceISequentialInStream- Parameters:
data- buffer to get read data- Returns:
- number of bytes read into the
dataarray; 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 asIInArchive.extract()orSevenZip.openInArchive(). Up to four exceptions depending on the situation can be saved for further analysis. SeeSevenZipExceptionandSevenZipException.printStackTraceExtended()for details.
-
read
public int read(byte[] data, int startPosition, int length) Readslengthbytes from the byte array stream. Iflength == 00 is returned. Iflength != 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 thedataarray beginning from the positionstartPosition
- Parameters:
data- buffer to get read data.startPosition- position (index) in the arraydatato store first read byte.length- count of the bytes to read.- Returns:
- number of bytes read into the
dataarray; 0 represents end of stream. - Throws:
IllegalStateException- will be thrown, ifstartPositionis an invalid index for the arraydataor ifstartPosition + length > data.length.
-
isEOF
public boolean isEOF()Retrieve "End Of Stream" status of the byte array stream.- Returns:
truethe 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
Move current location pointer to the new offset depending onseekOrigin.
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:
seekin interfaceISeekableStream- Parameters:
offset- absolute or relative offset in the stream to move toseekOrigin- one of three possible seek origins:
ISeekableStream.SEEK_SET-offsetis an absolute offset to move to,ISeekableStream.SEEK_CUR-offsetis a relative offset from the current position in the stream,ISeekableStream.SEEK_END-offsetis an offset from the end of the stream
- 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 asIInArchive.extract()orSevenZip.openInArchive(). Up to four exceptions depending on the situation can be saved for further analysis. SeeSevenZipExceptionandSevenZipException.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:
setSizein interfaceIOutStream- Parameters:
newSize- new size
-
write
public int write(byte[] data) Writedatabyte array to the stream. Ifdata.length > 0this function must write at least 1 byte. This function is allowed to write fewer thandata.lengthbytes. You must call thewrite()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:
writein interfaceISequentialOutStream- Parameters:
data- data to write- Returns:
- count of written bytes
-
write
public int write(byte[] data, int startPosition, int length) Writelengthbyte from the byte arraydatabeginning from the positionstartPosition.- Parameters:
data- data to writestartPosition- 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, ifstartPositionis an invalid index for the arraydataor ifstartPosition + length > data.length.
-
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. UsegetBytes()orwriteToOutputStream(OutputStream, boolean)instead.- Returns:
- detached input stream
- Throws:
IllegalStateException- always, since this method is not yet implemented
-
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. Useread(byte[])instead.- Returns:
InputStreamimplementation for this byte array stream- Throws:
IllegalStateException- always, since this method is not yet implemented
-
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. Usewrite(byte[])orwriteFromInputStream(InputStream, boolean)instead.- Returns:
OutputStreamimplementation 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 tocloseStreamAfterWriting-trueclose output streamoutputStreamby callingOutputStream.close()method,falsedon't close output stream- Throws:
IOException- if I/O exception occurs
-
writeFromInputStream
public void writeFromInputStream(InputStream inputStream, boolean closeStreamAfterReading) throws IOException Write entire data fromInputStreaminputStreaminto 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 callingseek(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. (SeeByteArrayStream(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 contentnewContentand set the current position to the beginning of the stream.- Parameters:
newContent- new content of the byte array streamcopyNewContentArray-true- copy thecontentbyte array, so the original array can be modified safely, without affecting the byte stream
false- don't copy thecontentbyte array. Any change to the byte arraycontentwill be reflected by the byte array stream.
-
close
Empty method. No closing required.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- never- See Also:
-