Skip to content

Commit d190051

Browse files
committed
Better exception messages
Better unit test messages
1 parent 6b41b2e commit d190051

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

src/main/java/org/apache/commons/io/IOUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,15 +2898,16 @@ public static byte[] toByteArray(final InputStream input, final int size) throws
28982898
public static byte[] toByteArray(final InputStream input, final int size, final int chunkSize) throws IOException {
28992899
Objects.requireNonNull(input, "input");
29002900
if (chunkSize <= 0) {
2901-
throw new IllegalArgumentException("Chunk size must be greater than zero: " + chunkSize);
2901+
throw new IllegalArgumentException(String.format("chunkSize <= 0, chunkSize = %,d", chunkSize));
29022902
}
29032903
if (size <= chunkSize) {
29042904
// throws if size < 0
29052905
return toByteArray(input::read, size);
29062906
}
29072907
final UnsynchronizedByteArrayOutputStream output = copyToOutputStream(input, size, chunkSize);
2908-
if (output.size() != size) {
2909-
throw new EOFException("Unexpected read size, current: " + output.size() + ", expected: " + size);
2908+
final int outSize = output.size();
2909+
if (outSize != size) {
2910+
throw new EOFException(String.format("Expected read size: %,d, actual: %,d", size, outSize));
29102911
}
29112912
return output.toByteArray();
29122913
}
@@ -2930,7 +2931,7 @@ public static byte[] toByteArray(final InputStream input, final int size, final
29302931
*/
29312932
public static byte[] toByteArray(final InputStream input, final long size) throws IOException {
29322933
if (size > Integer.MAX_VALUE) {
2933-
throw new IllegalArgumentException("Size cannot be greater than Integer max value: " + size);
2934+
throw new IllegalArgumentException(String.format("size > Integer.MAX_VALUE, size = %,d", size));
29342935
}
29352936
return toByteArray(input, (int) size);
29362937
}
@@ -2947,7 +2948,7 @@ public static byte[] toByteArray(final InputStream input, final long size) throw
29472948
*/
29482949
static byte[] toByteArray(final IOTriFunction<byte[], Integer, Integer, Integer> input, final int size) throws IOException {
29492950
if (size < 0) {
2950-
throw new IllegalArgumentException("Size must be equal or greater than zero: " + size);
2951+
throw new IllegalArgumentException(String.format("size < 0, size = %,d", size));
29512952
}
29522953
if (size == 0) {
29532954
return EMPTY_BYTE_ARRAY;
@@ -2959,7 +2960,7 @@ static byte[] toByteArray(final IOTriFunction<byte[], Integer, Integer, Integer>
29592960
offset += read;
29602961
}
29612962
if (offset != size) {
2962-
throw new EOFException("Unexpected read size, current: " + offset + ", expected: " + size);
2963+
throw new EOFException(String.format("Expected read size: %,d, actual: %,d", size, offset));
29632964
}
29642965
return data;
29652966
}

src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ private ReadAheadInputStream(final InputStream inputStream, final int bufferSize
239239
final boolean shutdownExecutorService) {
240240
super(Objects.requireNonNull(inputStream, "inputStream"));
241241
if (bufferSizeInBytes <= 0) {
242-
throw new IllegalArgumentException("bufferSizeInBytes should be greater than 0, but the value is " + bufferSizeInBytes);
242+
throw new IllegalArgumentException(String.format("bufferSizeInBytes <= 0, bufferSizeInBytes = %,d", bufferSizeInBytes));
243243
}
244244
this.executorService = Objects.requireNonNull(executorService, "executorService");
245245
this.shutdownExecutorService = shutdownExecutorService;

src/test/java/org/apache/commons/io/IOUtilsTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,10 +1772,8 @@ void testToByteArray_InputStream_LongerThanIntegerMaxValue() throws Exception {
17721772
@Test
17731773
void testToByteArray_InputStream_NegativeSize() throws Exception {
17741774
try (InputStream fin = Files.newInputStream(testFilePath)) {
1775-
final IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> IOUtils.toByteArray(fin, -1),
1776-
"Should have failed with IllegalArgumentException");
1777-
assertTrue(exc.getMessage().startsWith("Size must be equal or greater than zero"),
1778-
"Exception message does not start with \"Size must be equal or greater than zero\"");
1775+
final IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> IOUtils.toByteArray(fin, -1));
1776+
assertTrue(exc.getMessage().startsWith("size < 0"), exc.getMessage());
17791777
}
17801778
}
17811779

@@ -1820,7 +1818,7 @@ void testToByteArray_InputStream_SizeIllegal() throws Exception {
18201818
try (InputStream fin = Files.newInputStream(testFilePath)) {
18211819
final IOException exc = assertThrows(IOException.class, () -> IOUtils.toByteArray(fin, testFile.length() + 1),
18221820
"Should have failed with IOException");
1823-
assertTrue(exc.getMessage().startsWith("Unexpected read size"), "Exception message does not start with \"Unexpected read size\"");
1821+
assertTrue(exc.getMessage().startsWith("Expected read size"), exc.getMessage());
18241822
}
18251823
}
18261824

@@ -1829,8 +1827,7 @@ void testToByteArray_InputStream_SizeLong() throws Exception {
18291827
try (InputStream fin = Files.newInputStream(testFilePath)) {
18301828
final IllegalArgumentException exc = assertThrows(IllegalArgumentException.class, () -> IOUtils.toByteArray(fin, (long) Integer.MAX_VALUE + 1),
18311829
"Should have failed with IllegalArgumentException");
1832-
assertTrue(exc.getMessage().startsWith("Size cannot be greater than Integer max value"),
1833-
"Exception message does not start with \"Size cannot be greater than Integer max value\"");
1830+
assertTrue(exc.getMessage().startsWith("size > Integer.MAX_VALUE"), exc.getMessage());
18341831
}
18351832
}
18361833

0 commit comments

Comments
 (0)