Read Properly handles partial reads
(byte[] buffer, int len, RandomAccessFile f)
| 278 | * Properly handles partial reads |
| 279 | */ |
| 280 | public static void Read(byte[] buffer, int len, RandomAccessFile f) { |
| 281 | |
| 282 | int offset = 0; |
| 283 | int read = 0; |
| 284 | int tries = 0; |
| 285 | // read in chunks for progress bar |
| 286 | int remaining = len; |
| 287 | int block; |
| 288 | |
| 289 | while (remaining != 0) { |
| 290 | block = Math.min(remaining, MAX_READ); |
| 291 | try { |
| 292 | read = f.read(buffer, offset, block); |
| 293 | } catch (IOException e) { |
| 294 | Com.Error(Defines.ERR_FATAL, e.toString()); |
| 295 | } |
| 296 | |
| 297 | if (read == 0) { |
| 298 | |
| 299 | // we might have been trying to read from a CD |
| 300 | if (tries == 0) |
| 301 | { |
| 302 | tries = 1; |
| 303 | // todo: check if this hack is requried (does anyone has CD these days?) |
| 304 | // CDAudio.Stop(); |
| 305 | } else { |
| 306 | Com.Error(Defines.ERR_FATAL, "FS_Read: 0 bytes read"); |
| 307 | } |
| 308 | |
| 309 | } else if (read == -1) { |
| 310 | Com.Error(Defines.ERR_FATAL, "FS_Read: -1 bytes read"); |
| 311 | } |
| 312 | // |
| 313 | // do some progress bar thing here... |
| 314 | // |
| 315 | remaining -= read; |
| 316 | offset += read; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * LoadFile |
no test coverage detected