Skip to content

Double closing of InputStream in avaje-jex-static-content v3.0 #250

Closed
@dmitriy-bodroff

Description

@dmitriy-bodroff

The InputStream was closed twice, causing some following errors.

io/avaje/jex/staticcontent/StaticClassResourceHandler.java

private void sendFile(Context ctx, HttpExchange jdkExchange, String urlPath, File canonicalFile)
    throws IOException {
  try (var fis = new FileInputStream(canonicalFile)) { // The second closing of InputStream (fis)
    String mimeType = lookupMime(urlPath);
    ctx.header(CONTENT_TYPE, mimeType);
    ctx.headers(headers);
    if (precompress) {
      addCachedEntry(ctx, urlPath, fis);
      return;
    }
    ctx.write(fis); // Inside the first closing of InputStream (fis)
  } catch (FileNotFoundException e) {
    throw404(jdkExchange);
  }
}

io/avaje/jex/staticcontent/StaticFileHandler.java

private void sendFile(Context ctx, HttpExchange jdkExchange, String urlPath, File canonicalFile)
    throws IOException {
  try (var fis = new FileInputStream(canonicalFile)) {  // The second closing of InputStream (fis)
    String mimeType = lookupMime(urlPath);
    ctx.header(CONTENT_TYPE, mimeType);
    ctx.headers(headers);
    if (precompress) {
      addCachedEntry(ctx, urlPath, fis);
      return;
    }
    ctx.write(fis); // Inside the first closing of InputStream (fis)
  } catch (FileNotFoundException e) {
    throw404(jdkExchange);
  }
}

io/avaje/jex/core/JdkContext.java

@Override
public void write(InputStream is) {
  try (is; var os = outputStream()) { // The first closing of InputStream (is)
    is.transferTo(os);
  } catch (IOException e) {
    throw new UncheckedIOException(e);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions