View Javadoc

1   /*
2    * Copyright (c) 1998, 2005 Gargoyle Software Inc. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * 1. Redistributions of source code must retain the above copyright notice,
8    *    this list of conditions and the following disclaimer.
9    * 2. Redistributions in binary form must reproduce the above copyright notice,
10   *    this list of conditions and the following disclaimer in the documentation
11   *    and/or other materials provided with the distribution.
12   * 3. The end-user documentation included with the redistribution, if any, must
13   *    include the following acknowledgment:
14   *
15   *       "This product includes software developed by Gargoyle Software Inc.
16   *        (http://www.GargoyleSoftware.com/)."
17   *
18   *    Alternately, this acknowledgment may appear in the software itself, if
19   *    and wherever such third-party acknowledgments normally appear.
20   * 4. The name "Gargoyle Software" must not be used to endorse or promote
21   *    products derived from this software without prior written permission.
22   *    For written permission, please contact info@GargoyleSoftware.com.
23   * 5. Products derived from this software may not be called "GSBase", nor may
24   *    "GSBase" appear in their name, without prior written permission of
25   *    Gargoyle Software Inc.
26   *
27   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30   * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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