-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSliding Window Decompression.java
More file actions
61 lines (58 loc) · 2.22 KB
/
Sliding Window Decompression.java
File metadata and controls
61 lines (58 loc) · 2.22 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
import java.io.*;
import java.util.ArrayList;
public class P4_Wadaskar_Richa_SlidingWindowDecompression
{
public static void main(String[] args){
//specialChar can be replaced with the special character chosen to compress a file.
char specialChar = 7;
ArrayList<Character> fullFile = new ArrayList<Character>();
ArrayList<Character> output = new ArrayList<Character>();
FileReader inFile = null;
FileWriter outFile = null;
int positionWindow = 0;
int lengthChars = 0;
int windowSize = 30;
try {
//catsupply_output.txt can be replaced with any text file.
inFile = new FileReader("catsupply_output.txt");
outFile = new FileWriter(new File("outputFileCatSupply.txt"));
while(inFile.ready()) {
char c = (char)inFile.read();
fullFile.add(c);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
for(int i = 0; i < fullFile.size(); i++){
char a = (char)fullFile.get(i);
if(a == specialChar){
positionWindow = (int)fullFile.get(i+1);
lengthChars = (int)fullFile.get(i+2);
int moveBack = windowSize - positionWindow;
String repeat = findRepeats(moveBack, lengthChars, output);
for(int m = 0; m < repeat.length(); m++){
output.add(repeat.charAt(m));
}
i+=2;
} else {
output.add(fullFile.get(i));
}
}
System.out.println(output);
try{
for(int k = 0; k < output.size(); k++){
outFile.write(output.get(k));
}
outFile.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public static String findRepeats(int moveBack, int lengthChars, ArrayList<Character> output){
String A = "";
for(int j = output.size() - moveBack; j < output.size() - moveBack + lengthChars; j++){
A += (char)output.get(j);
}
return A;
}
}