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 |
8
| public BinaryDumpOutputStream(Writer out)
|
79 |
| { |
80 |
8
| this.out = new PrintWriter(out);
|
81 |
| } |
82 |
| |
83 |
8
| public void close() throws IOException
|
84 |
| { |
85 |
8
| if (out != null)
|
86 |
| { |
87 |
8
| if (lineCount > 0)
|
88 |
8
| finishFinalLine();
|
89 |
| |
90 |
8
| out.close();
|
91 |
| } |
92 |
| |
93 |
8
| out = null;
|
94 |
| } |
95 |
| |
96 |
8
| private void finishFinalLine()
|
97 |
| { |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
8
| while (lineCount < bytesPerLine)
|
103 |
| { |
104 |
| |
105 |
| |
106 |
40
| if (spacingInterval > 0 && bytesSinceSpace == spacingInterval)
|
107 |
| { |
108 |
4
| out.print(' ');
|
109 |
4
| bytesSinceSpace = 0;
|
110 |
| } |
111 |
| |
112 |
| |
113 |
| |
114 |
40
| out.print(" ");
|
115 |
| |
116 |
40
| if (showAscii)
|
117 |
38
| ascii[lineCount] = ' ';
|
118 |
| |
119 |
40
| lineCount++;
|
120 |
40
| bytesSinceSpace++;
|
121 |
| } |
122 |
| |
123 |
8
| if (showAscii)
|
124 |
| { |
125 |
6
| out.print(asciiBegin);
|
126 |
6
| out.print(ascii);
|
127 |
6
| out.print(asciiEnd);
|
128 |
| } |
129 |
| |
130 |
8
| out.println();
|
131 |
| } |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
16
| public void flush() throws IOException
|
138 |
| { |
139 |
16
| 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 |
2
| public void setAsciiBegin(String value)
|
173 |
| { |
174 |
2
| if (locked)
|
175 |
0
| throw new IllegalStateException();
|
176 |
| |
177 |
2
| asciiBegin = value;
|
178 |
| } |
179 |
| |
180 |
2
| public void setAsciiEnd(String value)
|
181 |
| { |
182 |
2
| if (locked)
|
183 |
0
| throw new IllegalStateException();
|
184 |
| |
185 |
2
| asciiEnd = value;
|
186 |
| } |
187 |
| |
188 |
2
| public void setBytesPerLine(int value)
|
189 |
| { |
190 |
2
| if (locked)
|
191 |
0
| throw new IllegalStateException();
|
192 |
| |
193 |
2
| bytesPerLine = value;
|
194 |
| |
195 |
2
| ascii = null;
|
196 |
| } |
197 |
| |
198 |
2
| public void setOffsetSeperator(String value)
|
199 |
| { |
200 |
2
| if (locked)
|
201 |
0
| throw new IllegalStateException();
|
202 |
| |
203 |
2
| offsetSeperator = value;
|
204 |
| } |
205 |
| |
206 |
2
| public void setShowAscii(boolean value)
|
207 |
| { |
208 |
2
| if (locked)
|
209 |
0
| throw new IllegalStateException();
|
210 |
| |
211 |
2
| showAscii = value;
|
212 |
| } |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
2
| public void setSubstituteChar(char value)
|
220 |
| { |
221 |
2
| if (locked)
|
222 |
0
| throw new IllegalStateException();
|
223 |
| |
224 |
2
| substituteChar = value;
|
225 |
| } |
226 |
| |
227 |
1016
| public void write(int b) throws IOException
|
228 |
| { |
229 |
1016
| char letter;
|
230 |
| |
231 |
1016
| if (showAscii && ascii == null)
|
232 |
6
| ascii = new char[bytesPerLine];
|
233 |
| |
234 |
| |
235 |
| |
236 |
1016
| locked = true;
|
237 |
| |
238 |
1016
| if (lineCount == bytesPerLine)
|
239 |
| { |
240 |
46
| if (showAscii)
|
241 |
| { |
242 |
32
| out.print(asciiBegin);
|
243 |
32
| out.print(ascii);
|
244 |
32
| out.print(asciiEnd);
|
245 |
| } |
246 |
| |
247 |
46
| out.println();
|
248 |
| |
249 |
46
| bytesSinceSpace = 0;
|
250 |
46
| lineCount = 0;
|
251 |
46
| offset += bytesPerLine;
|
252 |
| } |
253 |
| |
254 |
1016
| if (lineCount == 0 && showOffset)
|
255 |
| { |
256 |
38
| writeHex(offset, 4);
|
257 |
38
| out.print(offsetSeperator);
|
258 |
| } |
259 |
| |
260 |
| |
261 |
| |
262 |
1016
| if (spacingInterval > 0 && bytesSinceSpace == spacingInterval)
|
263 |
| { |
264 |
170
| out.print(' ');
|
265 |
170
| bytesSinceSpace = 0;
|
266 |
| } |
267 |
| |
268 |
1016
| writeHex(b, 2);
|
269 |
| |
270 |
1016
| if (showAscii)
|
271 |
| { |
272 |
762
| if (b < 32 | b > 127)
|
273 |
258
| letter = substituteChar;
|
274 |
| else |
275 |
504
| letter = (char) b;
|
276 |
| |
277 |
762
| ascii[lineCount] = letter;
|
278 |
| } |
279 |
| |
280 |
1016
| lineCount++;
|
281 |
1016
| bytesSinceSpace++;
|
282 |
| } |
283 |
| |
284 |
1054
| private void writeHex(int value, int digits)
|
285 |
| { |
286 |
1054
| int i;
|
287 |
1054
| int nybble;
|
288 |
| |
289 |
1054
| for (i = 0; i < digits; i++)
|
290 |
| { |
291 |
2184
| nybble = (value >> 4 * (digits - i - 1)) & 0x0f;
|
292 |
| |
293 |
2184
| out.print(HEX[nybble]);
|
294 |
| } |
295 |
| } |
296 |
| |
297 |
2
| public void setSpacingInterval(int spacingInterval)
|
298 |
| { |
299 |
2
| this.spacingInterval = spacingInterval;
|
300 |
| } |
301 |
| |
302 |
0
| public boolean isShowOffset()
|
303 |
| { |
304 |
0
| return showOffset;
|
305 |
| } |
306 |
| |
307 |
2
| public void setShowOffset(boolean showOffset)
|
308 |
| { |
309 |
2
| this.showOffset = showOffset;
|
310 |
| } |
311 |
| |
312 |
0
| public int getSpacingInterval()
|
313 |
| { |
314 |
0
| return spacingInterval;
|
315 |
| } |
316 |
| } |