KickJava   Java API By Example, From Geeks To Geeks.

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


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  * This input stream worked exactly like the normal DataInputStream except that
28  * it logs anything read to a file
29  * @author Robert Greig
30  */

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

39     public LoggedDataInputStream(InputStream in) {
40         super(in);
41     }
42
43     /**
44      * Read a line (up to the newline character) from the stream, logging
45      * it too.
46      *
47      * @deprecated It converts input data to string using {@link ByteArray#getStringFromBytes}
48      * that works only for ASCII without <tt>0</tt>. Use <tt>byte</tt> access methods instead.
49      */

50     public String JavaDoc readLine() throws IOException {
51         return readLineBytes().getStringFromBytes();
52     }
53
54     /**
55      *
56      * @return
57      * @throws IOException
58      * @throws EOFException at stream end
59      */

60     public ByteArray readLineBytes() throws IOException {
61         int ch;
62         boolean throwEOF = true;
63         ByteArray byteArray = new ByteArray();
64         loop: while (true) {
65             if (Thread.interrupted()) {
66                 Thread.currentThread().interrupt();
67                 break;
68             }
69             if (in.available() == 0) {
70                 try {
71                     Thread.sleep(100);
72                 } catch (InterruptedException JavaDoc iex) {
73                     Thread.currentThread().interrupt();
74                     break loop;
75                 }
76                 continue;
77             }
78             ch = in.read();
79             counter++;
80             switch (ch) {
81                 case -1:
82                     if (throwEOF) {
83                         throw new EOFException();
84                     }
85                 case '\n':
86                     break loop;
87                 default:
88                     byteArray.add((byte) ch);
89             }
90             throwEOF = false;
91         }
92         byte[] bytes = byteArray.getBytes();
93         Logger.logInput(bytes);
94         Logger.logInput('\n'); //NOI18N
95
return byteArray;
96     }
97
98     /**
99      * Synchronously reads fixed chunk from the stream, logging it too.
100      *
101      * @param len blocks until specifid number of bytes is read.
102      */

103     public byte[] readBytes(int len) throws IOException {
104         int ch;
105         ByteArray byteArray = new ByteArray();
106         loop: while (len != 0) {
107             if (Thread.interrupted()) {
108                 Thread.currentThread().interrupt();
109                 break;
110             }
111             if (in.available() == 0) {
112                 try {
113                     Thread.sleep(100);
114                 } catch (InterruptedException JavaDoc iex) {
115                     Thread.currentThread().interrupt();
116                     break loop;
117                 }
118                 continue;
119             }
120             ch = in.read();
121             counter++;
122             switch (ch) {
123                 case -1:
124                     break loop;
125                 default:
126                     byteArray.add((byte) ch);
127                     len--;
128             }
129         }
130         byte[] bytes = byteArray.getBytes();
131         Logger.logInput(bytes);
132         return bytes;
133     }
134
135     /**
136      * Closes this input stream and releases any system resources associated
137      * with the stream.
138      */

139     public void close() throws IOException {
140         in.close();
141     }
142
143     /**
144      * Reads up to byte.length bytes of data from this input stream into an
145      * array of bytes.
146      */

147     public int read(byte[] b) throws IOException {
148         int read = in.read(b);
149         if (read != -1) {
150             Logger.logInput(b, 0, read);
151             counter += read;
152         }
153         return read;
154     }
155
156     /**
157      * Reads up to len bytes of data from this input stream into an array of
158      * bytes
159      */

160     public int read(byte[] b, int off, int len) throws IOException {
161         int read = in.read(b, off, len);
162         if (read != -1) {
163             Logger.logInput(b, off, read);
164             counter += read;
165         }
166         return read;
167     }
168
169     public long skip(long n) throws IOException {
170         long skip = super.skip(n);
171         if (skip > 0) {
172             Logger.logInput(new String JavaDoc("<skipped " + skip + " bytes>").getBytes("utf8")); // NOI18N
173
counter += skip;
174         }
175         return skip;
176     }
177
178     /**
179      * Interruptible read.
180      * @throws InterruptedIOException on thread interrupt
181      */

182     public int read() throws IOException {
183         while (in.available() == 0) {
184             try {
185                 Thread.sleep(100);
186             } catch (InterruptedException JavaDoc iex) {
187                 Thread.currentThread().interrupt();
188                 throw new InterruptedIOException();
189             }
190         }
191
192         int i = super.read();
193         if (i != -1) {
194             Logger.logInput((char) i);
195             counter++;
196         }
197         return i;
198     }
199
200     public InputStream getUnderlyingStream() {
201         return in;
202     }
203
204     public void setUnderlyingStream(InputStream is) {
205         in = is;
206     }
207
208     public long getCounter() {
209         return counter;
210     }
211 }
Popular Tags