|
28 | 28 | */ |
29 | 29 | public final class IOUtils { |
30 | 30 |
|
| 31 | + private static final int TRANSFER_BUFFER_SIZE = 8192; |
| 32 | + |
31 | 33 | private IOUtils() { |
32 | 34 | // No instances |
33 | 35 | } |
34 | 36 |
|
| 37 | + /** |
| 38 | + * Reads {@code length} bytes from {@code in} to a byte array, or until EOF. |
| 39 | + * |
| 40 | + * @param in |
| 41 | + * input stream |
| 42 | + * @param length |
| 43 | + * number of bytes to read (or {@code -1} to read until EOF) |
| 44 | + * @return byte array; length is the actual number of bytes read when EOF was reached before {@code length} |
| 45 | + * @throws IOException |
| 46 | + * for exceptions reading from {@code in} |
| 47 | + */ |
35 | 48 | public static byte[] toBytes(final InputStream in, final int length) throws IOException { |
36 | 49 | if (length == -1) { |
37 | | - return toBytes(in); |
38 | | - } |
39 | | - final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
40 | | - final byte[] buff = new byte[Math.min(4096, length)]; |
41 | | - int counter; |
42 | | - int toRead = length; |
43 | | - while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) { |
44 | | - out.write(buff, 0, counter); |
45 | | - toRead -= counter; |
46 | | - } |
47 | | - return out.toByteArray(); |
48 | | - } |
49 | | - |
50 | | - public static byte[] toBytes(final InputStream in) throws IOException { |
51 | | - final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
52 | | - final byte[] buff = new byte[4096]; |
53 | | - int counter; |
54 | | - while ((counter = in.read(buff, 0, buff.length)) != -1) { |
55 | | - out.write(buff, 0, counter); |
| 50 | + return in.readAllBytes(); |
56 | 51 | } |
57 | | - return out.toByteArray(); |
| 52 | + return in.readNBytes(length); |
58 | 53 | } |
59 | 54 |
|
| 55 | + /** |
| 56 | + * Reads {@code length} characters from {@code in} to a string, or until EOF. |
| 57 | + * |
| 58 | + * @param in |
| 59 | + * input stream |
| 60 | + * @param length |
| 61 | + * number of characters to read (or {@code -1} to read until EOF) |
| 62 | + * @return string; length is the actual number of characters read when EOF was reached before {@code length} |
| 63 | + * @throws IOException |
| 64 | + * for exceptions reading from {@code in} |
| 65 | + */ |
60 | 66 | public static String toString(final Reader in, final int length) throws IOException { |
| 67 | + var out = new StringWriter(); |
61 | 68 | if (length == -1) { |
62 | | - return toString(in); |
63 | | - } |
64 | | - final StringWriter out = new StringWriter(); |
65 | | - final char[] buff = new char[Math.min(4096, length)]; |
66 | | - int counter; |
67 | | - int toRead = length; |
68 | | - while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) { |
69 | | - out.write(buff, 0, counter); |
70 | | - toRead -= counter; |
| 69 | + in.transferTo(out); |
| 70 | + } else { |
| 71 | + var buff = new char[Math.min(TRANSFER_BUFFER_SIZE, length)]; |
| 72 | + int counter; |
| 73 | + int toRead = length; |
| 74 | + while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) { |
| 75 | + out.write(buff, 0, counter); |
| 76 | + toRead -= counter; |
| 77 | + } |
71 | 78 | } |
72 | 79 | return out.toString(); |
73 | 80 | } |
74 | 81 |
|
75 | | - public static String toString(final Reader in) throws IOException { |
76 | | - final StringWriter out = new StringWriter(); |
77 | | - final char[] buff = new char[4096]; |
78 | | - int counter; |
79 | | - while ((counter = in.read(buff, 0, buff.length)) != -1) { |
80 | | - out.write(buff, 0, counter); |
81 | | - } |
82 | | - return out.toString(); |
83 | | - } |
84 | 82 | } |
0 commit comments