KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > io > DebugOutputStream


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.io;
4
5 import java.io.FilterOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.OutputStream JavaDoc;
8
9 /**
10  * Output stream used for debugging purposes.
11  */

12 public class DebugOutputStream extends FilterOutputStream JavaDoc {
13
14     // ---------------------------------------------------------------- ctors
15

16     protected boolean passThrough = true;
17
18     /**
19      * Output stream that debugs to system out.
20      */

21     public DebugOutputStream() {
22         super(System.out);
23     }
24
25     public DebugOutputStream(OutputStream JavaDoc out) {
26         super(out);
27     }
28
29     public DebugOutputStream(boolean passThrough) {
30         super(System.out);
31         this.passThrough = passThrough;
32     }
33
34     public DebugOutputStream(OutputStream JavaDoc out, boolean passThrough) {
35         super(out);
36         this.passThrough = passThrough;
37     }
38
39
40     // ---------------------------------------------------------------- methods
41

42     public void close() throws IOException JavaDoc {
43         super.close();
44     }
45
46     public void flush() throws IOException JavaDoc {
47         super.flush();
48     }
49
50     public void write(int b) throws IOException JavaDoc {
51         if (passThrough == true) {
52             super.write(b);
53         }
54         dumpByte(b);
55         System.out.println();
56     }
57
58     public void write(byte b[]) throws IOException JavaDoc {
59         super.write(b);
60     }
61
62     public void write(byte b[], int off, int len) throws IOException JavaDoc {
63         if (passThrough == true) {
64             super.write(b, off, len);
65         }
66         int i = off;
67         int count = len;
68         while (count-- > 0) {
69             dumpByte(b[i++]);
70         }
71         System.out.println();
72     }
73
74
75     /**
76      * Dumps single byte to output stream.
77      */

78     protected void dumpByte(int b) {
79         if (passThrough == true) {
80             System.out.print('\t');
81         }
82         if (b < 0) {
83             b += 128;
84         }
85         if (b < 0x10) {
86             System.out.print('0');
87         }
88
89         System.out.print(' ');
90         System.out.print(Integer.toHexString(b).toUpperCase());
91     }
92 }
93
Popular Tags