Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleWorkerContext.fromClassPath results in "Stream closed" IOException #6

Open
marc-guenther opened this issue Feb 22, 2021 · 0 comments

Comments

@marc-guenther
Copy link

I try to create a SimpleWorkerContext given a zip file on the classpath:

SimpleWorkerContext.fromClassPath(CORE_R4_FHIR_RESOURCES_FILENAME);

This results in a IOException: Stream closed.

The reason is, my zip file (the Core R4 Profile) contains Json files, and the JsonParser closes the stream after parsing each zip entry, although more entries are available on the zip stream.

Explanation:

This method is looping through the entries in the zip file (which is perfectly fine):
SimpleWorkerContext:

private void loadFromStream(InputStream stream, IContextResourceLoader loader) throws IOException, FHIRException {
  ZipInputStream zip = new ZipInputStream(stream);
  ZipEntry ze;
  while ((ze = zip.getNextEntry()) != null) {
      loadDefinitionItem(ze.getName(), zip, loader);
    zip.closeEntry();
  }
zip.close();
}

The loadDefinitions() calls the JsonParser, which in turn calls this:
TextFile:

  public static String streamToString(InputStream input) throws IOException  {
    InputStreamReader sr = new InputStreamReader(input, "UTF-8");    
    StringBuilder b = new StringBuilder();
    //while (sr.ready()) { Commented out by Claude Nanjo (1/14/2014) - sr.ready() always returns false - please remove if change does not impact other areas of codebase
    int i = -1;
    while((i = sr.read()) > -1) {
      char c = (char) i;
      b.append(c);
    }
    sr.close();
    
    return  b.toString().replace("\uFEFF", ""); 
  }

This closes the InputStream, which causes the entire zip stream to be closed, causing everything to fail.

Also, reading a file byte for byte seems horribly inefficient!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant