Skip to content

Commit 3682413

Browse files
committed
Allow reading static resources from jars
1 parent 4288924 commit 3682413

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/main/java/j2html/tags/InlineStaticResource.java

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package j2html.tags;
22

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;
66

77
import j2html.Config;
8-
import j2html.utils.CSSMin;
9-
import j2html.utils.JSMin;
108

119
import static j2html.TagCreator.*;
1210

@@ -27,27 +25,19 @@ public static ContainerTag get(String path, TargetFormat format) {
2725

2826
public static String getFileAsString(String path) {
2927
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
3230
try {
33-
return readFileAsString(path);
34-
} catch (Exception e2) {
31+
return streamToString(new FileInputStream(path));
32+
} catch (Exception exception) {
3533
throw new RuntimeException("Couldn't find file with path='" + path + "'");
3634
}
3735
}
3836
}
3937

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() : "";
5141
}
5242

5343
}

0 commit comments

Comments
 (0)