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