Transfers data from an input stream to an output stream. @param in the input stream to transfer from @param out the output stream to transfer to (or null to discard output) @param len the number of bytes to transfer. If negative, the entire contents of the input stream are transferred. @thro
(InputStream in, OutputStream out, long len)
| 2703 | * before the requested number of bytes have been read |
| 2704 | */ |
| 2705 | public static void transfer(InputStream in, OutputStream out, long len) throws IOException { |
| 2706 | if (len == 0 || out == null && len < 0 && in.read() < 0) |
| 2707 | return; // small optimization - avoid buffer creation |
| 2708 | byte[] buf = new byte[4096]; |
| 2709 | while (len != 0) { |
| 2710 | int count = len < 0 || buf.length < len ? buf.length : (int)len; |
| 2711 | count = in.read(buf, 0, count); |
| 2712 | if (count < 0) { |
| 2713 | if (len > 0) |
| 2714 | throw new IOException("unexpected end of stream"); |
| 2715 | break; |
| 2716 | } |
| 2717 | if (out != null) |
| 2718 | out.write(buf, 0, count); |
| 2719 | len -= len > 0 ? count : 0; |
| 2720 | } |
| 2721 | } |
| 2722 | |
| 2723 | /** |
| 2724 | * Reads the token starting at the current stream position and ending at |
no test coverage detected