1 11 12 package org.eclipse.osgi.framework.internal.reliablefile; 13 14 import java.io.*; 15 16 24 public class ReliableFileInputStream extends FilterInputStream { 25 28 private ReliableFile reliable; 29 30 33 private int sigSize; 34 35 38 private int readPos; 39 40 43 private int length; 44 45 54 public ReliableFileInputStream(String name) throws IOException { 55 this(ReliableFile.getReliableFile(name), ReliableFile.GENERATION_LATEST, ReliableFile.OPEN_BEST_AVAILABLE); 56 } 57 58 65 public ReliableFileInputStream(File file) throws IOException { 66 this(ReliableFile.getReliableFile(file), ReliableFile.GENERATION_LATEST, ReliableFile.OPEN_BEST_AVAILABLE); 67 } 68 69 79 public ReliableFileInputStream(File file, int generation, int openMask) throws IOException { 80 this(ReliableFile.getReliableFile(file), generation, openMask); 81 } 82 83 91 private ReliableFileInputStream(ReliableFile reliable, int generation, int openMask) throws IOException { 92 super(reliable.getInputStream(generation, openMask)); 93 94 this.reliable = reliable; 95 sigSize = reliable.getSignatureSize(); 96 readPos = 0; 97 length = super.available(); 98 if (sigSize > length) 99 length = 0; else 101 length -= sigSize; 102 } 103 104 110 public synchronized void close() throws IOException { 111 if (reliable != null) { 112 try { 113 super.close(); 114 } finally { 115 reliable.closeInputFile(); 116 reliable = null; 117 } 118 } 119 } 120 121 125 public synchronized int read(byte b[], int off, int len) throws IOException { 126 if (readPos >= length) { 127 return -1; 128 } 129 int num = super.read(b, off, len); 130 131 if (num != -1) { 132 if (num + readPos > length) { 133 num = length - readPos; 134 } 135 readPos += num; 136 } 137 return num; 138 } 139 140 144 public synchronized int read(byte b[]) throws IOException { 145 return read(b, 0, b.length); 146 } 147 148 152 public synchronized int read() throws IOException { 153 if (readPos >= length) { 154 return -1; 155 } 156 int num = super.read(); 157 158 if (num != -1) { 159 readPos++; 160 } 161 return num; 162 } 163 164 168 public synchronized int available() throws IOException { 169 if (readPos < length) return (length - readPos); 171 return 0; 172 } 173 174 178 public synchronized long skip(long n) throws IOException { 179 long len = super.skip(n); 180 if (readPos + len > length) 181 len = length - readPos; 182 readPos += len; 183 return len; 184 } 185 186 190 public boolean markSupported() { 191 return false; 192 } 193 194 198 public void mark(int readlimit) { 199 } 201 202 206 public void reset() throws IOException { 207 throw new IOException("reset not supported."); } 209 } 210 | Popular Tags |