| 241 | } |
| 242 | |
| 243 | public int seek(int start, int length) throws IOException { |
| 244 | int fileLength = (int) file.length(); |
| 245 | |
| 246 | if (length > data.length) { |
| 247 | throw new IllegalArgumentException |
| 248 | ("length " + length + " greater than buffer length " + data.length); |
| 249 | } |
| 250 | |
| 251 | if (start < 0) { |
| 252 | throw new IllegalArgumentException("negative start " + start); |
| 253 | } |
| 254 | |
| 255 | if (start + length > fileLength) { |
| 256 | throw new IllegalArgumentException |
| 257 | ("end " + (start + length) + " greater than file length " + |
| 258 | fileLength); |
| 259 | } |
| 260 | |
| 261 | if (start < this.start || start + length > this.start + this.length) { |
| 262 | this.length = Math.min(data.length, fileLength); |
| 263 | this.start = start - ((this.length - length) / 2); |
| 264 | if (this.start < 0) { |
| 265 | this.start = 0; |
| 266 | } else if (this.start + this.length > fileLength) { |
| 267 | this.start = fileLength - this.length; |
| 268 | } |
| 269 | file.seek(this.start); |
| 270 | file.readFully(data, 0, this.length); |
| 271 | } |
| 272 | |
| 273 | return start - this.start; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | protected interface MyEntry { |