Skip to content

Commit 64d0249

Browse files
committed
#836 Simplify code of IOUtils
1 parent 4036bbd commit 64d0249

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed
Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: Copyright 2016-2023 Mark Rotteveel
1+
// SPDX-FileCopyrightText: Copyright 2016-2025 Mark Rotteveel
22
// SPDX-License-Identifier: LGPL-2.1-or-later
33
package org.firebirdsql.jaybird.util;
44

@@ -12,57 +12,55 @@
1212
*/
1313
public final class IOUtils {
1414

15+
private static final int TRANSFER_BUFFER_SIZE = 8192;
16+
1517
private IOUtils() {
1618
// No instances
1719
}
1820

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+
*/
1932
public static byte[] toBytes(final InputStream in, final int length) throws IOException {
2033
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();
4035
}
41-
return out.toByteArray();
36+
return in.readNBytes(length);
4237
}
4338

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+
*/
4450
public static String toString(final Reader in, final int length) throws IOException {
51+
var out = new StringWriter();
4552
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+
}
5562
}
5663
return out.toString();
5764
}
5865

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-
}
6866
}

src/test/org/firebirdsql/jdbc/field/FBStringFieldTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-FileCopyrightText: Copyright 2002 David Jencks
33
// SPDX-FileCopyrightText: Copyright 2002-2003 Blas Rodriguez Somoza
44
// SPDX-FileCopyrightText: Copyright 2003 Ryan Baldwin
5-
// SPDX-FileCopyrightText: Copyright 2014-2024 Mark Rotteveel
5+
// SPDX-FileCopyrightText: Copyright 2014-2025 Mark Rotteveel
66
// SPDX-License-Identifier: LGPL-2.1-or-later
77
package org.firebirdsql.jdbc.field;
88

@@ -126,7 +126,7 @@ void getBinaryStreamNull() throws SQLException {
126126
@Override
127127
void getBinaryStreamNonNull() throws Exception {
128128
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
129-
String fromStream = new String(IOUtils.toBytes(field.getBinaryStream(), Integer.MAX_VALUE));
129+
String fromStream = new String(field.getBinaryStream().readAllBytes());
130130

131131
assertEquals(TEST_STRING_SHORT, fromStream.trim(), "Binary stream values test failure");
132132
}
@@ -135,7 +135,7 @@ void getBinaryStreamNonNull() throws Exception {
135135
@Override
136136
void getObject_InputStream() throws Exception {
137137
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
138-
String fromStream = new String(IOUtils.toBytes(field.getObject(InputStream.class), Integer.MAX_VALUE));
138+
String fromStream = new String(field.getObject(InputStream.class).readAllBytes());
139139

140140
assertEquals(TEST_STRING_SHORT, fromStream.trim(), "Binary stream values test failure");
141141
}
@@ -303,7 +303,7 @@ void getCharacterStreamNull() throws SQLException {
303303
void getCharacterStreamNonNull() throws Exception {
304304
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
305305

306-
String value = IOUtils.toString(field.getCharacterStream(), Integer.MAX_VALUE);
306+
String value = IOUtils.toString(field.getCharacterStream(), -1);
307307
assertEquals(TEST_STRING_SHORT, value, "Unexpected value from getCharacterStream");
308308
}
309309

@@ -312,7 +312,7 @@ void getCharacterStreamNonNull() throws Exception {
312312
void getObject_Reader() throws Exception {
313313
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
314314

315-
String value = IOUtils.toString(field.getObject(Reader.class), Integer.MAX_VALUE);
315+
String value = IOUtils.toString(field.getObject(Reader.class), -1);
316316
assertEquals(TEST_STRING_SHORT, value, "Unexpected value from getCharacterStream");
317317
}
318318

0 commit comments

Comments
 (0)