Lines 143-168
Link Here
|
143 |
actual.close(); |
143 |
actual.close(); |
144 |
} |
144 |
} |
145 |
|
145 |
|
146 |
private void ensureOpened() throws FileNotFoundException { |
146 |
/** |
|
|
147 |
* Ensures the file has been opened. |
148 |
* |
149 |
* @return <code>true</code> if the file is already open or was successfully opened, <code>false</code> otherwise |
150 |
* @throws FileNotFoundException |
151 |
*/ |
152 |
private boolean ensureOpened() throws FileNotFoundException { |
147 |
if (actual != null) |
153 |
if (actual != null) |
148 |
return; |
154 |
// has already been opened (may have been closed though) |
|
|
155 |
return true; |
156 |
// first time we try reading it |
149 |
if (target == null) |
157 |
if (target == null) |
150 |
throw new FileNotFoundException(); |
158 |
// if the file does not exist, its location will be null |
|
|
159 |
return false; |
160 |
java.io.File targetFile = target.toFile(); |
161 |
if (!targetFile.isFile()) |
162 |
// the file may have been deleted concurrently (or was out-of-sync) |
163 |
return false; |
164 |
// the file exists, we are happy |
151 |
actual = new FileInputStream(target.toFile()); |
165 |
actual = new FileInputStream(target.toFile()); |
|
|
166 |
return true; |
152 |
} |
167 |
} |
153 |
|
168 |
|
154 |
public int read() throws IOException { |
169 |
public int read() throws IOException { |
155 |
ensureOpened(); |
170 |
if (!ensureOpened()) |
|
|
171 |
return -1; |
156 |
return actual.read(); |
172 |
return actual.read(); |
157 |
} |
173 |
} |
158 |
|
174 |
|
159 |
public int read(byte[] b, int off, int len) throws IOException { |
175 |
public int read(byte[] b, int off, int len) throws IOException { |
160 |
ensureOpened(); |
176 |
if (!ensureOpened()) |
|
|
177 |
return -1; |
161 |
return actual.read(b, off, len); |
178 |
return actual.read(b, off, len); |
162 |
} |
179 |
} |
163 |
|
180 |
|
164 |
public long skip(long n) throws IOException { |
181 |
public long skip(long n) throws IOException { |
165 |
ensureOpened(); |
182 |
if (!ensureOpened()) |
|
|
183 |
return 0; |
166 |
return actual.skip(n); |
184 |
return actual.skip(n); |
167 |
} |
185 |
} |
168 |
} |
186 |
} |