I am aware of the getResourceAsStream() method but there is an issue with the parser that reads the file, the whole structure was implemented to expect a FileInputStream() and the getResourceAsStream() returns an input stream which cannot be casted. Is there any easy "fix" for this situation?
A resource contained within a JAR file is not itself a file, and cannot be read using a FileInputStream. If you have code that absolutely requires a FileInputStream, then you'll need to extract the data using getResourceAsStream(), copy it into a temporary file, then pass a FileInputStream for that temporary file to your code.
Of course, in future, never write code to expect concrete implementations of things like InputStream, you'll always regret it.
don't believe your parse only works on FileInputStream but not InputStream
if that is the real case, and you must use that parser
2 options
use adapter pattern to create a CustomFileInputStream and overwrite the respective methods, more of redirect the getResourceAsStream data to the CustomFileInputStream
save ur getResourceAsStream into a temp file, and parse the temp file, then delete the file when done
I recently encountered the same issue. A third-party library we use reads from FileInputStream but the resources can be anywhere, in a JAR or remote. We used to write to temporary files but that has too much overhead.
A better solution is write a FileInputStream which wraps InputStream. Here is the class we use,
public class VirtualFileInputStream extends FileInputStream {
private InputStream stream;
public VirtualFileInputStream(InputStream stream) {
super(FileDescriptor.in); // This will never be used
this.stream = stream;
}
public int available() throws IOException {
throw new IllegalStateException("Unimplemented method called");
}
public void close() throws IOException {
stream.close();
}
public boolean equals(Object obj) {
return stream.equals(obj);
}
public FileChannel getChannel() {
throw new IllegalStateException("Unimplemented method called");
}
public int hashCode() {
return stream.hashCode();
}
public void mark(int readlimit) {
stream.mark(readlimit);
}
public boolean markSupported() {
return stream.markSupported();
}
public int read() throws IOException {
return stream.read();
}
public int read(byte[] b, int off, int len) throws IOException {
return stream.read(b, off, len);
}
public int read(byte[] b) throws IOException {
return stream.read(b);
}
public void reset() throws IOException {
stream.reset();
}
public long skip(long n) throws IOException {
return stream.skip(n);
}
public String toString() {
return stream.toString();
}
}