Discards up to {@code 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, long n)
| 839 | * happens first. Returns the total number of bytes skipped. |
| 840 | */ |
| 841 | static long skipUpTo(InputStream in, long n) throws IOException { |
| 842 | long totalSkipped = 0; |
| 843 | // A buffer is allocated if skipSafely does not skip any bytes. |
| 844 | byte[] buf = null; |
| 845 | |
| 846 | while (totalSkipped < n) { |
| 847 | long remaining = n - totalSkipped; |
| 848 | long skipped = skipSafely(in, remaining); |
| 849 | |
| 850 | if (skipped == 0) { |
| 851 | // Do a buffered read since skipSafely could return 0 repeatedly, for example if |
| 852 | // in.available() always returns 0 (the default). |
| 853 | int skip = (int) min(remaining, BUFFER_SIZE); |
| 854 | if (buf == null) { |
| 855 | // Allocate a buffer bounded by the maximum size that can be requested, for |
| 856 | // example an array of BUFFER_SIZE is unnecessary when the value of remaining |
| 857 | // is smaller. |
| 858 | buf = new byte[skip]; |
| 859 | } |
| 860 | if ((skipped = in.read(buf, 0, skip)) == -1) { |
| 861 | // Reached EOF |
| 862 | break; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | totalSkipped += skipped; |
| 867 | } |
| 868 | |
| 869 | return totalSkipped; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Attempts to skip up to {@code n} bytes from the given input stream, but not more than {@code |
no test coverage detected