I've built a JAR file for my Javalin application, and the code runs just fine. However, reading some resource files from the JAR fails with inputStream.available() == 0
, but it works just right for some other files.
The following files should be delivered just right:
a/
|\
| +- a.txt
| +- b.js
| +- c.css
|
b/
\
+- d.png
+- e.txt
However, the InputStream
only reads the files a/a.txt
, a/b.js
and b/e.txt
. For all other files it returns nothing, and available() == 0
, but it works when I'm not reading the files out of a JAR but from the extracted Classpath (and I'm using a ClassLoader
no matter what the execution environment looks like). Also, the file size does not matter, a/a.txt
is way larger than a/c.css
, so I'm out of clues on this end.
Some example code (as I said, I'm using Javalin for the HTTP request/response, which is being handled in the ctx
object, and I am also using Apache Tika to detect the MIME-Type of the files requested, which works as expected):
// Example, real path is (correctly) fetched from the Context (ctx) object
String path = "a/c.css";
ClassLoader classLoader = Thread.currentThread ().getContextClassLoader ();
InputStream inputStream = classLoader.getResourceAsStream (path);
String contentType = tika.detect (inputStream, path);
ctx.header ("Content-Type", contentType);
if (contentType.contains ("text") || contentType.contains ("script")) {
InputStreamReader streamReader = new InputStreamReader (inputStream);
BufferedReader reader = new BufferedReader (streamReader);
String line;
StringBuilder builder = new StringBuilder ();
while ((line = reader.readLine ()) != null) {
builder.append (line).append ("\n");
}
String result = builder.toString ();
ctx.header ("Content-Length", String.valueOf (result.length ()));
ctx.result (result);
reader.close ();
} else {
ctx.header ("Content-Length", String.valueOf (inputStream.available ()));
ctx.result (inputStream);
}
Am I missing something here or am I doing something wrong?