@@ -26,25 +26,43 @@ public abstract class BaseLocalResponse {
2626 protected final byte [] file ;
2727
2828 /**
29- * Load from an {@link InputStream}.
29+ * Load from a {@link String}.
30+ *
31+ * @param input Assumes UTF-8 encoding.
32+ */
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.
3042 *
3143 * @param input will be decoded as UTF-8.
3244 */
33- public BaseLocalResponse (InputStream input ) {
34- this .file = this
35- .getBytes (new BufferedReader (new InputStreamReader (input , StandardCharsets .UTF_8 )).lines ());
45+ public BaseLocalResponse (byte [] input ) {
46+ if (input == null ) {
47+ throw new IllegalArgumentException ("Input byte array cannot be null." );
48+ }
49+ this .file = this .readToCleanUtf8Bytes (new String (input , StandardCharsets .UTF_8 ).lines ());
3650 }
3751
3852 /**
39- * Load from a {@link String}.
53+ * Load from an {@link InputStream}.
54+ * This method will not close the provided stream.
4055 *
4156 * @param input will be decoded as UTF-8.
4257 */
43- public BaseLocalResponse (String input ) {
44- if (input == null || input . isEmpty () ) {
45- throw new IllegalArgumentException ("Input string cannot be empty or null." );
58+ public BaseLocalResponse (InputStream input ) {
59+ if (input == null ) {
60+ throw new IllegalArgumentException ("Input stream cannot be null." );
4661 }
47- this .file = input .getBytes (StandardCharsets .UTF_8 );
62+ this .file = this
63+ .readToCleanUtf8Bytes (
64+ new BufferedReader (new InputStreamReader (input , StandardCharsets .UTF_8 )).lines ()
65+ );
4866 }
4967
5068 /**
@@ -53,7 +71,12 @@ public BaseLocalResponse(String input) {
5371 * @param input will be decoded as UTF-8.
5472 */
5573 public BaseLocalResponse (File input ) throws IOException {
56- this .file = this .getBytes (Files .lines (input .toPath (), StandardCharsets .UTF_8 ));
74+ if (input == null ) {
75+ throw new IllegalArgumentException ("Input file cannot be null." );
76+ }
77+ try (var lines = Files .lines (input .toPath (), StandardCharsets .UTF_8 )) {
78+ this .file = this .readToCleanUtf8Bytes (lines );
79+ }
5780 }
5881
5982 /**
@@ -62,11 +85,20 @@ public BaseLocalResponse(File input) throws IOException {
6285 * @param input will be decoded as UTF-8.
6386 */
6487 public BaseLocalResponse (Path input ) throws IOException {
65- this .file = this .getBytes (Files .lines (input , StandardCharsets .UTF_8 ));
88+ if (input == null ) {
89+ throw new IllegalArgumentException ("Input path cannot be null." );
90+ }
91+ try (var lines = Files .lines (input , StandardCharsets .UTF_8 )) {
92+ this .file = this .readToCleanUtf8Bytes (lines );
93+ }
6694 }
6795
68- private byte [] getBytes (Stream <String > stream ) {
69- return stream .collect (Collectors .joining ("" )).getBytes ();
96+ private byte [] readToCleanUtf8Bytes (Stream <String > stream ) {
97+ var cleanedString = stream .collect (Collectors .joining ("" ));
98+ if (cleanedString .trim ().isEmpty ()) {
99+ throw new IllegalArgumentException ("Input cannot be empty or contain only whitespace." );
100+ }
101+ return cleanedString .getBytes (StandardCharsets .UTF_8 );
70102 }
71103
72104 /**
@@ -120,4 +152,12 @@ public boolean isValidHmacSignature(String secretKey, String signature) {
120152
121153 return MessageDigest .isEqual (expectedBytes , actualBytes );
122154 }
155+
156+ /**
157+ * Print the file as a UTF-8 string.
158+ */
159+ @ Override
160+ public String toString () {
161+ return new String (this .file , StandardCharsets .UTF_8 );
162+ }
123163}
0 commit comments