Preview only show first 10 pages with watermark. For full document please download

Lzw - Lzw

Programa de compressão de dados

   EMBED


Share

Transcript

/*, * LZW.java, *, * Created on 01 Dec 2005, *, * Implementation of LZW compression/decompression algorithm, */, , import java.io.* ;, , /**, *, * @author Moshe Fresko, * @course Algorithmic Programming 1, * @exercise 3, */, , public class LZW implements Compression, {, boolean stopped = false ;, , Dict dict ;, // The bits that should be written for each code, int numOfBits ;, // The previous string that we should remember, // in order to insert into the dictionary, final ByteArray emptyBA = new ByteArray() ;, ByteArray w=emptyBA ;, // Constructor gets the number of bits to be written for each code, public LZW(), {, numOfBits = 12 ;, // Create a new Limited Dictionary, // For maximum of 2^bits entries, dict = new LimitedDict(1<=0) {, if (stopped), break ;, code = encodeOneChar(next) ;, if (code>=0), writeCode(os,code) ; }, code = encodeLast() ;, if (code>=0), writeCode(os,code) ; os.flush() ;, }, , ByteArray decodeOne(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 void decompress(InputStream is, OutputStream os) throws IOException { 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()) ; } } public void stop() { stopped = true ; } } "