FileDocCategorySizeDatePackage
LZW.javaAPI DocApache Poi 3.0.19071Mon Jun 18 19:04:08 BST 2007org.apache.poi.hdgf.other_lzw

LZW

public class LZW extends Object
author
Moshe Fresko
course
Algorithmic Programming 1
exercise
3

Fields Summary
boolean
stopped
Dict
dict
int
numOfBits
final ByteArray
emptyBA
ByteArray
w
Constructors Summary
public LZW()

		// Constructor gets the number of bits to be written for each code
	 
	
		numOfBits = 12 ;
			// Create a new Limited Dictionary
			// For maximum of 2^bits entries
		dict = new LimitedDict(1<<numOfBits) ;
			// Add all ascii characters to the dictionary
		for (int i=0;i<256;++i)
			dict.add(new ByteArray((byte)i)) ;
	
Methods Summary
org.apache.poi.hdgf.other_lzw.LZW$ByteArraydecodeOne(int code)

			// Either "ABA" or null, w="AB"
		ByteArray str = dict.strFromNum(code) ;
		if (str==null) {
			str = w.conc(w.getAt(0)) ;
			dict.add(str) ;
		} else
			if (! w.isEmpty())
				dict.add(w.conc(str.getAt(0))) ;
            w = str ;
		return w ;
	
public voiddecompress(java.io.InputStream is, java.io.OutputStream os)

		is = new BitInputStream(is) ;
		ByteArray str ;	// Next entry
		int code ;		// Next code to be read
		while ((code=readCode(is))>=0) {
			if (stopped)
				break ;
			str = decodeOne(code) ;
			os.write(str.getBytes()) ;
		}
	
intencodeLast()

		ByteArray nw = w ;
		w = emptyBA ;
		return dict.numFromStr(nw) ;
	
intencodeOneChar(int n)

		byte c = (byte) n ;
		ByteArray nw = w.conc(c) ;
		int code = dict.numFromStr(nw) ;
			// if it exists then we continue to search for a longer string
		if (code!=-1) {
			w = nw ;
			return -1 ;
		} else {
			dict.add(nw) ;
			nw = w ;
			w = new ByteArray(c) ;
			return dict.numFromStr(nw) ;
		}
	
intreadCode(java.io.InputStream is)

		int num = 0 ;
		for (int i=0;i<numOfBits;++i) {
			int next = is.read() ;
			if (next<0)
				return -1 ;
			num += next<<i ;
		}
		return num ;
	
public voidstop()

 stopped = true ; 
voidwriteCode(java.io.OutputStream os, int code)

		for (int i=0;i<numOfBits;++i) {
			os.write(code&1) ;
			code /= 2 ;
		}