If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Lzw

Hello....

I have written a little LZW copmression program... the program works fine, however, at some point int some files, it will stop working properly and starts giving strange characters. this happens only with large files.

for example if I have one sentence in the file, everything is fine. When I copy it in the file 900 times, everything is still fine, when I copy it 1800 times, it will start to give wrong output.

/* read the bits from the file, 12 bits each time.
* Then change the the code into an integer.
* Break when the end of the file is reached.
* End of file is -1.
*/
while (!eof) {
for (int i = 0; i < 12; i++) {
bit = input.read();

if (bit == -1) {
eof = true;
break;
}
else
code += (bit - 48);
}

/* if the end of file is not reached yet then
* convert the most recently read 12 bits into
* an integer and store it in the vector.
*/
if (!eof) {
integerCode = Integer.parseInt(code, 2);
toBeDecoded.add(new Integer(integerCode));
code = "";
}
}//end of while.
}
catch (IOException e){
System.out.println(e);
}
}
//--------------------------------------

Pretty hairy stuff. Why this happends I don't know, but I fail to find any close() -ing of
the files you use, that seems a bit risky to me. And I would use a StringBuffer
instead of a String when there is a lot of += going on. The buffer could be
set up initially to match the filesize.

But what really beats me is why you want this to stand a test of 1800
consecutive and identical (and useless) operations , kind of like
testing tyres with a drill...