1 18 19 package com.Ostermiller.util; 20 21 import java.io.*; 22 23 42 public class StraightStreamReader extends Reader{ 43 44 49 private InputStream in; 50 51 60 private byte[] buffer; 61 62 69 public StraightStreamReader(InputStream in) { 70 this.in = in; 71 } 72 73 80 public void close() throws IOException { 81 in.close(); 82 } 83 84 96 public void mark(int readAheadLimit) throws IOException { 97 in.mark(readAheadLimit); 98 } 99 100 107 public boolean markSupported(){ 108 return in.markSupported(); 109 } 110 111 121 public int read() throws IOException { 122 return in.read(); 123 } 124 125 135 public int read(char[] cbuf) throws IOException { 136 return read(cbuf, 0, cbuf.length); 137 } 138 139 151 public int read(char[] cbuf, int off, int len) throws IOException { 152 if (buffer == null || buffer.length < len){ 155 buffer = new byte[len]; 156 } 157 int length = in.read(buffer, 0, len); 159 for (int i=0; i<length; i++){ 160 cbuf[off+i] = (char)(0xFF & buffer[i]); 161 } 162 return length; 163 } 164 165 174 public boolean ready() throws IOException { 175 return (in.available() > 0); 176 } 177 178 190 public void reset() throws IOException { 191 in.reset(); 192 } 193 194 205 public long skip(long n) throws IOException { 206 return in.skip(n); 207 } 208 209 221 private static void main(String [] args){ 222 try { 223 File f = new File("test.txt"); 224 if (f.exists()){ 225 throw new IOException(f + " already exists. I don't want to overwrite it."); 226 } 227 StraightStreamReader in; 228 char[] cbuf = new char[0x1000]; 229 int read; 230 int totRead; 231 232 FileOutputStream out = new FileOutputStream(f); 234 for (int i=0x00; i<0x100; i++){ 235 out.write(i); 236 } 237 out.close(); 238 239 in = new StraightStreamReader(new FileInputStream(f)); 241 for (int i=0x00; i<0x100; i++){ 242 read = in.read(); 243 if (read != i){ 244 System.err.println("Error: " + i + " read as " + read); 245 } 246 } 247 in.close(); 248 249 in = new StraightStreamReader(new FileInputStream(f)); 251 totRead = in.read(cbuf); 252 if (totRead != 0x100){ 253 System.err.println("Simple buffered read did not read the full amount: 0x" + Integer.toHexString(totRead)); 254 } 255 for (int i=0x00; i<totRead; i++){ 256 if (cbuf[i] != i){ 257 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]); 258 } 259 } 260 in.close(); 261 262 in = new StraightStreamReader(new FileInputStream(f)); 264 totRead = 0; 265 while (totRead <= 0x100 && (read = in.read(cbuf, totRead, 0x100 - totRead)) > 0){ 266 totRead += read; 267 } 268 if (totRead != 0x100){ 269 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead)); 270 } 271 for (int i=0x00; i<totRead; i++){ 272 if (cbuf[i] != i){ 273 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]); 274 } 275 } 276 in.close(); 277 278 in = new StraightStreamReader(new FileInputStream(f)); 280 totRead = 0; 281 while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 0x100 - totRead)) > 0){ 282 totRead += read; 283 } 284 if (totRead != 0x100){ 285 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead)); 286 } 287 for (int i=0x00; i<totRead; i++){ 288 if (cbuf[i+0x123] != i){ 289 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]); 290 } 291 } 292 in.close(); 293 294 in = new StraightStreamReader(new FileInputStream(f)); 296 totRead = 0; 297 while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 7)) > 0){ 298 totRead += read; 299 } 300 if (totRead != 0x100){ 301 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead)); 302 } 303 for (int i=0x00; i<totRead; i++){ 304 if (cbuf[i+0x123] != i){ 305 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]); 306 } 307 } 308 in.close(); 309 310 f.delete(); 311 } catch (IOException x){ 312 System.err.println(x.getMessage()); 313 } 314 } 315 } 316 | Popular Tags |