@@ -26,25 +26,48 @@ public abstract class BaseLocalResponse {
2626 protected final byte [] file ;
2727
2828 /**
29- * Load from an {@link InputStream }.
29+ * Load from a {@link String }.
3030 *
3131 * @param input will be decoded as UTF-8.
3232 */
33- public BaseLocalResponse (InputStream input ) {
33+ public BaseLocalResponse (String input ) {
34+ if (input == null ) {
35+ throw new IllegalArgumentException ("Input string cannot be null." );
36+ }
37+ this .file = this .readToCleanUtf8Bytes (input .lines ());
38+ }
39+
40+ /**
41+ * Load from a byte array.
42+ *
43+ * @param input will be decoded as UTF-8.
44+ */
45+ public BaseLocalResponse (byte [] input ) {
46+ if (input == null ) {
47+ throw new IllegalArgumentException ("Input byte array cannot be null." );
48+ }
3449 this .file = this
35- .getBytes (new BufferedReader (new InputStreamReader (input , StandardCharsets .UTF_8 )).lines ());
50+ .readToCleanUtf8Bytes (
51+ new BufferedReader (
52+ new InputStreamReader (new java .io .ByteArrayInputStream (input ), StandardCharsets .UTF_8 )
53+ ).lines ()
54+ );
3655 }
3756
3857 /**
39- * Load from a {@link String}.
58+ * Load from an {@link InputStream}.
59+ * This method will not close the provided stream.
4060 *
4161 * @param input will be decoded as UTF-8.
4262 */
43- public BaseLocalResponse (String input ) {
44- if (input == null || input . isEmpty () ) {
45- throw new IllegalArgumentException ("Input string cannot be empty or null." );
63+ public BaseLocalResponse (InputStream input ) {
64+ if (input == null ) {
65+ throw new IllegalArgumentException ("Input stream cannot be null." );
4666 }
47- this .file = input .getBytes (StandardCharsets .UTF_8 );
67+ this .file = this
68+ .readToCleanUtf8Bytes (
69+ new BufferedReader (new InputStreamReader (input , StandardCharsets .UTF_8 )).lines ()
70+ );
4871 }
4972
5073 /**
@@ -53,7 +76,10 @@ public BaseLocalResponse(String input) {
5376 * @param input will be decoded as UTF-8.
5477 */
5578 public BaseLocalResponse (File input ) throws IOException {
56- this .file = this .getBytes (Files .lines (input .toPath (), StandardCharsets .UTF_8 ));
79+ if (input == null ) {
80+ throw new IllegalArgumentException ("Input file cannot be null." );
81+ }
82+ this .file = this .readToCleanUtf8Bytes (Files .lines (input .toPath (), StandardCharsets .UTF_8 ));
5783 }
5884
5985 /**
@@ -62,11 +88,18 @@ public BaseLocalResponse(File input) throws IOException {
6288 * @param input will be decoded as UTF-8.
6389 */
6490 public BaseLocalResponse (Path input ) throws IOException {
65- this .file = this .getBytes (Files .lines (input , StandardCharsets .UTF_8 ));
91+ if (input == null ) {
92+ throw new IllegalArgumentException ("Input path cannot be null." );
93+ }
94+ this .file = this .readToCleanUtf8Bytes (Files .lines (input , StandardCharsets .UTF_8 ));
6695 }
6796
68- private byte [] getBytes (Stream <String > stream ) {
69- return stream .collect (Collectors .joining ("" )).getBytes ();
97+ private byte [] readToCleanUtf8Bytes (Stream <String > stream ) {
98+ var cleanedString = stream .collect (Collectors .joining ("" ));
99+ if (cleanedString .trim ().isEmpty ()) {
100+ throw new IllegalArgumentException ("Input cannot be empty or contain only whitespace." );
101+ }
102+ return cleanedString .getBytes (StandardCharsets .UTF_8 );
70103 }
71104
72105 /**
@@ -120,4 +153,12 @@ public boolean isValidHmacSignature(String secretKey, String signature) {
120153
121154 return MessageDigest .isEqual (expectedBytes , actualBytes );
122155 }
156+
157+ /**
158+ * Print the file as a UTF-8 string.
159+ */
160+ @ Override
161+ public String toString () {
162+ return new String (this .file , StandardCharsets .UTF_8 );
163+ }
123164}
0 commit comments