-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInane.java
64 lines (51 loc) · 2.61 KB
/
Inane.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
import java.io.File;
import java.io.FileReader;
import java.io.FileOutputStream;
public class Inane {
public static void main(String[] args) {
/* args[0] is expected to be the name of the file to read input code from.
So we open the file and read all the characters into a character array
*/
// If the user doesn't specify any parameters while calling this code then
// inform the user and exit'
if (args.length == 0) {
System.out.println("Please re-run the program and specify a file");
return;
}
char[] source = new char[(int) new File(args[0]).length()]; // Use the length of the file to setup a fixed length character array
byte[] output = new byte[65536]; // This shall be our output byte array. Its 64KB
int outputPointer = 0; // Pointer to the output byte to operate on. Initially points to the first byte
FileOutputStream fout = null; // A File Output Stream so we can write the byte array to a file
// Initialize all bytes in the output byte array to 0
for (int i = 0; i < 65536; i++) {
output[i] = 0;
}
try {
FileReader reader = new FileReader(args[0]); // Create a file reader given the file to read from
reader.read(source); // Read all characters from the file into the character array
reader.close(); // And close the file immediately
fout = new FileOutputStream("b.bin"); // Pass a file name to the FileOutputStream
// Loop through all the characters in the source character array
for (char c : source) {
switch (c) {
case '+':
output[outputPointer] |= 1; // If we detected '+' then set the least significant bit of current output byte
break;
case '-':
output[outputPointer] <<= 1; // If we detected '-' then shift current output byte left by 1 bit
break;
case '>':
outputPointer += 1; // If we detected '>' then point to the next byte in the output
break;
default:
break; // Effectively just ignore every other character
}
}
// Write the output byte array to a file
fout.write(output);
}
catch(Exception e) {
e.getMessage();
}
}
}