1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.util.io; |
16 |
|
|
17 |
|
import java.io.IOException; |
18 |
|
import java.io.OutputStream; |
19 |
|
import java.io.PrintWriter; |
20 |
|
import java.io.Writer; |
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
public class BinaryDumpOutputStream extends OutputStream |
32 |
|
{ |
33 |
1 |
private static final char[] HEX = |
34 |
|
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
35 |
|
|
36 |
|
private PrintWriter _out; |
37 |
|
|
38 |
4 |
private boolean locked = false; |
39 |
|
|
40 |
4 |
private boolean _showOffset = true; |
41 |
|
|
42 |
4 |
private int bytesPerLine = 16; |
43 |
|
|
44 |
4 |
private int _spacingInterval = 4; |
45 |
|
|
46 |
4 |
private char substituteChar = '.'; |
47 |
|
|
48 |
4 |
private String offsetSeperator = ": "; |
49 |
|
|
50 |
4 |
private int offset = 0; |
51 |
|
|
52 |
4 |
private int lineCount = 0; |
53 |
|
|
54 |
4 |
private int bytesSinceSpace = 0; |
55 |
|
|
56 |
4 |
private char[] ascii = null; |
57 |
|
|
58 |
4 |
private boolean showAscii = true; |
59 |
|
|
60 |
4 |
private String asciiBegin = " |"; |
61 |
|
|
62 |
4 |
private String asciiEnd = "|"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
public BinaryDumpOutputStream() |
69 |
|
{ |
70 |
0 |
this(new PrintWriter(System.out, true)); |
71 |
0 |
} |
72 |
|
|
73 |
|
public BinaryDumpOutputStream(PrintWriter out) |
74 |
0 |
{ |
75 |
0 |
this._out = out; |
76 |
0 |
} |
77 |
|
|
78 |
|
public BinaryDumpOutputStream(Writer out) |
79 |
4 |
{ |
80 |
4 |
this._out = new PrintWriter(out); |
81 |
4 |
} |
82 |
|
|
83 |
|
public void close() throws IOException |
84 |
|
{ |
85 |
4 |
if (_out != null) |
86 |
|
{ |
87 |
4 |
if (lineCount > 0) |
88 |
4 |
finishFinalLine(); |
89 |
|
|
90 |
4 |
_out.close(); |
91 |
|
} |
92 |
|
|
93 |
4 |
_out = null; |
94 |
4 |
} |
95 |
|
|
96 |
|
private void finishFinalLine() |
97 |
|
{ |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
40 |
while (lineCount < bytesPerLine) |
103 |
|
{ |
104 |
|
|
105 |
|
|
106 |
36 |
if (_spacingInterval > 0 && bytesSinceSpace == _spacingInterval) |
107 |
|
{ |
108 |
7 |
_out.print(' '); |
109 |
7 |
bytesSinceSpace = 0; |
110 |
|
} |
111 |
|
|
112 |
|
|
113 |
|
|
114 |
36 |
_out.print(" "); |
115 |
|
|
116 |
36 |
if (showAscii) |
117 |
27 |
ascii[lineCount] = ' '; |
118 |
|
|
119 |
36 |
lineCount++; |
120 |
36 |
bytesSinceSpace++; |
121 |
|
} |
122 |
|
|
123 |
4 |
if (showAscii) |
124 |
|
{ |
125 |
3 |
_out.print(asciiBegin); |
126 |
3 |
_out.print(ascii); |
127 |
3 |
_out.print(asciiEnd); |
128 |
|
} |
129 |
|
|
130 |
4 |
_out.println(); |
131 |
4 |
} |
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
public void flush() throws IOException |
138 |
|
{ |
139 |
8 |
_out.flush(); |
140 |
8 |
} |
141 |
|
|
142 |
|
public String getAsciiBegin() |
143 |
|
{ |
144 |
0 |
return asciiBegin; |
145 |
|
} |
146 |
|
|
147 |
|
public String getAsciiEnd() |
148 |
|
{ |
149 |
0 |
return asciiEnd; |
150 |
|
} |
151 |
|
|
152 |
|
public int getBytesPerLine() |
153 |
|
{ |
154 |
0 |
return bytesPerLine; |
155 |
|
} |
156 |
|
|
157 |
|
public String getOffsetSeperator() |
158 |
|
{ |
159 |
0 |
return offsetSeperator; |
160 |
|
} |
161 |
|
|
162 |
|
public boolean getShowAscii() |
163 |
|
{ |
164 |
0 |
return showAscii; |
165 |
|
} |
166 |
|
|
167 |
|
public char getSubstituteChar() |
168 |
|
{ |
169 |
0 |
return substituteChar; |
170 |
|
} |
171 |
|
|
172 |
|
public void setAsciiBegin(String value) |
173 |
|
{ |
174 |
1 |
if (locked) |
175 |
0 |
throw new IllegalStateException(); |
176 |
|
|
177 |
1 |
asciiBegin = value; |
178 |
1 |
} |
179 |
|
|
180 |
|
public void setAsciiEnd(String value) |
181 |
|
{ |
182 |
1 |
if (locked) |
183 |
0 |
throw new IllegalStateException(); |
184 |
|
|
185 |
1 |
asciiEnd = value; |
186 |
1 |
} |
187 |
|
|
188 |
|
public void setBytesPerLine(int value) |
189 |
|
{ |
190 |
1 |
if (locked) |
191 |
0 |
throw new IllegalStateException(); |
192 |
|
|
193 |
1 |
bytesPerLine = value; |
194 |
|
|
195 |
1 |
ascii = null; |
196 |
1 |
} |
197 |
|
|
198 |
|
public void setOffsetSeperator(String value) |
199 |
|
{ |
200 |
1 |
if (locked) |
201 |
0 |
throw new IllegalStateException(); |
202 |
|
|
203 |
1 |
offsetSeperator = value; |
204 |
1 |
} |
205 |
|
|
206 |
|
public void setShowAscii(boolean value) |
207 |
|
{ |
208 |
1 |
if (locked) |
209 |
0 |
throw new IllegalStateException(); |
210 |
|
|
211 |
1 |
showAscii = value; |
212 |
1 |
} |
213 |
|
|
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
public void setSubstituteChar(char value) |
220 |
|
{ |
221 |
1 |
if (locked) |
222 |
0 |
throw new IllegalStateException(); |
223 |
|
|
224 |
1 |
substituteChar = value; |
225 |
1 |
} |
226 |
|
|
227 |
|
public void write(int b) throws IOException |
228 |
|
{ |
229 |
|
char letter; |
230 |
|
|
231 |
348 |
if (showAscii && ascii == null) |
232 |
3 |
ascii = new char[bytesPerLine]; |
233 |
|
|
234 |
|
|
235 |
|
|
236 |
348 |
locked = true; |
237 |
|
|
238 |
348 |
if (lineCount == bytesPerLine) |
239 |
|
{ |
240 |
16 |
if (showAscii) |
241 |
|
{ |
242 |
11 |
_out.print(asciiBegin); |
243 |
11 |
_out.print(ascii); |
244 |
11 |
_out.print(asciiEnd); |
245 |
|
} |
246 |
|
|
247 |
16 |
_out.println(); |
248 |
|
|
249 |
16 |
bytesSinceSpace = 0; |
250 |
16 |
lineCount = 0; |
251 |
16 |
offset += bytesPerLine; |
252 |
|
} |
253 |
|
|
254 |
348 |
if (lineCount == 0 && _showOffset) |
255 |
|
{ |
256 |
14 |
writeHex(offset, 4); |
257 |
14 |
_out.print(offsetSeperator); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
348 |
if (_spacingInterval > 0 && bytesSinceSpace == _spacingInterval) |
263 |
|
{ |
264 |
57 |
_out.print(' '); |
265 |
57 |
bytesSinceSpace = 0; |
266 |
|
} |
267 |
|
|
268 |
348 |
writeHex(b, 2); |
269 |
|
|
270 |
348 |
if (showAscii) |
271 |
|
{ |
272 |
261 |
if (b < 32 | b > 127) |
273 |
96 |
letter = substituteChar; |
274 |
|
else |
275 |
165 |
letter = (char) b; |
276 |
|
|
277 |
261 |
ascii[lineCount] = letter; |
278 |
|
} |
279 |
|
|
280 |
348 |
lineCount++; |
281 |
348 |
bytesSinceSpace++; |
282 |
348 |
} |
283 |
|
|
284 |
|
private void writeHex(int value, int digits) |
285 |
|
{ |
286 |
|
int i; |
287 |
|
int nybble; |
288 |
|
|
289 |
1114 |
for (i = 0; i < digits; i++) |
290 |
|
{ |
291 |
752 |
nybble = (value >> 4 * (digits - i - 1)) & 0x0f; |
292 |
|
|
293 |
752 |
_out.print(HEX[nybble]); |
294 |
|
} |
295 |
362 |
} |
296 |
|
|
297 |
|
public void setSpacingInterval(int spacingInterval) |
298 |
|
{ |
299 |
1 |
this._spacingInterval = spacingInterval; |
300 |
1 |
} |
301 |
|
|
302 |
|
public boolean isShowOffset() |
303 |
|
{ |
304 |
0 |
return _showOffset; |
305 |
|
} |
306 |
|
|
307 |
|
public void setShowOffset(boolean showOffset) |
308 |
|
{ |
309 |
1 |
this._showOffset = showOffset; |
310 |
1 |
} |
311 |
|
|
312 |
|
public int getSpacingInterval() |
313 |
|
{ |
314 |
0 |
return _spacingInterval; |
315 |
|
} |
316 |
|
} |