StringGenerator.java
1 |
import java.io.*; |
---|---|
2 |
import java.util.*; |
3 |
import java.security.*; |
4 |
|
5 |
public class StringGenerator |
6 |
{ |
7 |
private static SecureRandom random = new SecureRandom(); |
8 |
|
9 |
public static void writeRecord(OutputStream out, int[] data) |
10 |
throws IOException |
11 |
{ |
12 |
out.write(data.length); |
13 |
|
14 |
for (int i = 0; i < data.length; i++) |
15 |
{ |
16 |
out.write(data[i]); |
17 |
} |
18 |
} |
19 |
|
20 |
public static int[] genRandomUnsignedBytes(int len) |
21 |
throws IOException |
22 |
{ |
23 |
int[] result = new int[len]; |
24 |
|
25 |
for (int i = 0; i < len; i++) |
26 |
{ |
27 |
result[i] = random.nextInt(256);
|
28 |
|
29 |
if (result[i] < 0 || result[i] > 255) |
30 |
throw new IllegalStateException("found invalid int " + result[i]); |
31 |
} |
32 |
|
33 |
return result;
|
34 |
} |
35 |
|
36 |
private static class IntArrayHolder |
37 |
implements Comparable |
38 |
{ |
39 |
int[] data = null; |
40 |
|
41 |
public IntArrayHolder(int[] data) |
42 |
{ |
43 |
this.data = data;
|
44 |
|
45 |
} |
46 |
|
47 |
public int compareTo(Object other) |
48 |
{ |
49 |
if (other == null) |
50 |
throw new NullPointerException(); |
51 |
|
52 |
IntArrayHolder oth = (IntArrayHolder) other; |
53 |
|
54 |
int result = 0; |
55 |
|
56 |
if (data.length == oth.data.length)
|
57 |
{ |
58 |
// compare contents, lowest element to highest element
|
59 |
for (int i = 0; i < data.length; i++) |
60 |
{ |
61 |
if (data[i] == oth.data.length)
|
62 |
{ |
63 |
continue;
|
64 |
} |
65 |
else
|
66 |
{ |
67 |
// first differing element determines the result
|
68 |
result = (data[i] > oth.data[i]) ? 1 : -1; |
69 |
} |
70 |
} |
71 |
|
72 |
// result stays as 0, nothing to do since they are equal
|
73 |
} |
74 |
else
|
75 |
{ |
76 |
result = (data.length > oth.data.length) ? 1 : -1; |
77 |
} |
78 |
|
79 |
return result;
|
80 |
} |
81 |
|
82 |
public boolean equals(Object other) |
83 |
{ |
84 |
if (!(other instanceof IntArrayHolder)) |
85 |
return false; |
86 |
|
87 |
return this.compareTo((IntArrayHolder) other) == 0; |
88 |
} |
89 |
|
90 |
public int hashCode() |
91 |
{ |
92 |
return Arrays.hashCode(data); |
93 |
} |
94 |
} |
95 |
|
96 |
public static void main(String[] args) |
97 |
{ |
98 |
OutputStream out = null; |
99 |
int[] data = null; |
100 |
|
101 |
try
|
102 |
{ |
103 |
out = new FileOutputStream(args[1]); |
104 |
|
105 |
if ("-r".equalsIgnoreCase(args[0])) |
106 |
{ |
107 |
for (int j = 3; j < 33; j++) |
108 |
{ |
109 |
TreeSet<IntArrayHolder> set = new TreeSet<IntArrayHolder>(); |
110 |
|
111 |
while (set.size() < 10000) |
112 |
{ |
113 |
set.add(new IntArrayHolder(genRandomUnsignedBytes(j)));
|
114 |
} |
115 |
|
116 |
Iterator<IntArrayHolder> iter = set.iterator();
|
117 |
|
118 |
while (iter.hasNext())
|
119 |
{ |
120 |
writeRecord(out, iter.next().data); |
121 |
} |
122 |
} |
123 |
} |
124 |
else
|
125 |
{ |
126 |
// empty string
|
127 |
data = new int[0]; |
128 |
writeRecord(out, data); |
129 |
|
130 |
// all possible 1 character strings
|
131 |
data = new int[1]; |
132 |
for (int n = 0; n < 256; n++) |
133 |
{ |
134 |
data[0] = n;
|
135 |
writeRecord(out, data); |
136 |
} |
137 |
|
138 |
// all possible 2 character strings
|
139 |
data = new int[2]; |
140 |
for (int i = 0; i < 256; i++) |
141 |
{ |
142 |
data[0] = i;
|
143 |
for (int j = 0; j < 256; j++) |
144 |
{ |
145 |
data[1] = j;
|
146 |
writeRecord(out, data); |
147 |
} |
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
catch (IOException ioe) |
153 |
{ |
154 |
ioe.printStackTrace(); |
155 |
} |
156 |
|
157 |
finally
|
158 |
{ |
159 |
try
|
160 |
{ |
161 |
if (out != null) |
162 |
out.close(); |
163 |
} |
164 |
|
165 |
catch (IOException ioe) |
166 |
{ |
167 |
// ignore
|
168 |
} |
169 |
} |
170 |
} |
171 |
} |