forked from echoi3-hw/LZW-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncoder.java
More file actions
107 lines (78 loc) · 3.12 KB
/
Encoder.java
File metadata and controls
107 lines (78 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
* 1. read the text until we find. pattern that is not in our table
2. put that pattern into the table, output the code for everything BUT the last letter of that new pattern
3. reset to only include what we have not output a code for
*/
public class Encoder
{
//make table that has the list of string and their value
private ArrayList<String> table = new ArrayList<String>();
//makes arrayList that stores LZW code
private ArrayList<Integer> code = new ArrayList<Integer>();
private String prefix = "";
private String pattern = ""; // represents everything but the newest read char
private char readchar = 0; //this will represent the last char when checking a substring
//empty constructor
public Encoder ()
{
}
public void encode (String fileName) throws IOException
{
try {
//make buffered reader and file reader
BufferedReader br = new BufferedReader(new FileReader(fileName));
//make printwriter so we can print as we encode
PrintWriter writer = new PrintWriter(new FileWriter(fileName + "encoded"));
for (int i=0; i<95; i++)
{
table.add(""+(char)(i+32)); //inputs values into table that are already in the ascii table; began at 33rd character to avoid weird chars
}
while (br.ready()) //read in file to code and add to input
{
readchar = (char) br.read(); //reads in one char from file
pattern = prefix + readchar; //adds the char onto what has been read so far
if (table.contains(pattern) || pattern.length()==1) { //checks if the table alrady contains this pattern/if it's already part of the ascii table
prefix = pattern; //sets prefix to the pattern; now when pattern = prefix + readchar loops it'll include what's alrady been read
code.add((int)readchar); //adds the char's index to the table
}
else {
if (prefix.length()==1) { //checks if it is in ascii table as a single letter
code.add((int)pattern.charAt(0)); //adds the index to the list of codes
writer.print((int)pattern.charAt(0) + ","); //prints the code of this pattern
}
else//if the pattern is not in table, it adds it to the table. also print this pattern
{
code.add (33+table.indexOf(prefix)); //adds value of everything but last letter to code
writer.print(33+table.indexOf(prefix) +","); //prints the code of this pattern
}
prefix = "" + readchar; // resets with only the last char of the sequence
table.add(pattern); //adds this pattern to the table
}
}
//when you reach the end - print what you have in your prefix
if (prefix.length() == 1)
{
//prints the code for anything in the ascii table from index 0-127
writer.print((int)prefix.charAt(0) );
}
else
{
//prints the code for any pattern in the table that's been added; codes past 127
writer.print(128+table.indexOf(prefix));
}
//save and close
br.close();
writer.close();
}
catch (IOException e)
{
System.out.println("cannot read");
}
}
}