1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.trace;
39
40 import java.io.OutputStream;
41
42 /***
43 * <p style="color: orange">Internal use only.</p>.
44 *
45 * @version $Revision: 1.9 $
46 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
47 */
48 public class TraceOutputStream
49 extends
50 OutputStream {
51
52 private StringBuffer buffer_;
53
54 /***
55 *
56 */
57 public TraceOutputStream() {
58 buffer_ = new StringBuffer();
59 }
60
61 /***
62 * Writes the specified byte to this output stream. The general
63 * contract for <code>write</code> is that one byte is written
64 * to the output stream. The byte to be written is the eight
65 * low-order bits of the argument <code>b</code>. The 24
66 * high-order bits of <code>b</code> are ignored.
67 * <p>
68 * Subclasses of <code>OutputStream</code> must provide an
69 * implementation for this method.
70 *
71 * @param aByte the <code>byte</code>.
72 */
73 public void write(final int aByte) {
74 final char c = (char)aByte;
75 switch(c) {
76 case '\n':
77 printBuffer();
78 break;
79
80 case '\r':
81 break;
82
83 default:
84 buffer_.append(c);
85 break;
86 }
87 }
88
89 /***
90 * Writes <code>b.length</code> bytes from the specified byte array
91 * to this output stream. The general contract for <code>write(b)</code>
92 * is that it should have exactly the same effect as the call
93 * <code>write(b, 0, b.length)</code>.
94 *
95 * @param b the data.
96 * @see java.io.OutputStream#write(byte[], int, int)
97 */
98 public void write(byte b[]) {
99 write(b, 0, b.length);
100 }
101
102 /***
103 * Writes <code>len</code> bytes from the specified byte array
104 * starting at offset <code>off</code> to this output stream.
105 * The general contract for <code>write(b, off, len)</code> is that
106 * some of the bytes in the array <code>b</code> are written to the
107 * output stream in order; element <code>b[off]</code> is the first
108 * byte written and <code>b[off+len-1]</code> is the last byte written
109 * by this operation.
110 * <p>
111 * The <code>write</code> method of <code>OutputStream</code> calls
112 * the write method of one argument on each of the bytes to be
113 * written out. Subclasses are encouraged to override this method and
114 * provide a more efficient implementation.
115 * <p>
116 * If <code>b</code> is <code>null</code>, a
117 * <code>NullPointerException</code> is thrown.
118 * <p>
119 * If <code>off</code> is negative, or <code>len</code> is negative, or
120 * <code>off+len</code> is greater than the length of the array
121 * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
122 *
123 * @param b the data.
124 * @param off the start offset in the data.
125 * @param len the number of bytes to write.
126 */
127 public void write(byte b[], int off, int len) {
128 if( b == null ) {
129 throw new NullPointerException();
130 }
131 else if( (off < 0) || (off > b.length) || (len < 0)
132 || ((off + len) > b.length) || ((off + len) < 0) ) {
133 throw new IndexOutOfBoundsException();
134 }
135 else if( len == 0 ) {
136 return;
137 }
138 for( int i = 0 ; i < len ; i++ ) {
139 write(b[off + i]);
140 }
141 }
142
143 /***
144 * Flushes this output stream and forces any buffered output bytes
145 * to be written out. The general contract of <code>flush</code> is
146 * that calling it is an indication that, if any bytes previously
147 * written have been buffered by the implementation of the output
148 * stream, such bytes should immediately be written to their
149 * intended destination.
150 * <p>
151 * The <code>flush</code> method of <code>OutputStream</code> does nothing.
152 */
153 public void flush() {
154 printBuffer();
155 Trace.flush();
156 }
157
158 /***
159 * Closes this output stream and releases any system resources
160 * associated with this stream. The general contract of <code>close</code>
161 * is that it closes the output stream. A closed stream cannot perform
162 * output operations and cannot be reopened.
163 * <p>
164 * The <code>close</code> method of <code>OutputStream</code> does nothing.
165 */
166 public void close() {
167 Trace.flush();
168 }
169
170 /***
171 *
172 */
173 private void printBuffer() {
174 StringBuffer tempBuffer = buffer_;
175 buffer_ = new StringBuffer();
176 Trace.println( tempBuffer.toString() );
177 }
178
179 }
180
181