net.freeutils.tnef

Class CompressedRTFInputStream

public class CompressedRTFInputStream extends InputStream

The CompressedRTFInputStream class provides decompression of compressed RTF data.

Since: 2009-10-12

Field Summary
protected byte[]buf
protected intbufend
protected intbufstart
protected intcompressedSize
protected intcrc
protected intcrc32
protected static byte[]COMPRESSED_RTF_PREBUF
Prebuffered bytes used in RTF-compressed format.
protected static int[]CRC32_TABLE
The lookup table used in the CRC32 calculation
protected byte[]dict
protected intdictstart
protected static intDICT_SIZE
protected intflagCount
protected intflags
protected InputStreamin
protected intmagic
protected static intMAGIC_COMPRESSED
protected static intMAGIC_UNCOMPRESSED
protected intout
protected intuncompressedSize
Constructor Summary
CompressedRTFInputStream(InputStream in)
Constructs a new CompressedRTFInputStream which reads the compressed data from the given underlying stream.
Method Summary
intavailable()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.
static intcalculateCRC32(byte[] buf, int off, int len)
Calculates the CRC32 of the given bytes.
voidclose()
Closes this input stream and releases any system resources associated with the stream.
static byte[]decompressRTF(byte[] src)
Decompresses compressed-RTF data.
protected voidinit()
Initializes this stream by reading the compressed RTF header data.
protected booleanmore()
Reads more data into the dictionary buffer.
protected booleanmoreCompressed()
Reads (and decompresses) more compressed data into the dictionary buffer.
protected booleanmoreUncompressed()
Reads more uncompressed data into the dictionary buffer.
intread()
Reads the next byte of data from the input stream.
intread(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes.

Field Detail

buf

protected final byte[] buf

bufend

protected int bufend

bufstart

protected int bufstart

compressedSize

protected int compressedSize

crc

protected int crc

crc32

protected int crc32

COMPRESSED_RTF_PREBUF

protected static byte[] COMPRESSED_RTF_PREBUF
Prebuffered bytes used in RTF-compressed format.

CRC32_TABLE

protected static int[] CRC32_TABLE
The lookup table used in the CRC32 calculation

dict

protected final byte[] dict

dictstart

protected int dictstart

DICT_SIZE

protected static final int DICT_SIZE

flagCount

protected int flagCount

flags

protected int flags

in

protected final InputStream in

magic

protected int magic

MAGIC_COMPRESSED

protected static final int MAGIC_COMPRESSED

MAGIC_UNCOMPRESSED

protected static final int MAGIC_UNCOMPRESSED

out

protected int out

uncompressedSize

protected int uncompressedSize

Constructor Detail

CompressedRTFInputStream

public CompressedRTFInputStream(InputStream in)
Constructs a new CompressedRTFInputStream which reads the compressed data from the given underlying stream.

Parameters: in the stream containing the compressed data

Throws: IOException if an error occurs

Method Detail

available

public int available()
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

Returns: an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking or {@code 0} when it reaches the end of the input stream.

Throws: IOException if an error occurs

calculateCRC32

public static int calculateCRC32(byte[] buf, int off, int len)
Calculates the CRC32 of the given bytes. The CRC32 calculation is similar to the standard one as demonstrated in RFC 1952, but with the inversion (before and after the calculation) omitted.

Parameters: buf the byte array to calculate CRC32 on off the offset of buf at which the CRC32 calculation will start len the number of bytes on which to calculate the CRC32

Returns: the CRC32 value

close

public void close()
Closes this input stream and releases any system resources associated with the stream.

Throws: IOException if an error occurs

decompressRTF

public static byte[] decompressRTF(byte[] src)
Decompresses compressed-RTF data.

This standalone method decompresses the data using a somewhat different algorithm than this stream's algorithm. It trades memory footprint (twice the size of the entire output is kept in memory) for speed (an order of magnitude faster than the stream version).

It also serves as a concise reference implementation for the decompression algorithm.

Parameters: src the compressed-RTF data bytes

Returns: an array containing the decompressed bytes

Throws: IllegalArgumentException if src does not contain valid compressed-RTF bytes

init

protected void init()
Initializes this stream by reading the compressed RTF header data.

Throws: IOException if an error occurs

more

protected boolean more()
Reads more data into the dictionary buffer.

Returns: true if more data was read, false if there is no more data available

Throws: IOException if an error occurs

moreCompressed

protected boolean moreCompressed()
Reads (and decompresses) more compressed data into the dictionary buffer.

Returns: true if more data was read, false if there is no more data available

Throws: IOException if an error occurs

moreUncompressed

protected boolean moreUncompressed()
Reads more uncompressed data into the dictionary buffer.

Returns: true if more data was read, false if there is no more data available

Throws: IOException if an error occurs

read

public int read()
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Returns: the next byte of data, or -1 if the end of the stream is reached

Throws: IOException if an error occurs

read

public int read(byte[] b, int off, int len)
Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.

This method blocks until input data is available, end of file is detected, or an exception is thrown.

If len is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at end of file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

Parameters: b the buffer into which the data is read off the start offset in array b at which the data is written len the maximum number of bytes to read

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached

Throws: IOException If the first byte cannot be read for any reason other than end of file, or if the input stream has been closed, or if some other I/O error occurs NullPointerException If b is null IndexOutOfBoundsException If off is negative, len is negative, or len is greater than b.length - off