|
1 | | -// SPDX-FileCopyrightText: Copyright 2016-2023 Mark Rotteveel |
| 1 | +// SPDX-FileCopyrightText: Copyright 2016-2025 Mark Rotteveel |
2 | 2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
3 | 3 | package org.firebirdsql.jaybird.util; |
4 | 4 |
|
|
12 | 12 | */ |
13 | 13 | public final class IOUtils { |
14 | 14 |
|
| 15 | + private static final int TRANSFER_BUFFER_SIZE = 8192; |
| 16 | + |
15 | 17 | private IOUtils() { |
16 | 18 | // No instances |
17 | 19 | } |
18 | 20 |
|
| 21 | + /** |
| 22 | + * Reads {@code length} bytes from {@code in} to a byte array, or until EOF. |
| 23 | + * |
| 24 | + * @param in |
| 25 | + * input stream |
| 26 | + * @param length |
| 27 | + * number of bytes to read (or {@code -1} to read until EOF) |
| 28 | + * @return byte array; length is the actual number of bytes read when EOF was reached before {@code length} |
| 29 | + * @throws IOException |
| 30 | + * for exceptions reading from {@code in} |
| 31 | + */ |
19 | 32 | public static byte[] toBytes(final InputStream in, final int length) throws IOException { |
20 | 33 | if (length == -1) { |
21 | | - return toBytes(in); |
22 | | - } |
23 | | - final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
24 | | - final byte[] buff = new byte[Math.min(4096, length)]; |
25 | | - int counter; |
26 | | - int toRead = length; |
27 | | - while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) { |
28 | | - out.write(buff, 0, counter); |
29 | | - toRead -= counter; |
30 | | - } |
31 | | - return out.toByteArray(); |
32 | | - } |
33 | | - |
34 | | - public static byte[] toBytes(final InputStream in) throws IOException { |
35 | | - final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
36 | | - final byte[] buff = new byte[4096]; |
37 | | - int counter; |
38 | | - while ((counter = in.read(buff, 0, buff.length)) != -1) { |
39 | | - out.write(buff, 0, counter); |
| 34 | + return in.readAllBytes(); |
40 | 35 | } |
41 | | - return out.toByteArray(); |
| 36 | + return in.readNBytes(length); |
42 | 37 | } |
43 | 38 |
|
| 39 | + /** |
| 40 | + * Reads {@code length} characters from {@code in} to a string, or until EOF. |
| 41 | + * |
| 42 | + * @param in |
| 43 | + * input stream |
| 44 | + * @param length |
| 45 | + * number of characters to read (or {@code -1} to read until EOF) |
| 46 | + * @return string; length is the actual number of characters read when EOF was reached before {@code length} |
| 47 | + * @throws IOException |
| 48 | + * for exceptions reading from {@code in} |
| 49 | + */ |
44 | 50 | public static String toString(final Reader in, final int length) throws IOException { |
| 51 | + var out = new StringWriter(); |
45 | 52 | if (length == -1) { |
46 | | - return toString(in); |
47 | | - } |
48 | | - final StringWriter out = new StringWriter(); |
49 | | - final char[] buff = new char[Math.min(4096, length)]; |
50 | | - int counter; |
51 | | - int toRead = length; |
52 | | - while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) { |
53 | | - out.write(buff, 0, counter); |
54 | | - toRead -= counter; |
| 53 | + in.transferTo(out); |
| 54 | + } else { |
| 55 | + var buff = new char[Math.min(TRANSFER_BUFFER_SIZE, length)]; |
| 56 | + int counter; |
| 57 | + int toRead = length; |
| 58 | + while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) { |
| 59 | + out.write(buff, 0, counter); |
| 60 | + toRead -= counter; |
| 61 | + } |
55 | 62 | } |
56 | 63 | return out.toString(); |
57 | 64 | } |
58 | 65 |
|
59 | | - public static String toString(final Reader in) throws IOException { |
60 | | - final StringWriter out = new StringWriter(); |
61 | | - final char[] buff = new char[4096]; |
62 | | - int counter; |
63 | | - while ((counter = in.read(buff, 0, buff.length)) != -1) { |
64 | | - out.write(buff, 0, counter); |
65 | | - } |
66 | | - return out.toString(); |
67 | | - } |
68 | 66 | } |
0 commit comments