1
1
package j2html .tags ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .FileReader ;
5
- import java .io . IOException ;
3
+ import java .io .FileInputStream ;
4
+ import java .io .InputStream ;
5
+ import java .util . Scanner ;
6
6
7
7
import j2html .Config ;
8
- import j2html .utils .CSSMin ;
9
- import j2html .utils .JSMin ;
10
8
11
9
import static j2html .TagCreator .*;
12
10
@@ -27,27 +25,19 @@ public static ContainerTag get(String path, TargetFormat format) {
27
25
28
26
public static String getFileAsString (String path ) {
29
27
try {
30
- return readFileAsString (InlineStaticResource .class .getResource (path ). getPath ( ));
31
- } catch (Exception e1 ) {
28
+ return streamToString (InlineStaticResource .class .getResourceAsStream (path ));
29
+ } catch (Exception expected ) { // we don't ask users to specify classpath or file-system
32
30
try {
33
- return readFileAsString ( path );
34
- } catch (Exception e2 ) {
31
+ return streamToString ( new FileInputStream ( path ) );
32
+ } catch (Exception exception ) {
35
33
throw new RuntimeException ("Couldn't find file with path='" + path + "'" );
36
34
}
37
35
}
38
36
}
39
37
40
- /**
41
- * @author kjheimark <3
42
- */
43
- private static String readFileAsString (String path ) throws IOException {
44
- BufferedReader bufferedReader = new BufferedReader (new FileReader (path ));
45
- StringBuilder sb = new StringBuilder ();
46
- int c ;
47
- while ((c = bufferedReader .read ()) >= 0 && c >= 0 ) {
48
- sb .append ((char ) c );
49
- }
50
- return sb .toString ();
38
+ private static String streamToString (InputStream inputStream ) {
39
+ Scanner s = new Scanner (inputStream ).useDelimiter ("\\ A" );
40
+ return s .hasNext () ? s .next () : "" ;
51
41
}
52
42
53
43
}
0 commit comments