Reads some bytes from an input stream and stores them into the buffer array {@code b}. This method blocks until {@code len} bytes of input data have been read into the array, or end of file is detected. The number of bytes read is returned, possibly zero. Does not close the stream. <p>A caller can
(InputStream in, byte[] b, int off, int len)
| 931 | * {@code off + len} is greater than {@code b.length} |
| 932 | */ |
| 933 | @CanIgnoreReturnValue |
| 934 | // Sometimes you don't care how many bytes you actually read, I guess. |
| 935 | // (You know that it's either going to read len bytes or stop at EOF.) |
| 936 | public static int read(InputStream in, byte[] b, int off, int len) throws IOException { |
| 937 | checkNotNull(in); |
| 938 | checkNotNull(b); |
| 939 | if (len < 0) { |
| 940 | throw new IndexOutOfBoundsException(String.format("len (%s) cannot be negative", len)); |
| 941 | } |
| 942 | checkPositionIndexes(off, off + len, b.length); |
| 943 | int total = 0; |
| 944 | while (total < len) { |
| 945 | int result = in.read(b, off + total, len - total); |
| 946 | if (result == -1) { |
| 947 | break; |
| 948 | } |
| 949 | total += result; |
| 950 | } |
| 951 | return total; |
| 952 | } |
| 953 | |
| 954 | /** Compares the contents of the two {@link InputStream}s for equality. */ |
| 955 | static boolean contentsEqual(InputStream in1, InputStream in2) throws IOException { |
no test coverage detected