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