1 11 package org.eclipse.core.internal.registry; 12 13 import java.io.*; 14 15 18 public class BufferedRandomInputStream extends InputStream { 19 20 private RandomAccessFile inputFile; 21 private String filePath; private int buffer_size; private int buffer_pos; 27 private long buffer_start = 0; 28 29 32 private long file_pointer; 33 34 private byte buffer[]; 35 36 public BufferedRandomInputStream(File file) throws IOException { 37 this(file, 2048); } 39 40 public BufferedRandomInputStream(File file, int bufferSize) throws IOException { 41 filePath = file.getCanonicalPath(); 42 inputFile = new RandomAccessFile(file, "r"); buffer = new byte[bufferSize]; 44 file_pointer = 0; 45 resetBuffer(); 46 } 47 48 private void resetBuffer() { 49 buffer_pos = 0; 50 buffer_size = 0; 51 buffer_start = 0; 52 } 53 54 private int fillBuffer() throws IOException { 55 buffer_pos = 0; 56 buffer_start = file_pointer; 57 buffer_size = inputFile.read(buffer, 0, buffer.length); 58 file_pointer += buffer_size; 59 return buffer_size; 60 } 61 62 public int read() throws IOException { 63 if (buffer_pos >= buffer_size) { 64 if (fillBuffer() <= 0) 65 return -1; 66 } 67 return buffer[buffer_pos++] & 0xFF; 68 } 69 70 public int read(byte b[], int off, int len) throws IOException { 71 int available = buffer_size - buffer_pos; 72 if (available < 0) 73 return -1; 74 if (len <= available) { 76 System.arraycopy(buffer, buffer_pos, b, off, len); 77 buffer_pos += len; 78 return len; 79 } 80 System.arraycopy(buffer, buffer_pos, b, off, available); 82 if (fillBuffer() <= 0) 83 return available; 84 return available + read(b, off + available, len - available); 86 } 87 88 public long skip(long n) throws IOException { 89 if (n <= 0) 90 return 0; 91 92 int available = buffer_size - buffer_pos; 93 if (n <= available) { 94 buffer_pos += n; 95 return n; 96 } 97 resetBuffer(); 98 final int skipped = inputFile.skipBytes((int) (n - available)); 99 file_pointer += skipped; 100 return available + skipped; 101 } 102 103 public int available() throws IOException { 104 return (buffer_size - buffer_pos); 105 } 106 107 public void close() throws IOException { 108 inputFile.close(); 109 inputFile = null; 110 buffer = null; 111 } 112 113 public String toString() { 114 return filePath; 115 } 116 117 124 public void seek(long pos) throws IOException { 125 if (pos >= buffer_start && pos < buffer_start + buffer_size) { 126 buffer_pos = (int) (pos - buffer_start); 128 } else { 129 inputFile.seek(pos); 131 file_pointer = pos; 132 resetBuffer(); 133 } 134 } 135 136 141 public long length() throws IOException { 142 return inputFile.length(); 143 } 144 145 } 146 | Popular Tags |