Avoid throwing plain RuntimeException (plus related polishing)

See gh-24805
This commit is contained in:
Juergen Hoeller
2020-09-18 19:01:49 +02:00
parent c1617d3c52
commit 82e64af5a5
3 changed files with 33 additions and 38 deletions

View File

@@ -97,8 +97,6 @@ public abstract class StreamUtils {
/**
* Copy the contents of the given {@link ByteArrayOutputStream} into a {@link String}.
* <p>This is a more effective equivalent of {@code new String(baos.toByteArray(), charset)}.
* <p>As long as the {@code charset} is already available at the point of
* invocation, no exception is expected to be thrown by this method.
* @param baos the {@code ByteArrayOutputStream} to be copied into a String
* @param charset the {@link Charset} to use to decode the bytes
* @return the String that has been copied to (possibly empty)
@@ -108,10 +106,12 @@ public abstract class StreamUtils {
Assert.notNull(baos, "No ByteArrayOutputStream specified");
Assert.notNull(charset, "No Charset specified");
try {
// Can be replaced with toString(Charset) call in Java 10+
return baos.toString(charset.name());
}
catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Failed to copy contents of ByteArrayOutputStream into a String", ex);
// Should never happen
throw new IllegalArgumentException("Invalid charset name: " + charset, ex);
}
}
@@ -261,6 +261,7 @@ public abstract class StreamUtils {
return new NonClosingOutputStream(out);
}
private static class NonClosingInputStream extends FilterInputStream {
public NonClosingInputStream(InputStream in) {