Skip to content

Commit

Permalink
#836 Simplify code of IOUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
mrotteveel committed Mar 4, 2025
1 parent 4036bbd commit 64d0249
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 44 deletions.
76 changes: 37 additions & 39 deletions src/main/org/firebirdsql/jaybird/util/IOUtils.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright 2016-2023 Mark Rotteveel
// SPDX-FileCopyrightText: Copyright 2016-2025 Mark Rotteveel
// SPDX-License-Identifier: LGPL-2.1-or-later
package org.firebirdsql.jaybird.util;

Expand All @@ -12,57 +12,55 @@
*/
public final class IOUtils {

private static final int TRANSFER_BUFFER_SIZE = 8192;

private IOUtils() {
// No instances
}

/**
* Reads {@code length} bytes from {@code in} to a byte array, or until EOF.
*
* @param in
* input stream
* @param length
* number of bytes to read (or {@code -1} to read until EOF)
* @return byte array; length is the actual number of bytes read when EOF was reached before {@code length}
* @throws IOException
* for exceptions reading from {@code in}
*/
public static byte[] toBytes(final InputStream in, final int length) throws IOException {
if (length == -1) {
return toBytes(in);
}
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final byte[] buff = new byte[Math.min(4096, length)];
int counter;
int toRead = length;
while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) {
out.write(buff, 0, counter);
toRead -= counter;
}
return out.toByteArray();
}

public static byte[] toBytes(final InputStream in) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final byte[] buff = new byte[4096];
int counter;
while ((counter = in.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, counter);
return in.readAllBytes();
}
return out.toByteArray();
return in.readNBytes(length);
}

/**
* Reads {@code length} characters from {@code in} to a string, or until EOF.
*
* @param in
* input stream
* @param length
* number of characters to read (or {@code -1} to read until EOF)
* @return string; length is the actual number of characters read when EOF was reached before {@code length}
* @throws IOException
* for exceptions reading from {@code in}
*/
public static String toString(final Reader in, final int length) throws IOException {
var out = new StringWriter();
if (length == -1) {
return toString(in);
}
final StringWriter out = new StringWriter();
final char[] buff = new char[Math.min(4096, length)];
int counter;
int toRead = length;
while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) {
out.write(buff, 0, counter);
toRead -= counter;
in.transferTo(out);
} else {
var buff = new char[Math.min(TRANSFER_BUFFER_SIZE, length)];
int counter;
int toRead = length;
while (toRead > 0 && (counter = in.read(buff, 0, Math.min(toRead, buff.length))) != -1) {
out.write(buff, 0, counter);
toRead -= counter;
}
}
return out.toString();
}

public static String toString(final Reader in) throws IOException {
final StringWriter out = new StringWriter();
final char[] buff = new char[4096];
int counter;
while ((counter = in.read(buff, 0, buff.length)) != -1) {
out.write(buff, 0, counter);
}
return out.toString();
}
}
10 changes: 5 additions & 5 deletions src/test/org/firebirdsql/jdbc/field/FBStringFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-FileCopyrightText: Copyright 2002 David Jencks
// SPDX-FileCopyrightText: Copyright 2002-2003 Blas Rodriguez Somoza
// SPDX-FileCopyrightText: Copyright 2003 Ryan Baldwin
// SPDX-FileCopyrightText: Copyright 2014-2024 Mark Rotteveel
// SPDX-FileCopyrightText: Copyright 2014-2025 Mark Rotteveel
// SPDX-License-Identifier: LGPL-2.1-or-later
package org.firebirdsql.jdbc.field;

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

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

assertEquals(TEST_STRING_SHORT, fromStream.trim(), "Binary stream values test failure");
}
Expand Down Expand Up @@ -303,7 +303,7 @@ void getCharacterStreamNull() throws SQLException {
void getCharacterStreamNonNull() throws Exception {
toReturnStringExpectations(TEST_STRING_SHORT, encoding);

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

Expand All @@ -312,7 +312,7 @@ void getCharacterStreamNonNull() throws Exception {
void getObject_Reader() throws Exception {
toReturnStringExpectations(TEST_STRING_SHORT, encoding);

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

Expand Down

0 comments on commit 64d0249

Please sign in to comment.