-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileTransformer.java
More file actions
104 lines (85 loc) · 2.56 KB
/
FileTransformer.java
File metadata and controls
104 lines (85 loc) · 2.56 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
package turner.bsbsnd.crypto;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Encrypt or decrypt a file using a character shift algorithm.
* each character is shifted +7 to encrypt and -7 to decrypt.
*
* NOTE: I SUSPECT THERE WAS SOMETHING MISSING/INCORRECT WITH THE TEST DATA I WAS PROVIDED.
* * Line 1 of the text decrypts as "oridian". I suspect that this was intended to be "Noridian"(missing N).
* However, if my decryption algorithm is correct, the encrpted input text should have began with U'('N' encrypted),
* but it does not.
*
*
* @author Jonathan Turner
*
*/
public class FileTransformer {
/** helper method
* @param inputFile
* @param outputFile
* @return
*/
public boolean decodeFile(String inputFile, String outputFile)
{
return transformFile(true,inputFile,outputFile);
}
/** helper method
* @param inputFile
* @param outputFile
* @return
*/
public boolean encodeFile(String inputFile, String outputFile)
{
return transformFile(false,inputFile,outputFile);
}
/**
* encode or decode a file. Algorithm is a "shift" type. Add or subtract 7 to/from character codes.
*
*
* @param boolean decode - true if decoding, false id encrypting.
* @param inputFile
* @param outputFile
* @return
*/
private boolean transformFile(boolean decode, String inputFile, String outputFile)
{
try {
// open the input and output files
FileInputStream inStream = new FileInputStream(inputFile);
FileOutputStream outStream = new FileOutputStream(outputFile);
// read the contents of the input file into a byte array.
byte[] bytes = new byte[inStream.available()];
inStream.read(bytes);
String inputString= new String(bytes);
String outputString="";
// // iterate through the bytes and transform each character by subtracting 7 from its value as an it, skipping the EOL characters.
for (int i=0;i<bytes.length;i++)
{
char oldChar = inputString.charAt(i);
int newChar= oldChar;
if ((newChar != 13) &&( newChar !=10))
{
if (decode== true)
newChar= oldChar -7;
else
newChar= oldChar +7;
}
outputString += (char)newChar;
}
//write out the result and close files.
outStream.write(outputString.getBytes());
inStream.close();
outStream.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
}
return true;
}
}