-
Notifications
You must be signed in to change notification settings - Fork 0
/
IO.java
159 lines (128 loc) · 3.82 KB
/
IO.java
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*********************
* Author: Maxx Boehme
*********************/
import java.util.*;
import java.io.*;
public class IO {
public static final boolean DEBUGGING = true; // Set to TRUE to print debugging information
public static final int BLOCK_SIZE = 128; // Don't touch
// File extensions
public static final String COMPRESSED_FILE_EXTENSION = "cpz";
public static final String DECOMPRESSED_FILE_EXTENSION = "dcz";
/* Pair class definition */
static class Pair {
private boolean valid;
final private int index;
final private char character;
Pair(int index, char character) {
this(index, character, true);
}
Pair(int index, char character, boolean _v) {
assert (((int)character) < BLOCK_SIZE);
this.index = index;
this.character = character;
this.setValid(_v);
}
public int getIndex() {
return index;
}
public char getCharacter() {
return character;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
}
/* Decompressor class definition */
public static class Decompressor {
private boolean finish;
private FileWriter oWriter;
private String lastString;
private int cursor;
private Vector output;
Decompressor(String inFile) throws Exception {
lastString = "";
finish = false;
cursor = 0;
output = new Vector();
FileInputStream inStream = new FileInputStream(inFile);
FileWriter fw = new FileWriter(inFile + "." + DECOMPRESSED_FILE_EXTENSION );
oWriter = fw;
while(true) {
int indexHigh = (byte)inStream.read();
if(indexHigh == -1)
break ;
int indexMiddle = (byte) inStream.read();
int indexLow = (byte) inStream.read();
int index = indexHigh*BLOCK_SIZE*BLOCK_SIZE + indexMiddle*BLOCK_SIZE + indexLow;
char c = (char) inStream.read();
output.add(new Pair(index, c));
}
}
public void finalize() throws Exception {
oWriter.close();
}
public Pair decode() {
Pair ret;
if(cursor < output.size()) {
ret = (Pair)output.get(cursor++);
ret.setValid(true);
} else {
ret = new Pair(0,'a', false);
}
return ret;
}
public void append(String op) throws Exception {
oWriter.write(lastString);
lastString = new String(op);
}
}
/* Compressor class definition */
static class Compressor {
private FileOutputStream oStream;
private Vector output;
private File inFile, outFile;
Compressor (String fileName) throws Exception {
String outfileName = fileName + "." + COMPRESSED_FILE_EXTENSION;
output = new Vector();
outFile = new File(outfileName);
oStream = new FileOutputStream(outFile);
inFile = new File(fileName);
}
public void encode(int index, char character) throws Exception {
output.add(new Pair(index, character));
int xdash = index / BLOCK_SIZE;
int xLowest = index % BLOCK_SIZE;
int xdashdash = xdash / BLOCK_SIZE;
int xMiddle = xdash % BLOCK_SIZE;
oStream.write((byte) xdashdash);
oStream.write((byte) xMiddle);
oStream.write((byte) xLowest);
oStream.write(character);
if(DEBUGGING) {
// This is to help you during debugging, please comment this out for large files
System.out.println(" ( " + index + ", " + character + " )");
}
}
public void finalize() throws Exception {
oStream.close();
}
public char[] getCharacters() throws Exception {
char[] ret = new char[(int) (inFile.length()+1)];
int retCursor = 0;
FileInputStream from = new FileInputStream(inFile);
while (true) {
byte nextByte = (byte) from.read();
if (nextByte == -1)
break;
char nextChar = (char) nextByte;
ret[retCursor++] = nextChar;
}
ret[(int)(inFile.length())] = '\000';
return ret;
}
}
}