Skip to content

Commit

Permalink
NullPointerException: "Deflater has been closed" emanating from `Fi…
Browse files Browse the repository at this point in the history
…lterServletOutputStream` (#553)
  • Loading branch information
basil authored May 28, 2024
1 parent c83d511 commit e203fa6
Showing 1 changed file with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ public class FilterServletOutputStream extends ServletOutputStream {
private final OutputStream out;
private final ServletOutputStream realSream;

/**
* Whether the stream is closed; implicitly initialized to false.
*/
private volatile boolean closed;

/**
* Object used to prevent a race on the 'closed' instance variable.
*/
private final Object closeLock = new Object();

/**
* Constructs a new {@link FilterOutputStream}.
* @param out the stream that sits above the realStream, performing some filtering. This must be eventually delegating eventual writes to {@code realStream}.
Expand Down Expand Up @@ -42,7 +52,36 @@ public void write(byte[] b, int off, int len) throws IOException {

@Override
public void close() throws IOException {
out.close();
if (closed) {
return;
}
synchronized (closeLock) {
if (closed) {
return;
}
closed = true;
}

Throwable flushException = null;
try {
flush();
} catch (Throwable e) {
flushException = e;
throw e;
} finally {
if (flushException == null) {
out.close();
} else {
try {
out.close();
} catch (Throwable closeException) {
if (flushException != closeException) {
closeException.addSuppressed(flushException);
}
throw closeException;
}
}
}
}

@Override
Expand Down

0 comments on commit e203fa6

Please sign in to comment.