Skip to content

Commit

Permalink
#836 Simplify code of IOUtils
Browse files Browse the repository at this point in the history
(cherry picked from commit 64d0249)
  • Loading branch information
mrotteveel committed Mar 5, 2025
1 parent cee57eb commit 74343cb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
74 changes: 36 additions & 38 deletions src/main/org/firebirdsql/jaybird/util/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,57 +28,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();
}
}
8 changes: 4 additions & 4 deletions src/test/org/firebirdsql/jdbc/field/FBStringFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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 @@ -147,7 +147,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 @@ -315,7 +315,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 @@ -324,7 +324,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 74343cb

Please sign in to comment.