1 6 21 22 package de.schlichtherle.io.rof; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 28 29 40 public class ReadOnlyFileInputStream extends InputStream { 41 42 47 protected ReadOnlyFile rof; 48 49 53 private long mark = -1; 54 55 62 public ReadOnlyFileInputStream(ReadOnlyFile rof) { 63 this.rof = rof; 64 } 65 66 public int read() throws IOException { 67 return rof.read(); 68 } 69 70 public int read(byte[] b) throws IOException { 71 return rof.read(b); 72 } 73 74 public int read(byte[] b, int off, int len) throws IOException { 75 return rof.read(b, off, len); 76 } 77 78 public long skip(long n) throws IOException { 79 if (n > Integer.MAX_VALUE) 80 throw new IllegalArgumentException ("Too many bytes to skip!"); 81 return rof.skipBytes((int) n); 82 } 83 84 public int available() throws IOException { 85 final long avail = rof.length() - rof.getFilePointer(); 86 return avail > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) avail; 87 } 88 89 public void close() throws IOException { 90 rof.close(); 91 } 92 93 public void mark(int readlimit) { 94 try { 95 mark = rof.getFilePointer(); 96 } catch (IOException failure) { 97 Logger.getLogger(ReadOnlyFileInputStream.class.getName()) 98 .log(Level.WARNING, "mark()/reset() not supported!", failure); 99 mark = -2; 100 } 101 } 102 103 public void reset() throws IOException { 104 if (mark < 0) 105 throw new IOException (mark == -1 106 ? "No mark set!" 107 : "mark()/reset() not supported by underlying ReadOnlyFile!"); 108 rof.seek(mark); 109 } 110 111 public boolean markSupported() { 112 try { 113 rof.seek(rof.getFilePointer()); 114 return true; 115 } catch (IOException failure) { 116 return false; 117 } 118 } 119 } 120 | Popular Tags |