1 21 22 24 import java.io.*; 25 26 40 public class StraightStreamReader extends Reader{ 41 42 45 private InputStream in; 46 47 54 private byte[] buffer; 55 56 61 public StraightStreamReader(InputStream in) { 62 this.in = in; 63 } 64 65 70 public void close() throws IOException { 71 in.close(); 72 } 73 74 84 public void mark(int readAheadLimit) throws IOException { 85 in.mark(readAheadLimit); 86 } 87 88 93 public boolean markSupported(){ 94 return in.markSupported(); 95 } 96 97 105 public int read() throws IOException { 106 return in.read(); 107 } 108 109 117 public int read(char[] cbuf) throws IOException { 118 return read(cbuf, 0, cbuf.length); 119 } 120 121 131 public int read(char[] cbuf, int off, int len) throws IOException { 132 if (buffer == null || buffer.length < len){ 135 buffer = new byte[len]; 136 } 137 int length = in.read(buffer, 0, len); 139 for (int i=0; i<length; i++){ 140 cbuf[off+i] = (char)(0xFF & buffer[i]); 141 } 142 return length; 143 } 144 145 152 public boolean ready() throws IOException { 153 return (in.available() > 0); 154 } 155 156 166 public void reset() throws IOException { 167 in.reset(); 168 } 169 170 179 public long skip(long n) throws IOException { 180 return in.skip(n); 181 } 182 183 193 private static void main(String [] args){ 194 try { 195 File f = new File("test.txt"); 196 if (f.exists()){ 197 throw new IOException(f + " already exists. I don't want to overwrite it."); 198 } 199 StraightStreamReader in; 200 char[] cbuf = new char[0x1000]; 201 int read; 202 int totRead; 203 204 FileOutputStream out = new FileOutputStream(f); 206 for (int i=0x00; i<0x100; i++){ 207 out.write(i); 208 } 209 out.close(); 210 211 in = new StraightStreamReader(new FileInputStream(f)); 213 for (int i=0x00; i<0x100; i++){ 214 read = in.read(); 215 if (read != i){ 216 System.err.println("Error: " + i + " read as " + read); 217 } 218 } 219 in.close(); 220 221 in = new StraightStreamReader(new FileInputStream(f)); 223 totRead = in.read(cbuf); 224 if (totRead != 0x100){ 225 System.err.println("Simple buffered read did not read the full amount: 0x" + Integer.toHexString(totRead)); 226 } 227 for (int i=0x00; i<totRead; i++){ 228 if (cbuf[i] != i){ 229 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]); 230 } 231 } 232 in.close(); 233 234 in = new StraightStreamReader(new FileInputStream(f)); 236 totRead = 0; 237 while (totRead <= 0x100 && (read = in.read(cbuf, totRead, 0x100 - totRead)) > 0){ 238 totRead += read; 239 } 240 if (totRead != 0x100){ 241 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead)); 242 } 243 for (int i=0x00; i<totRead; i++){ 244 if (cbuf[i] != i){ 245 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i]); 246 } 247 } 248 in.close(); 249 250 in = new StraightStreamReader(new FileInputStream(f)); 252 totRead = 0; 253 while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 0x100 - totRead)) > 0){ 254 totRead += read; 255 } 256 if (totRead != 0x100){ 257 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead)); 258 } 259 for (int i=0x00; i<totRead; i++){ 260 if (cbuf[i+0x123] != i){ 261 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]); 262 } 263 } 264 in.close(); 265 266 in = new StraightStreamReader(new FileInputStream(f)); 268 totRead = 0; 269 while (totRead <= 0x100 && (read = in.read(cbuf, totRead+0x123, 7)) > 0){ 270 totRead += read; 271 } 272 if (totRead != 0x100){ 273 System.err.println("Not enough read. Bytes read: " + Integer.toHexString(totRead)); 274 } 275 for (int i=0x00; i<totRead; i++){ 276 if (cbuf[i+0x123] != i){ 277 System.err.println("Error: 0x" + i + " read as 0x" + cbuf[i+0x123]); 278 } 279 } 280 in.close(); 281 282 f.delete(); 283 } catch (IOException x){ 284 System.err.println(x.getMessage()); 285 } 286 } 287 } 288 | Popular Tags |