Repository navigation
Expand file tree
/
Copy pathTextHuffViewer.java
More file actions
317 lines (295 loc) · 11.3 KB
/
Copy pathTextHuffViewer.java
File metadata and controls
317 lines (295 loc) · 11.3 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Scanner;
/**
* A text based version of the IHuffViewer. Used as an alternative to the
* GUIHuffViewer. All functionality from the GUI version of the viewer is
* replicated here.
*
* @author Mike Scott
*/
public class TextHuffViewer implements IHuffViewer {
private IHuffProcessor myModel;
private File myInputFile;
private boolean myForce;
private int myHeaderFormat;
private final Scanner KEY;
private static String HUFF_SUFFIX = ".hf";
private static String UNHUFF_SUFFIX = ".unhf";
/**
* Create a text based interface Huffman viewer. After this constructor is
* called the setModel method must be called and then the start method must
* be called to allow a user to interact with the viewer.
*/
public TextHuffViewer() {
myHeaderFormat = IHuffConstants.STORE_COUNTS;
KEY = new Scanner(System.in);
}
/**
* Associates this view with the given model. The GUI (or other View) will
* attach itself to the model so that communication between the view and the
* model as well as <em>vice versa</em> is supported.
*
* @param model is the model for this view
*/
public void setModel(IHuffProcessor model) {
myModel = model;
myModel.setViewer(this);
}
/**
* To be called by model/client code to display strings in the View.
* Displays string on a single line. Call multiple times show several
* strings.
*
* @param s is string to be displayed
*/
public void update(String s) {
System.out.println(s);
}
/**
* Display a text message in the view. Messages are informational in
* purpose.
*
* @param s is the message displayed
*/
public void showMessage(String s) {
System.out.println("MESSAGE");
System.out.println(s);
System.out.println();
}
/**
* Show a message indicating an error. The user must respond with a return
* before proceeding.
*
* @param s is the error-message displayed
*/
public void showError(String s) {
System.out.println("ERROR");
System.out.println(s);
System.out.println();
System.out.println("Presss enter to continue: ");
KEY.nextLine();
System.out.println();
}
// The menu main choices for the user.
private static enum MenuChoices {
UNUSED, FILE_OP, FLIP_COMPRESSION_STATUS, FLIP_HEADER_CHOICE;
}
/**
* Start processing Huffman operation requests. Do so until the user stops.
*/
public void start() {
int choice = 1;
while (keepGoing(choice)) {
System.out.println("STATUS");
System.out.print("Current Header format for compressing: ");
if (this.myHeaderFormat == IHuffConstants.STORE_COUNTS) {
System.out.println("Standard COUNT Format");
} else {
System.out.println("Standard TREE Format");
}
System.out.println("Current force compression status: " + this.myForce);
System.out.println();
System.out.println("OPTIONS");
System.out.println("1. File operations. ");
System.out.println("2. Flip Force Compression status.");
System.out.println("3. Flip Header format.");
System.out.println("4. Quit (or any value other than 1, 2, or 3)");
System.out.println();
choice = processChoice();
}
System.out.println("Ending program.");
}
/*
* Return true if the choice is options 1 (file operation), 2 (flip force
* status), or 3 (flip header choice). False for anything else.
*/
private boolean keepGoing(int choice) {
return MenuChoices.FILE_OP.ordinal() <= choice
&& choice <= MenuChoices.FLIP_HEADER_CHOICE.ordinal();
}
/*
* Get the next choice from the user and process it.
*/
private int processChoice() {
System.out.print("Enter menu option: ");
String input = KEY.nextLine();
System.out.println();
if (input.length() == 0) {
input = "4";
}
int firstCharValue = input.charAt(0) - '0';
// If it is 1, 2, or 3 and process choice
if (keepGoing(firstCharValue)) {
if (firstCharValue == MenuChoices.FILE_OP.ordinal()) {
processFileRequest();
} else if (firstCharValue == MenuChoices.FLIP_COMPRESSION_STATUS.ordinal()) {
myForce = !myForce;
} else {
// must be a 3, flip the header format
myHeaderFormat =
(myHeaderFormat == IHuffConstants.STORE_COUNTS) ? IHuffConstants.STORE_TREE
: IHuffConstants.STORE_COUNTS;
}
}
return firstCharValue;
}
/*
* Process a file request. These are the important operations for the
* program.
*/
private void processFileRequest() {
// We assume the user shall enter a valid int for the menu choice.
// Future work will be to make this more robust.
System.out.println("FILE OPERATION");
System.out.println("1. Preprocess a file to determine if compression is possible.");
System.out.println("2. Compress a file. Results in a call to preprocessing as well.");
System.out.println("3. Uncompress a file.");
System.out.println();
System.out.print("Enter file operation option: ");
int choice = KEY.nextInt();
KEY.nextLine();
System.out.println();
if (choice == 1) {
setMyFile("preprocess only"); // Gross! An array for choices?
preprocess();
} else if (choice == 2) {
setMyFile("compress");
compress();
} else {
setMyFile("decompress");
uncompress();
}
}
/*
* Process a file to determine the amount of bits saved if it were to be
* compressed.
*/
private void preprocess() {
showMessage("Begining preprocessing of " + myInputFile + ".");
ByteArrayInputStream inputStream = getFastByteReader(myInputFile);
try {
int saved = myModel.preprocessCompress(inputStream, myHeaderFormat);
showMessage("save(d): " + saved + " bits");
} catch (IOException e) {
showError("Error / Exception while reading file for preprocessing.");
e.printStackTrace();
}
}
// Compress a file based on user input.
private void compress() {
preprocess();
String newName = myInputFile.getName() + HUFF_SUFFIX;
newName = setNewFileName("compressed", newName);
if (newName == null) {
showError("Trouble with file setting path of output file. Exiting compress.");
} else {
try {
FileOutputStream out = new FileOutputStream(new File(newName));
ByteArrayInputStream inputStream = getFastByteReader(myInputFile);
myModel.compress(inputStream, out, myForce);
} catch (IOException e) {
showError("Problem while trying to compress file.");
e.printStackTrace();
}
}
myInputFile = null; // must enter new file after this.
}
/*
* Set the name of the output file. This can be for
* a compressed or uncompressed file. The parameter
* type specifies if the output file is for compression
* or uncompression. Allow the user to alter the name
* of the output file if they wish.
*/
private String setNewFileName(String type, String newName) {
System.out.println("Proposed name for " + type + " file: " + newName);
System.out.print("Enter c to change " + type + " file name. "
+ "Anything else to keep proposed name: ");
String response = KEY.nextLine();
System.out.println();
if (response.length() == 1 && response.charAt(0) == 'c') {
System.out.print("Enter name for " + type + " file: ");
newName = KEY.nextLine();
System.out.println();
}
newName = setFullPath(newName);
if (newName == null) {
showError("Trouble with file setting path of output file.");
}
return newName;
}
/*
* Set the newName to have the full path of the original file.
* In other words, the compressed file will be placed in the same
* directory as the file we are compressing.
* Return the resulting path or null if a an IOException occurs
* while determining the canonical path of the input file.
*/
private String setFullPath(String newName) {
String result = null;
try {
String path = myInputFile.getCanonicalPath();
int pos = path.lastIndexOf(myInputFile.getName());
result = path.substring(0, pos) + newName;
} catch (IOException e) {
showError("Problem in trying to obtain Canoical Path of file " + newName);
e.printStackTrace();
}
return result;
}
/*
* Uncompress a file. Ask the user for the input file
* and the name for the uncompressed output file.
* Then uncompress the file using the current processor.
*/
private void uncompress() {
showMessage("Uncompressing file based on user choice.");
String name = myInputFile.getName();
String uncompressedName = name;
if (name.endsWith(HUFF_SUFFIX)) {
uncompressedName =
name.substring(0, name.length() - HUFF_SUFFIX.length()) + UNHUFF_SUFFIX;
} else {
uncompressedName = name + UNHUFF_SUFFIX;
}
uncompressedName = setNewFileName("compressed", uncompressedName);
try {
FileOutputStream out = new FileOutputStream(new File(uncompressedName));
ByteArrayInputStream inputStream = getFastByteReader(myInputFile);
myModel.uncompress(inputStream, out);
} catch (IOException e) {
showError("Problem while trying to uncompress file.");
e.printStackTrace();
}
}
// Set myFile based on user input.
private void setMyFile(String prompt) {
System.out.print("Enter name (full or relative path) of file to " + prompt + ": ");
String path = KEY.nextLine();
System.out.println();
myInputFile = new File(path);
}
// Get a ByteArrayInputStream to read from the given file.
private ByteArrayInputStream getFastByteReader(File f) {
try {
FileChannel channel = new FileInputStream(f).getChannel();
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
byte[] barray = new byte[buffer.limit()];
if (barray.length != channel.size()) {
showError(String.format("Reading %s error: lengths differ %d %ld\n", f.getName(),
barray.length, channel.size()));
}
buffer.get(barray);
return new ByteArrayInputStream(barray);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}