1 22 package org.netbeans.lib.cvsclient.util; 23 24 import java.io.*; 25 26 31 public class LoggedDataInputStream extends FilterInputStream { 32 33 private long counter; 34 35 39 public LoggedDataInputStream(InputStream in) { 40 super(in); 41 } 42 43 50 public String readLine() throws IOException { 51 return readLineBytes().getStringFromBytes(); 52 } 53 54 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 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'); return byteArray; 96 } 97 98 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 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 139 public void close() throws IOException { 140 in.close(); 141 } 142 143 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 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 ("<skipped " + skip + " bytes>").getBytes("utf8")); counter += skip; 174 } 175 return skip; 176 } 177 178 182 public int read() throws IOException { 183 while (in.available() == 0) { 184 try { 185 Thread.sleep(100); 186 } catch (InterruptedException 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 |