Help! My final project for a class involves writing a program that both compresses a file (dictionary must use a hash map) and decompresses a file (using an array list). The program uses the BitInputStream and BitOutputStream classes. I have spent weeks trying to get this to work and I cannot (and the project is already overdue by a day!) Can anyone help? Here is what I have...
/** A instance of this class has methods that perform LZW compression and
** decompression. It assumes that the source alphabet is comprised of the
** 128 ASCII chars. (Hence, it can compress/decompress only text files.)
**
** @author Kevin Behr and R. McCloskey
** @version December 2006
**
** Logic for Compression/Decompression algorithms obtained from
** http://marknelson.us/1989/10/01/lzw-data-compression/
*/
import java.io.*;
import java.util.HashMap; // to represent dictionary in compress()
import java.util.ArrayList; // to represent dictionary in decompress()
public class LZWForText3 {
/** <insert comments describing what this method does>
*/
public void compress(InputStream input, OutputStream output)
throws IOException
{
// for doing bit-oriented writing
BitOutputStream bos = new BitOutputStream(output);
/* initialize the dictionary to contain an entry for each
** length one string corresponding to an ASCII character
*/
HashMap<String, Integer> hMap = new HashMap<String, Integer>();
for(int i=0; i <= 127; i++)
{
String z = Character.toString((char)i);
hMap.put(z,i);
}
/* read the input data from 'input' and
** write its compressed form to 'bos'
*/
int read = input.read();
String str = Character.toString((char)read);
int code;
int SIZE = 128;
read = input.read();
while(read != -1)
{
if(hMap.containsKey(str + read))
{
str = str + read;
}
else
{
code = ((Integer)hMap.get(str)).intValue();
bos.writeBit(code);
hMap.put(str + read, SIZE);
str = Character.toString((char)read);
}
code = ((Integer)hMap.get(str)).intValue();
bos.writeBit(code);
bos.flush();
}
}
/** <insert comments describing what this method does>
*/
public void decompress(InputStream input, OutputStream output)
throws IOException
{
// for doing bit-oriented reading (as opposed to byte-oriented)
BitInputStream bis = new BitInputStream(input);
// for doing character-oriented writing (as opposed to byte-oriented)
PrintWriter pw = new PrintWriter(output);
/* initialize the dictionary to contain an entry for each
** length one string corresponding to an ASCII character
*/
ArrayList<String> aList = new ArrayList<String>();
for(int i=0; i <= 127; i++)
{
String z = Character.toString((char)i);
aList.add(z);
}
/* read the input data from 'bis' and
** write its decompressed form to 'pw'
*/
int SIZE = 128;
int read = bis.readBit();
String str = Character.toString((char)read);
String str2 = "";
while((read = bis.readBit()) != -1)
{
pw.print(str);
read = bis.readBit();
if(read == SIZE)
{
str2 = str + str.charAt(0);
}
else
{
str2 = aList.get(read);
}
aList.add(SIZE, str + str2.charAt(0));
SIZE++;
str = str2;
}
pw.print(aList.get(0));
pw.flush();
}
}