KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > util > LoggedDataOutputStream


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.util;
23
24 import java.io.*;
25
26 /**
27  * A data output stream that also logs everything sent to a Writer (via the
28  * logger).
29  * @author Robert Greig
30  */

31 public class LoggedDataOutputStream extends FilterOutputStream {
32
33     private long counter;
34
35     /**
36      * Construct a logged stream using the specified underlying stream
37      * @param out the stream
38      */

39     public LoggedDataOutputStream(OutputStream out) {
40         super(out);
41     }
42
43     /**
44      * Write a line to the stream, logging it too. For compatibility reasons
45      * only. Does exactly the same what writeBytes() does.
46      *
47      * @deprecated Line to to bytes conversion is host specifics.
48      * Use raw byte access methods insted.
49      *
50      */

51     public void writeChars(String JavaDoc line) throws IOException {
52         writeBytes(line);
53     }
54     
55     /**
56      * Write a line to the stream, logging it too.
57      *
58      * Line to to bytes conversion is host specifics. Use {@link #writeBytes(String, String)} if possible.
59      */

60     public void writeBytes(String JavaDoc line) throws IOException {
61         byte[] bytes = line.getBytes();
62         out.write(bytes);
63         Logger.logOutput(bytes);
64         counter += bytes.length;
65     }
66
67     /**
68      * Write a line to the stream, logging it too.
69      */

70     public void writeBytes(String JavaDoc line, String JavaDoc encoding) throws IOException {
71         byte[] bytes = line.getBytes(encoding);
72         out.write(bytes);
73         Logger.logOutput(bytes);
74         counter += bytes.length;
75     }
76
77     public void write(int b) throws IOException {
78         super.write(b);
79         counter++;
80     }
81
82     public void write(byte b[]) throws IOException {
83         super.write(b);
84         counter += b.length;
85     }
86
87     public void write(byte b[], int off, int len) throws IOException {
88         super.write(b, off, len);
89         counter += len;
90     }
91
92     /**
93      * Closes this input stream and releases any system resources associated
94      * with the stream.
95      */

96     public void close() throws IOException {
97         out.close();
98     }
99
100     public OutputStream getUnderlyingStream() {
101         return out;
102     }
103
104     public void setUnderlyingStream(OutputStream os) {
105         out = os;
106     }
107
108     public long getCounter() {
109         return counter;
110     }
111 }
Popular Tags