-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEncoderDecoder.java
266 lines (233 loc) · 9.33 KB
/
EncoderDecoder.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
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
//EncoderDecoder.java
//Serpa Chaves:Thais:u34
//Submission 03
//Enconding and Decoding Textfiles
/*
* Program works as expected
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
/*
* A class to encode and decode files of plain text, one file per run,
* and write the encoded/decoded text to an output textfile. The names of
* the input and output files are both read from the command line
*/
public class EncoderDecoder {
private String inFileName;
private String outFileName;
private Scanner inputFile;
private PrintWriter outputFile;
private List<String> originallyDecodedLines;
private List<String> encodedLines;
private List<String> originallyEncodedLines;
private List<String> decodedLines;
private static String function;
// Constructor
public EncoderDecoder(String inFileName,
String outFileName) throws FileNotFoundException {
this.inFileName = inFileName;
this.outFileName = outFileName;
this.inputFile = new Scanner(new File(inFileName));
this.outputFile = new PrintWriter(new File(outFileName));
this.originallyDecodedLines = new ArrayList<>();
this.encodedLines = new ArrayList<>();
this.originallyEncodedLines = new ArrayList<>();
this.decodedLines = new ArrayList<>();
this.function = function;
}
public static void main(String[] args) throws FileNotFoundException {
if (args.length != 3) {
displayOpeningScreenAndProgramInfo();
return;
}
function = args[0];
String inputFileName = args[1];
String outputFileName = args[2];
try {
if (function.equals("e")) {
EncoderDecoder encoder = new EncoderDecoder(inputFileName,
outputFileName);
encoder.readPlainText(inputFileName);
encoder.encodePlainText();
encoder.writeOutputFile();
encoder.closeOpenedFiles();
System.out.println("");
System.out.println("The input file " + inputFileName +
" has been encoded");
System.out.println("and output to the file " + outputFileName
+ ".");
System.out.println("");
Utils.pause();
} else if (function.equals("d")) {
EncoderDecoder decoder = new EncoderDecoder(inputFileName,
outputFileName);
decoder.readEncodedText();
decoder.decodeEncodedText();
decoder.writeOutputFile();
decoder.closeOpenedFiles();
System.out.println("");
System.out.println("The input file " + inputFileName +
" has been decoded");
System.out.println("and output to the file " + outputFileName
+ ".");
System.out.println("");
Utils.pause();
} else {
System.out.println("");
System.out.println("Bad first parameter.");
System.out.println("Program now terminating.");
System.out.println("");
Utils.pause();
System.exit(1);
}
} catch (FileNotFoundException e) {
System.out.println("");
System.out.println("Problem opening input file "
+ inputFileName + ".");
System.out.println("Program now terminating.");
System.out.println("");
Utils.pause();
System.exit(1);
}
}
/*
* This method reads the lines of plain text from an input file into
* this Decoder object. Displays a message, followed by a pause, if the
* input file of plain text contains a line longer than the maximum of
* 72 characters
*/
public void readPlainText(String inputFileName) {
int lineNum = 0;
while (inputFile.hasNextLine()) {
String line = inputFile.nextLine();
lineNum++;
if (line.length() > 72) {
System.out.println("");
System.out.println("Line " + lineNum + " of input file " +
inFileName + " exceeds maximum length" +
"of 72 characters.");
System.out.println("Program now terminating.");
Utils.pause();
System.exit(1);
}
originallyDecodedLines.add(line);
}
}
/*
* This method reads the lines of encoded text from an input file into
* this Decoder object.
**/
public void readEncodedText() {
while (inputFile.hasNextLine()) {
originallyEncodedLines.add(inputFile.nextLine());
}
}
/*
* This method encodes the lines of plain text stored in this encoder
* object "in place". That is, each line of plain text is replaced
* by its corresponding line of encoded text.
*/
public void encodePlainText() {
// generating random shift value
int shiftValue = (int) (Math.random() * 94) + 1;
for (int i = 0; i < originallyDecodedLines.size(); i++) {
// padding lines to 72 characters
StringBuilder paddedLine = new StringBuilder(originallyDecodedLines.get(i));
while (paddedLine.length() < 72) {
char randomCharacter = (char) (Math.random() * 95 + 32);
paddedLine.append(randomCharacter);
}
// encoding padded line
StringBuilder encodingLine = new StringBuilder();
for (int j = 0; j < paddedLine.length(); j++) {
char originalChar = paddedLine.charAt(j);
int shiftedChar = (int) (originalChar) + shiftValue;
// dealing with wrap around chars
if (shiftedChar > 126) {
shiftedChar -= 95;
}
encodingLine.append((char) shiftedChar);
}
// adding last 4 characters
int originalLength = originallyDecodedLines.get(i).length();
encodingLine.append((char) (70 - (shiftValue / 10)));
encodingLine.append((char) (90 - (originalLength / 10)));
encodingLine.append((char) (70 + (shiftValue % 10)));
encodingLine.append((char) (90 + (originalLength % 10)));
encodedLines.add(encodingLine.toString());
}
}
/*
* This method decodes the lines of encoded text stored in this Decoder
* object "in place". That is, each line of encoded text is replaced by
* its corresponding line of (decoded) plain text.
**/
public void decodeEncodedText() {
for (int i = 0; i < originallyEncodedLines.size(); i++) {
String encodedLine = originallyEncodedLines.get(i);
// Find shift
int tensDigitOfShift = 70 - encodedLine.charAt(72);
int unitsDigitOfShift = encodedLine.charAt(74) - 70;
int shift = tensDigitOfShift * 10 + unitsDigitOfShift;
// find original line length
int tensDigitLength = 90 - encodedLine.charAt(73);
int unitsDigitLength = encodedLine.charAt(75) - 90;
int originalLength = tensDigitLength * 10 + unitsDigitLength;
// Decode original words
StringBuilder decodedLine = new StringBuilder();
for (int j = 0; j < 72; j++) {
char encodedChar = encodedLine.charAt(j);
int charValue = (int) encodedChar;
int decodedValue = charValue - shift;
// For when the value needs to loop around
if (decodedValue < 32) {
decodedValue += 95;
}
decodedLine.append((char) decodedValue);
}
// Trim line to original length
decodedLines.add(decodedLine.substring(0, originalLength));
}
}
/*
* This method write all lines of (encoded or decoded) text to
* an output file.
*/
public void writeOutputFile() {
if (function.equals("e")) {
for (int i = 0; i < encodedLines.size(); i++) {
outputFile.println(encodedLines.get(i));
}
}
if (function.equals("d")) {
for (int i = 0; i < decodedLines.size(); i++) {
outputFile.println(decodedLines.get(i));
}
}
}
/*
* This method close both the input file and the output file.
**/
public void closeOpenedFiles() {
inputFile.close();
outputFile.close();
}
public static void displayOpeningScreenAndProgramInfo() {
OpeningScreen openingScreen = new OpeningScreen(
"Serpa Chaves:Thais:A00462622:u34",
"Submission 03",
"Encoding and Decoding Textfiles");
openingScreen.display();
try {
TextItems textItem = new TextItems(
EncoderDecoder.class.getResourceAsStream("EncoderDecoder.txt"));
textItem.displayItem("ProgramInfo");
} catch (NullPointerException e) {
System.out.println("Could not open file EncoderDecoder.txt.");
System.out.println("Program now terminating.");
System.exit(1);
}
}
}