Discards up to n bytes of data from the input stream. This method will block until either the full amount has been skipped or until the end of the stream is reached, whichever happens first. Returns the total number of bytes skipped.
(InputStream in, final long n)
| 798 | |
| 799 | |
| 800 | static long skipUpTo(InputStream in, final long n) throws IOException { |
| 801 | long totalSkipped = 0; |
| 802 | byte[] buf = createBuffer(); |
| 803 | while (totalSkipped < n) { |
| 804 | long remaining = n - totalSkipped; |
| 805 | long skipped = skipSafely(in, remaining); |
| 806 | if (skipped == 0) { |
| 807 | // Do a buffered read since skipSafely could return 0 repeatedly, for example if |
| 808 | // in.available() always returns 0 (the default). |
| 809 | int skip = (int) Math.min(remaining, buf.length); |
| 810 | if ((skipped = in.read(buf, 0, skip)) == -1) { |
| 811 | // Reached EOF |
| 812 | break; |
| 813 | } |
| 814 | } |
| 815 | totalSkipped += skipped; |
| 816 | } |
| 817 | return totalSkipped; |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * Attempts to skip up to {@code n} bytes from the given input stream, but not more than |
no test coverage detected