@@ -116,47 +116,94 @@ public static List<String> getLines(BufferedReader reader, int count) throws IOE
116
116
return lines ;
117
117
}
118
118
119
+ /**
120
+ * Loads the string as a classpath resource or a path, using UTF-8.
121
+ * @param path The path to load.
122
+ * @return A buffered reader.
123
+ * @throws FileNotFoundException If the path wasn't found.
124
+ */
119
125
public static BufferedReader getReader (String path ) throws FileNotFoundException {
120
126
return getReader (path , StandardCharsets .UTF_8 );
121
127
}
122
128
123
- public static BufferedReader getReader (String path , Charset charSet ) throws FileNotFoundException {
124
- return new BufferedReader (new InputStreamReader (getInputStream (path ),charSet ),BUFFER_SIZE );
129
+ /**
130
+ * Loads the string as a classpath resource or a path, using the specified charset.
131
+ * @param path The path to load.
132
+ * @param charset The charset to use.
133
+ * @return A buffered reader.
134
+ * @throws FileNotFoundException If the path wasn't found.
135
+ */
136
+ public static BufferedReader getReader (String path , Charset charset ) throws FileNotFoundException {
137
+ return new BufferedReader (new InputStreamReader (getInputStream (path ),charset ),BUFFER_SIZE );
125
138
}
126
139
127
- public static BufferedReader getReader (URI uri , Charset charSet ) throws IOException {
128
- InputStream is = uri .toURL ().openStream ();
129
- return new BufferedReader (new InputStreamReader (is ,charSet ),BUFFER_SIZE );
140
+ /**
141
+ * This method converts the URI into a URL, and applies the
142
+ * protocol check to see if it's http or https. If it is then an exception is thrown, otherwise
143
+ * the stream is opened. Figures out if the stream is zipped using the magic bytes.
144
+ * @param uri The URI to load.
145
+ * @param charset The charset to use.
146
+ * @return A buffered reader from the input stream.
147
+ * @throws IOException If the URI failed to open.
148
+ */
149
+ public static BufferedReader getReader (URI uri , Charset charset ) throws IOException {
150
+ return getReader (uri .toURL (),charset );
130
151
}
131
152
132
- public static BufferedReader getReader (Path path , Charset charSet ) throws IOException {
133
- InputStream is = new FileInputStream (path .toFile ());
134
- return new BufferedReader (new InputStreamReader (is ,charSet ),BUFFER_SIZE );
153
+ /**
154
+ * This method applies the
155
+ * protocol check to see if the URL is http or https. If it is then an exception is thrown, otherwise
156
+ * the stream is opened. Figures out if the stream is zipped using the magic bytes.
157
+ * @param url The URL to load.
158
+ * @param charset The charset to use.
159
+ * @return A buffered reader from the input stream.
160
+ * @throws IOException If the URI failed to open.
161
+ */
162
+ public static BufferedReader getReader (URL url , Charset charset ) throws IOException {
163
+ if (isDisallowedProtocol (url )) {
164
+ throw new IllegalArgumentException ("Tried to read disallowed URL protocol: '" + url .toString () + "'" );
165
+ }
166
+ return getReader (url .openStream (),charset );
167
+ }
168
+
169
+ /**
170
+ * Opens a buffered reader on the specified path with the specified charset. Figures out if the stream is zipped using the magic bytes.
171
+ * @param path The path to read.
172
+ * @param charset The charset to use.
173
+ * @return A buffered reader.
174
+ * @throws IOException If the path failed to open.
175
+ */
176
+ public static BufferedReader getReader (Path path , Charset charset ) throws IOException {
177
+ return getReader (new FileInputStream (path .toFile ()),charset );
135
178
}
136
179
137
180
/**
138
181
* Makes a reader wrapped around the string. Figures out if the stream is zipped using the magic bytes.
139
182
* @param filename The input filename.
140
- * @param charSet The charset to use.
183
+ * @param charset The charset to use.
141
184
* @return A BufferedReader wrapped around the appropriate stream.
142
185
* @throws FileNotFoundException If the file can't be read.
143
186
* @throws IOException If an error occurred when opening the file.
144
187
*/
145
- public static BufferedReader getReader (String filename , String charSet ) throws FileNotFoundException , IOException {
146
- return getReader (new File (filename ), charSet );
188
+ public static BufferedReader getReader (String filename , String charset ) throws FileNotFoundException , IOException {
189
+ return getReader (new File (filename ), charset );
147
190
}
148
191
149
192
/**
150
193
* Makes a reader wrapped around the file. Figures out if the stream is zipped using the magic bytes.
151
194
* @param file The file to read.
152
- * @param charSet The charset to use.
195
+ * @param charset The charset to use.
153
196
* @return A BufferedReader wrapped around the appropriate stream.
154
197
* @throws FileNotFoundException If the file can't be read.
155
198
* @throws IOException If an error occurred when opening the file.
156
199
*/
157
- public static BufferedReader getReader (File file , String charSet ) throws FileNotFoundException , IOException {
158
- InputStream stream = wrapGZIPStream (new FileInputStream (file ));
159
- return new BufferedReader (new InputStreamReader (stream ,charSet ));
200
+ public static BufferedReader getReader (File file , String charset ) throws FileNotFoundException , IOException {
201
+ return getReader (new FileInputStream (file ),Charset .forName (charset ));
202
+ }
203
+
204
+ private static BufferedReader getReader (InputStream stream , Charset charset ) throws IOException {
205
+ InputStream wrappedStream = wrapGZIPStream (stream );
206
+ return new BufferedReader (new InputStreamReader (wrappedStream ,charset ),BUFFER_SIZE );
160
207
}
161
208
162
209
/**
@@ -246,12 +293,12 @@ public static String toString(String path) throws IOException {
246
293
return toString (path , StandardCharsets .UTF_8 );
247
294
}
248
295
249
- public static String toString (String path , Charset charSet ) throws IOException {
250
- String str = fromResource (path , charSet );
296
+ public static String toString (String path , Charset charset ) throws IOException {
297
+ String str = fromResource (path , charset );
251
298
if (str != null ) {
252
299
return str ;
253
300
} else {
254
- str = fromFile (path , charSet );
301
+ str = fromFile (path , charset );
255
302
if (str != null ) {
256
303
return str ;
257
304
} else {
@@ -260,35 +307,58 @@ public static String toString(String path, Charset charSet) throws IOException {
260
307
}
261
308
}
262
309
263
- public static String fromResource (String path , Charset charSet ) {
264
- return fromInputStream (IOUtil .class .getResourceAsStream (path ), charSet );
310
+ public static String fromResource (String path , Charset charset ) {
311
+ return fromInputStream (IOUtil .class .getResourceAsStream (path ), charset );
265
312
}
266
313
267
314
public static String fromPath (Path path ) throws FileNotFoundException {
268
315
return fromFile (path .toFile (), StandardCharsets .UTF_8 );
269
316
}
270
317
271
- public static String fromPath (Path path , Charset charSet ) throws FileNotFoundException {
272
- return fromFile (path .toFile (), charSet );
318
+ public static String fromPath (Path path , Charset charset ) throws FileNotFoundException {
319
+ return fromFile (path .toFile (), charset );
273
320
}
274
321
275
- public static String fromFile (String path , Charset charSet ) throws FileNotFoundException {
276
- return fromFile (new File (path ), charSet );
322
+ public static String fromFile (String path , Charset charset ) throws FileNotFoundException {
323
+ return fromFile (new File (path ), charset );
277
324
}
278
325
279
- public static String fromFile (File file , Charset charSet ) throws FileNotFoundException {
326
+ public static String fromFile (File file , Charset charset ) throws FileNotFoundException {
280
327
if (file .length () == 0 ) {
281
328
return "" ;
282
329
}
283
- return fromInputStream (new FileInputStream (file ), charSet );
330
+ return fromInputStream (new FileInputStream (file ), charset );
284
331
}
285
332
286
- public static String fromUri (URI uri , Charset charSet ) throws IOException {
287
- return fromInputStream (uri .toURL ().openStream (), charSet );
333
+ /**
334
+ * Reads the location specified by a URI into a String. Checks to see if the URI is
335
+ * remote first, and throws IllegalArgumentException if it is.
336
+ * @param uri The URI to read.
337
+ * @param charset The charset to use.
338
+ * @return The String contents of the URI.
339
+ * @throws IOException If the URI failed to load or if it wasn't convertible to a URL.
340
+ */
341
+ public static String fromUri (URI uri , Charset charset ) throws IOException {
342
+ return fromUrl (uri .toURL (),charset );
343
+ }
344
+
345
+ /**
346
+ * Reads the location specified by a URL into a String. Checks to see if the URL is
347
+ * remote first, and throws IllegalArgumentException if it is.
348
+ * @param url The URL to read.
349
+ * @param charset The charset to use.
350
+ * @return The String contents of the URL.
351
+ * @throws IOException If the URL failed to load.
352
+ */
353
+ public static String fromUrl (URL url , Charset charset ) throws IOException {
354
+ if (isDisallowedProtocol (url )) {
355
+ throw new IllegalArgumentException ("Tried to read disallowed URL protocol: '" + url .toString () + "'" );
356
+ }
357
+ return fromInputStream (url .openStream (),charset );
288
358
}
289
359
290
- private static String fromInputStream (InputStream in , Charset charSet ) {
291
- try (Scanner scanner = new Scanner (new BufferedInputStream (in ,BUFFER_SIZE ),charSet .name ())) {
360
+ private static String fromInputStream (InputStream in , Charset charset ) {
361
+ try (Scanner scanner = new Scanner (new BufferedInputStream (in ,BUFFER_SIZE ),charset .name ())) {
292
362
return scanner .useDelimiter ("\\ Z" ).next ();
293
363
}
294
364
}
@@ -456,6 +526,10 @@ public static Iterator<Path> getPaths(List<String> fileNames, Path parentPath) {
456
526
public static InputStream getInputStreamForLocation (String location ) {
457
527
URL url = getURLForLocation (location );
458
528
if (url != null ) {
529
+ if (isDisallowedProtocol (url )) {
530
+ logger .severe ("Tried to open a disallowed URL protocol: " + url .toString ());
531
+ return null ;
532
+ }
459
533
try {
460
534
InputStream ret = url .openStream ();
461
535
@@ -544,6 +618,31 @@ public static URL getURLForLocation(String location) {
544
618
return ret ;
545
619
}
546
620
621
+ /**
622
+ * Checks the url to see if the protocol is disallowed.
623
+ * Disallowed protocols are http, https, ftp. Null
624
+ * URLs are also disallowed.
625
+ * @param url The URL to check.
626
+ * @return True if the protocol is disallowed, the URL is null, or the protocol is null.
627
+ */
628
+ public static boolean isDisallowedProtocol (URL url ) {
629
+ if (url == null ) {
630
+ return true ;
631
+ }
632
+ String protocol = url .getProtocol ();
633
+ if (protocol == null ) {
634
+ return true ;
635
+ }
636
+ switch (protocol ) {
637
+ case "http" :
638
+ case "https" :
639
+ case "ftp" :
640
+ return true ;
641
+ default :
642
+ return false ;
643
+ }
644
+ }
645
+
547
646
public static class NamesPathIterator implements Iterator <Path >{
548
647
549
648
private final Iterator <String > fileNames ;
0 commit comments