-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUnscrambleBug.java
More file actions
115 lines (105 loc) · 2.66 KB
/
Copy pathUnscrambleBug.java
File metadata and controls
115 lines (105 loc) · 2.66 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
import java.io.FilterInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
public class UnscrambleBug{
public static void main(String[] args){
if(args.length!=2){
System.err.println("usage: java Unscramble srcpath destpath");
return;
}
UnscrambleInputStream uis = null;
FileOutputStream fos = null;
try{
FileInputStream fis = new FileInputStream(args[0]);
uis = new UnscrambleInputStream(fis, makeMap());
fos = new FileOutputStream(args[1]);
/*int b;
while((b=uis.read())!=-1){
fos.write(b);
}
*/
// test the byte[] read method
byte[] bytes = new byte[128];
int readLength;
while((readLength= uis.read(bytes, 0, bytes.length))!=-1){
fos.write(bytes, 0, readLength);
}
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(uis!=null){
try{
uis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
if(fos!=null){
try{
fos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
static int[] makeMap(){
int[] map = new int[256];
for(int i=0;i<map.length;i++){
map[i]=i;
}
// Shuffle with the same seed.
Random random = new Random(Scramble.RANDOM_SEED);
for(int i=0; i<map.length;i++){
int randomNum = random.nextInt(map.length);
if(randomNum!=i){
int temp = map[i];
map[i] = map[randomNum];
map[randomNum] = temp;
}
}
int[] indexMap = new int[map.length];
for(int i=0;i<indexMap.length;i++){
indexMap[map[i]] = i;
// store the index for the corresponding element map[i] in the indexMap array.
}
return indexMap;
}
}
class UnscrambleInputStream extends FilterInputStream{
private int[] map;
public UnscrambleInputStream(InputStream in, int[] map){
super(in);
if(map==null || map.length!=256){
throw new IllegalArgumentException("map.length need to be 256 exactly");
}
this.map = map;
}
@Override
public int read() throws IOException{
int readValue = in.read();
if(readValue != -1){
readValue = map[readValue];
}
return readValue;
}
@Override
public int read(byte[] b, int offset, int length) throws IOException{
int readResult = in.read(b, offset, length);
// in is inherited from Ancestor class
if(readResult!=-1){
for(int i=offset; i < readResult+offset; i++){
/*int byteValue = Byte.toUnsignedInt(b[i]);
// need conversion, otherwise you may encounter IndexOutOfBoundsException because of negative int number
b[i] = (byte)map[byteValue];
*/
// implementation as per the book
b[i] = (byte)map[i];
}
}
return readResult;
}
}