| 1 package com.quadcap.sql.file; 2 3 40 41 import java.io.InputStream ; 42 import java.io.IOException ; 43 44 import com.quadcap.util.Debug; 45 46 51 public class RandomAccessInputStream extends InputStream { 52 RandomAccess ra; 53 int position; 54 byte[] buf1 = new byte[1]; 55 56 public RandomAccessInputStream(RandomAccess ra) { 57 this.ra = ra; 58 this.position = 0; 59 } 60 61 public int getPosition() { return position; } 62 public void setPosition(int p) { position = p; } 63 64 75 public int read() throws IOException { 76 if (position >= ra.size()) return -1; 77 78 ra.read(position, buf1, 0, 1); 79 position++; 80 return buf1[0] & 0xff; 81 } 82 83 97 public int read(byte b[], int off, int len) throws IOException { 98 if (position >= ra.size()) return -1; 99 if (ra.size() - position < len) len = (int)(ra.size() - position); 100 101 if (b != null) ra.read(position, b, off, len); 102 position += len; 103 return len; 104 } 105 106 124 public long skip(long n) throws IOException { 125 return read(null, 0, (int)n); 126 } 127 128 138 public int available() throws IOException { 139 return (int)(ra.size() - position); 140 } 141 142 148 public void close() throws IOException { 149 ra.close(); 150 } 151 152 } 153 | Popular Tags |