|
21 | 21 | import java.io.ByteArrayInputStream; |
22 | 22 | import java.io.CharArrayWriter; |
23 | 23 | import java.io.Closeable; |
| 24 | +import java.io.EOFException; |
24 | 25 | import java.io.File; |
25 | 26 | import java.io.IOException; |
26 | 27 | import java.io.InputStream; |
@@ -1398,4 +1399,51 @@ public static boolean contentEquals(Reader input1, Reader input2) |
1398 | 1399 | return (ch2 == -1); |
1399 | 1400 | } |
1400 | 1401 |
|
| 1402 | + /** |
| 1403 | + * Skip the requested number of bytes or fail if there are not enough left. |
| 1404 | + * <p> |
| 1405 | + * This allows for the possibility that {@link InputStream#skip(long)} may |
| 1406 | + * not skip as many bytes as requested (most likely because of reaching EOF). |
| 1407 | + * |
| 1408 | + * @param input stream to skip |
| 1409 | + * @param toSkip the number of bytes to skip |
| 1410 | + * @see InputStream#skip(long) |
| 1411 | + * |
| 1412 | + * @throws IOException if there is a problem reading the file |
| 1413 | + * @throws IllegalArgumentException if toSkip is negative |
| 1414 | + * @throws EOFException if the number of bytes skipped was incorrect |
| 1415 | + */ |
| 1416 | + public static void skipFully(InputStream input, long toSkip) throws IOException { |
| 1417 | + if (toSkip < 0){ |
| 1418 | + throw new IllegalArgumentException("Bytes to skip must not be negative: "+toSkip); |
| 1419 | + } |
| 1420 | + long skipped = input.skip(toSkip); |
| 1421 | + if (skipped != toSkip) { |
| 1422 | + throw new EOFException("Bytes to skip: "+toSkip+" actual: "+skipped); |
| 1423 | + } |
| 1424 | + } |
| 1425 | + |
| 1426 | + /** |
| 1427 | + * Skip the requested number of bytes or fail if there are not enough left. |
| 1428 | + * <p> |
| 1429 | + * This allows for the possibility that {@link Reader#skip(long)} may |
| 1430 | + * not skip as many bytes as requested (most likely because of reaching EOF). |
| 1431 | + * |
| 1432 | + * @param input stream to skip |
| 1433 | + * @param toSkip the number of bytes to skip |
| 1434 | + * @see Reader#skip(long) |
| 1435 | + * |
| 1436 | + * @throws IOException if there is a problem reading the file |
| 1437 | + * @throws IllegalArgumentException if toSkip is negative |
| 1438 | + * @throws EOFException if the number of bytes skipped was incorrect |
| 1439 | + */ |
| 1440 | + public static void skipFully(Reader input, long toSkip) throws IOException { |
| 1441 | + if (toSkip < 0){ |
| 1442 | + throw new IllegalArgumentException("Bytes to skip must not be negative: "+toSkip); |
| 1443 | + } |
| 1444 | + long skipped = input.skip(toSkip); |
| 1445 | + if (skipped != toSkip) { |
| 1446 | + throw new EOFException("Bytes to skip: "+toSkip+" actual: "+skipped); |
| 1447 | + } |
| 1448 | + } |
1401 | 1449 | } |
0 commit comments