public class ZepFile extends Object
Because of the expected small size of the data, the ZepFile class operates on bytes in memory instead of through Streams.
The format for the file is very simple. All 32-bit int and 16-bit short values are encoded with the Most Significant Bit first (Java style).
4 bytes, signature: "ZEP!" or "ICZ!" 4 bytes, int value: indicating the number of data blocks. 4 bytes, int value: representing the offset in the file of the first data block. This is not used if there are no blocks. 4 bytes, int value: representing the type of the data. This value is only a hint to an application. Any number of bytes can now be placed in the file up until the offset of the first data block. This allows for any application specific information as necessary. All data blocks are contiguous in the file from the first one and each one consists of zero or more uncompressed data bytes followed by zero or more compressed data bytes. The format of each data block is: 4 bytes, int value: number of uncompressed bytes. 4 bytes, int value: number of compressed. ... followed by the uncompressed bytes and then the compressed bytes.This format allows the the individual components of the file to be traversed with no knowledge of the particulars of the content of the data.
Modifier and Type | Field and Description |
---|---|
protected int |
blockCount |
protected boolean |
clearable |
protected int |
dataOffset |
protected int |
dataType |
protected ByteArray |
myData |
protected IntArray |
offsets |
static int |
RESERVED_TYPE_IMAGES |
static int |
RESERVED_TYPE_ZEP_STORE |
protected boolean |
writable |
Constructor and Description |
---|
ZepFile()
Create an empty ZepFile for writing data to.
|
ZepFile(byte[] src,
int offset,
int length)
Decode a copy of the bytes in the ZepFile and prepare it for reading.
|
ZepFile(ByteArray data)
Decode an ZepFile and continue to use the provided ByteArray as the internal data.
|
ZepFile(int typeOfData,
ByteArray bufferToUse)
Create an ZepFile for writing data to.
|
ZepFile(RandomStream from,
boolean permanent)
An ZepFile created permanently from a RandomStream
will not be modifyable.
|
Modifier and Type | Method and Description |
---|---|
void |
addAndCompressDataBlock(byte[] uncompressed,
int uncompressedOffset,
int uncompressedLength,
byte[] toCompress,
int compressedOffset,
int compressedLength) |
void |
addAndCompressDataBlock(byte[] uncompressed,
int uncompressedOffset,
int uncompressedLength,
int[] toCompress,
int compressedOffset,
int compressedLength) |
void |
addAndCompressDataBlock(ByteArray uncompressedData,
ByteArray toCompress) |
void |
addAndCompressDataBlock(ByteArray uncompressedData,
IntArray toCompress) |
void |
addPrecompressedDataBlock(byte[] uncompressed,
int uncompressedOffset,
int uncompressedLength,
byte[] compressed,
int compressedOffset,
int compressedLength) |
void |
addPrecompressedDataBlock(ByteArray uncompressedData,
ByteArray compressedData) |
void |
clear(int typeOfData)
Clear the contents of the ZepFile and prepare it for writing.
|
protected void |
decode() |
void |
decode(byte[] src,
int offset,
int length)
Clear the ZepFile and decode a copy of the data in the bytes.
|
void |
decode(ByteArray data)
Clear the ZepFile and decode the data in the bytes and continue to use the specified
ByteArray as the internal buffer.
|
ZepFile |
decode(InputStream in)
Read all the bytes in the InputStream, close the stream and then decode the bytes.
|
protected boolean |
decodeOffsets() |
static void |
endBuildingZepFile(Object fromStartBuilding,
int numberOfBlocksWritten)
Call this after writing all the data blocks and after optionally calling prepareForSpecialReserveBytes.
|
static void |
endZepFileData(Object fromStartZepFileData,
byte[] uncompressedData,
int uncompressedDataOffset) |
ByteArray |
exposeData()
Expose and return the data buffer for the ZepFile.
|
static Deflater |
getAppropriateDeflater()
Get an Inflater that can be used to unzip the data in this file.
|
static Inflater |
getAppropriateInflater()
Get an Inflater that can be used to unzip the data in this file.
|
int |
getBlockCount() |
ICompressed |
getCompressedData(int index) |
RandomStream |
getCompressedDataAsRandomStream(int index,
boolean checkForCompressedRandomStream)
Read the data block section into memory, if it is not already in memory.
|
ByteArray |
getDataBytes(ByteArray dest)
Get a copy of the data bytes in the ICZ file.
|
ICompressed |
getRawData(int index,
ByteArray uncompressed) |
void |
getUncompressedData(int index,
ByteArray destination) |
static boolean |
hasSignature(byte[] in,
int offset,
int length) |
static boolean |
hasSignature(InputStream in) |
static void |
prepareForSpecialReserveBytes(Object fromStartBuilding)
Call this after writing all the data blocks.
|
void |
setCustomHeaderData(byte[] data,
int offset,
int length) |
static Object |
startBuildingZepFile(RandomStream out,
int zipDataType,
int numberOfSpecialReserveBytes)
Call this when building a ZepFile manually.
|
static Object |
startZepFileData(RandomStream out,
int uncompressedLength)
Call this when you are going to write out a data block to the ZepFile.
|
void |
takeData()
Use this to clear the ZepFile and release the data bytes from the internal ByteArray.
|
public static int RESERVED_TYPE_IMAGES
public static int RESERVED_TYPE_ZEP_STORE
protected ByteArray myData
protected int blockCount
protected int dataOffset
protected int dataType
protected IntArray offsets
protected boolean writable
protected boolean clearable
public ZepFile(int typeOfData, ByteArray bufferToUse)
typeOfData
- the value of the type of data field. This is data is not used by this file and is
simply stored in the file.bufferToUse
- an optional ByteArray to use. It will be cleared before use if it is not null.public ZepFile(byte[] src, int offset, int length) throws CorruptedDataException
src
- offset
- length
- CorruptedDataException
- if the file is not formatted correctly.public ZepFile(ByteArray data) throws CorruptedDataException
data
- the data to decode.CorruptedDataException
- if the file is not formatted correctly.public ZepFile(RandomStream from, boolean permanent) throws IOException
from
- permanent
- true to use the RandomStream as the source of the data,
false to read in the data into memory and then close the stream.IOException
public ZepFile()
public static boolean hasSignature(InputStream in) throws IOException
IOException
public static boolean hasSignature(byte[] in, int offset, int length)
public ZepFile decode(InputStream in) throws IOException
in
- the stream to read the bytes from.IOException
public void takeData()
public ByteArray exposeData()
public ByteArray getDataBytes(ByteArray dest)
dest
- a destination ByteArray or null to create and return a new one.
If dest is not null it is NOT cleared before data is added to it.public static final Inflater getAppropriateInflater()
public static final Deflater getAppropriateDeflater()
public void clear(int typeOfData)
public void decode(byte[] src, int offset, int length) throws CorruptedDataException
src
- offset
- length
- CorruptedDataException
public void decode(ByteArray data) throws CorruptedDataException
src
- offset
- length
- CorruptedDataException
protected void decode() throws CorruptedDataException
CorruptedDataException
protected boolean decodeOffsets()
public int getBlockCount()
public void setCustomHeaderData(byte[] data, int offset, int length)
public void addPrecompressedDataBlock(byte[] uncompressed, int uncompressedOffset, int uncompressedLength, byte[] compressed, int compressedOffset, int compressedLength)
public void addAndCompressDataBlock(byte[] uncompressed, int uncompressedOffset, int uncompressedLength, byte[] toCompress, int compressedOffset, int compressedLength)
public void addAndCompressDataBlock(byte[] uncompressed, int uncompressedOffset, int uncompressedLength, int[] toCompress, int compressedOffset, int compressedLength)
public void addPrecompressedDataBlock(ByteArray uncompressedData, ByteArray compressedData)
public void addAndCompressDataBlock(ByteArray uncompressedData, ByteArray toCompress)
public void addAndCompressDataBlock(ByteArray uncompressedData, IntArray toCompress)
public RandomStream getCompressedDataAsRandomStream(int index, boolean checkForCompressedRandomStream) throws IOException, CorruptedDataException
data
- bufferToUse
- an optional ByteArray to use to buffer the data.IOException
CorruptedDataException
public ICompressed getCompressedData(int index) throws CorruptedDataException
CorruptedDataException
public void getUncompressedData(int index, ByteArray destination) throws CorruptedDataException
CorruptedDataException
public ICompressed getRawData(int index, ByteArray uncompressed) throws CorruptedDataException
CorruptedDataException
public static Object startBuildingZepFile(RandomStream out, int zipDataType, int numberOfSpecialReserveBytes) throws IOException
First call this method and specify the zipDataType and the number of special reserve bytes required in the header.
When that method returns the RandomStream will be positioned ready for the first data blocks. Write out the data blocks contiguously to the RandomStream.
When complete optionally call prepareForSpecialReserveBytes() and then write out the special reserve bytes - no more than specified by numberOfSpecialReserveBytes.
Finally call endBuildingZepFile() specifying the number of blocks written - but note that this does not close the RandomStream.
out
- the output RandomStream. The ZepFile structure is written starting from the current location.zipDataType
- the data type - which is optional.numberOfSpecialReserveBytes
- the number of special reserve bytes which can be zero or more.IOException
public static Object startZepFileData(RandomStream out, int uncompressedLength) throws IOException
out
- the RandomStream of the ZepFile.uncompressedLength
- the amount of uncompressed data you will be writing. This data should
not be written now - it should be written at endZepFileData.IOException
public static void endZepFileData(Object fromStartZepFileData, byte[] uncompressedData, int uncompressedDataOffset) throws IOException
IOException
public static void prepareForSpecialReserveBytes(Object fromStartBuilding) throws IOException
fromStartBuilding
- the Object returned from startBuildingZepFile.IOException
public static void endBuildingZepFile(Object fromStartBuilding, int numberOfBlocksWritten) throws IOException
fromStartBuilding
- the Object returned from startBuildingZepFile.numberOfBlocksWritten
- the number of data blocks that were written.IOException