001: /*****************************************************************************
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025:
026: * The Original Software is the CVS Client Library.
027: * The Initial Developer of the Original Software is Robert Greig.
028: * Portions created by Robert Greig are Copyright (C) 2000.
029: * All Rights Reserved.
030: *
031: * If you wish your version of this file to be governed by only the CDDL
032: * or only the GPL Version 2, indicate your decision by adding
033: * "[Contributor] elects to include this software in this distribution
034: * under the [CDDL or GPL Version 2] license." If you do not indicate a
035: * single choice of license, a recipient has the option to distribute
036: * your version of this file under either the CDDL, the GPL Version 2 or
037: * to extend the choice of license to its licensees as provided above.
038: * However, if you add GPL Version 2 code and therefore, elected the GPL
039: * Version 2 license, then the option applies only if the new code is
040: * made subject to such option by the copyright holder.
041:
042: * Contributor(s): Robert Greig.
043: *****************************************************************************/package org.netbeans.lib.cvsclient.util;
044:
045: import java.io.*;
046:
047: /**
048: * This input stream worked exactly like the normal DataInputStream except that
049: * it logs anything read to a file
050: * @author Robert Greig
051: */
052: public class LoggedDataInputStream extends FilterInputStream {
053:
054: private long counter;
055:
056: /**
057: * Construct a logged stream using the specified underlying stream
058: * @param in the stream
059: */
060: public LoggedDataInputStream(InputStream in) {
061: super (in);
062: }
063:
064: /**
065: * Read a line (up to the newline character) from the stream, logging
066: * it too.
067: *
068: * @deprecated It converts input data to string using {@link ByteArray#getStringFromBytes}
069: * that works only for ASCII without <tt>0</tt>. Use <tt>byte</tt> access methods instead.
070: */
071: public String readLine() throws IOException {
072: return readLineBytes().getStringFromBytes();
073: }
074:
075: /**
076: *
077: * @return
078: * @throws IOException
079: * @throws EOFException at stream end
080: */
081: public ByteArray readLineBytes() throws IOException {
082: int ch;
083: boolean throwEOF = true;
084: ByteArray byteArray = new ByteArray();
085: loop: while (true) {
086: if (Thread.interrupted()) {
087: Thread.currentThread().interrupt();
088: break;
089: }
090: if (in.available() == 0) {
091: try {
092: Thread.sleep(100);
093: } catch (InterruptedException iex) {
094: Thread.currentThread().interrupt();
095: break loop;
096: }
097: continue;
098: }
099: ch = in.read();
100: counter++;
101: switch (ch) {
102: case -1:
103: if (throwEOF) {
104: throw new EOFException();
105: }
106: case '\n':
107: break loop;
108: default:
109: byteArray.add((byte) ch);
110: }
111: throwEOF = false;
112: }
113: byte[] bytes = byteArray.getBytes();
114: Logger.logInput(bytes);
115: Logger.logInput('\n'); //NOI18N
116: return byteArray;
117: }
118:
119: /**
120: * Synchronously reads fixed chunk from the stream, logging it too.
121: *
122: * @param len blocks until specifid number of bytes is read.
123: */
124: public byte[] readBytes(int len) throws IOException {
125: int ch;
126: ByteArray byteArray = new ByteArray();
127: loop: while (len != 0) {
128: if (Thread.interrupted()) {
129: Thread.currentThread().interrupt();
130: break;
131: }
132: if (in.available() == 0) {
133: try {
134: Thread.sleep(100);
135: } catch (InterruptedException iex) {
136: Thread.currentThread().interrupt();
137: break loop;
138: }
139: continue;
140: }
141: ch = in.read();
142: counter++;
143: switch (ch) {
144: case -1:
145: break loop;
146: default:
147: byteArray.add((byte) ch);
148: len--;
149: }
150: }
151: byte[] bytes = byteArray.getBytes();
152: Logger.logInput(bytes);
153: return bytes;
154: }
155:
156: /**
157: * Closes this input stream and releases any system resources associated
158: * with the stream.
159: */
160: public void close() throws IOException {
161: in.close();
162: }
163:
164: /**
165: * Reads up to byte.length bytes of data from this input stream into an
166: * array of bytes.
167: */
168: public int read(byte[] b) throws IOException {
169: int read = in.read(b);
170: if (read != -1) {
171: Logger.logInput(b, 0, read);
172: counter += read;
173: }
174: return read;
175: }
176:
177: /**
178: * Reads up to len bytes of data from this input stream into an array of
179: * bytes
180: */
181: public int read(byte[] b, int off, int len) throws IOException {
182: int read = in.read(b, off, len);
183: if (read != -1) {
184: Logger.logInput(b, off, read);
185: counter += read;
186: }
187: return read;
188: }
189:
190: public long skip(long n) throws IOException {
191: long skip = super .skip(n);
192: if (skip > 0) {
193: Logger.logInput(new String("<skipped " + skip + " bytes>")
194: .getBytes("utf8")); // NOI18N
195: counter += skip;
196: }
197: return skip;
198: }
199:
200: /**
201: * Interruptible read.
202: * @throws InterruptedIOException on thread interrupt
203: */
204: public int read() throws IOException {
205: while (in.available() == 0) {
206: try {
207: Thread.sleep(100);
208: } catch (InterruptedException iex) {
209: Thread.currentThread().interrupt();
210: throw new InterruptedIOException();
211: }
212: }
213:
214: int i = super .read();
215: if (i != -1) {
216: Logger.logInput((char) i);
217: counter++;
218: }
219: return i;
220: }
221:
222: public InputStream getUnderlyingStream() {
223: return in;
224: }
225:
226: public void setUnderlyingStream(InputStream is) {
227: in = is;
228: }
229:
230: public long getCounter() {
231: return counter;
232: }
233: }
|