Skip to content

Commit 74343cb

Browse files
committed
#836 Simplify code of IOUtils
(cherry picked from commit 64d0249)
1 parent cee57eb commit 74343cb

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

src/main/org/firebirdsql/jaybird/util/IOUtils.java

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -28,57 +28,55 @@
2828
*/
2929
public final class IOUtils {
3030

31+
private static final int TRANSFER_BUFFER_SIZE = 8192;
32+
3133
private IOUtils() {
3234
// No instances
3335
}
3436

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+
*/
3548
public static byte[] toBytes(final InputStream in, final int length) throws IOException {
3649
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();
5651
}
57-
return out.toByteArray();
52+
return in.readNBytes(length);
5853
}
5954

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+
*/
6066
public static String toString(final Reader in, final int length) throws IOException {
67+
var out = new StringWriter();
6168
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+
}
7178
}
7279
return out.toString();
7380
}
7481

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

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ void getBinaryStreamNull() throws SQLException {
138138
@Override
139139
void getBinaryStreamNonNull() throws Exception {
140140
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
141-
String fromStream = new String(IOUtils.toBytes(field.getBinaryStream(), Integer.MAX_VALUE));
141+
String fromStream = new String(field.getBinaryStream().readAllBytes());
142142

143143
assertEquals(TEST_STRING_SHORT, fromStream.trim(), "Binary stream values test failure");
144144
}
@@ -147,7 +147,7 @@ void getBinaryStreamNonNull() throws Exception {
147147
@Override
148148
void getObject_InputStream() throws Exception {
149149
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
150-
String fromStream = new String(IOUtils.toBytes(field.getObject(InputStream.class), Integer.MAX_VALUE));
150+
String fromStream = new String(field.getObject(InputStream.class).readAllBytes());
151151

152152
assertEquals(TEST_STRING_SHORT, fromStream.trim(), "Binary stream values test failure");
153153
}
@@ -315,7 +315,7 @@ void getCharacterStreamNull() throws SQLException {
315315
void getCharacterStreamNonNull() throws Exception {
316316
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
317317

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

@@ -324,7 +324,7 @@ void getCharacterStreamNonNull() throws Exception {
324324
void getObject_Reader() throws Exception {
325325
toReturnStringExpectations(TEST_STRING_SHORT, encoding);
326326

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

0 commit comments

Comments
 (0)