Skip to content

Commit

Permalink
Create delegate on InflaterInputStream and DeflaterOutputStream to ca…
Browse files Browse the repository at this point in the history
…ll end on Inflater and Deflater

Signed-off-by: Tanguy Lambert <[email protected]>

Add missing import

Add missing Copyright comments

Signed-off-by: Tanguy LAMBERT <[email protected]>
  • Loading branch information
ZongoForSpeed committed Oct 20, 2024
1 parent ff5f5a8 commit 8be1f9e
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

import javax.inject.Inject;

import org.glassfish.jersey.message.internal.ClosingDeflaterOutputStream;
import org.glassfish.jersey.message.internal.ClosingInflaterInputStream;
import org.glassfish.jersey.spi.ContentEncoder;

/**
Expand Down Expand Up @@ -77,7 +79,7 @@ public InputStream decode(String contentEncoding, InputStream encodedStream)
return new InflaterInputStream(markSupportingStream);
} else {
// no zlib wrapper
return new InflaterInputStream(markSupportingStream, new Inflater(true));
return new ClosingInflaterInputStream(markSupportingStream, true);
}
}

Expand All @@ -98,7 +100,7 @@ public OutputStream encode(String contentEncoding, OutputStream entityStream)
}

return deflateWithoutZLib
? new DeflaterOutputStream(entityStream, new Deflater(Deflater.DEFAULT_COMPRESSION, true))
? new ClosingDeflaterOutputStream(entityStream, Deflater.DEFAULT_COMPRESSION, true)
: new DeflaterOutputStream(entityStream);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.message.internal;

import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;

/**
* After Java 9, in the class Deflater the method finalize doesn't call end anymore
* The method end must be called explicitly. But when using the constructor
* DeflaterOutputStream(OutputStream out, Deflater def), the Deflater.end() method will
* not be called when closing the stream.
* This lead to memory leaks in the off-heap.
*
* @see java.util.zip.Deflater
* @see java.util.zip.DeflaterOutputStream
*/
public final class ClosingDeflaterOutputStream extends OutputStream {

private final Deflater deflater;

private final DeflaterOutputStream delegate;

private boolean closed = false;

public ClosingDeflaterOutputStream(OutputStream out, int level, boolean nowrap) {
deflater = new Deflater(level, nowrap);
delegate = new DeflaterOutputStream(out, deflater);
}

@Override
public void write(int b) throws IOException {
delegate.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
delegate.write(b, off, len);
}

public void finish() throws IOException {
delegate.finish();
}

@Override
public void close() throws IOException {
if (!closed) {
try {
delegate.close();
} finally {
deflater.end();
}
closed = true;
}
}

@Override
public void flush() throws IOException {
delegate.flush();
}

@Override
public void write(byte[] b) throws IOException {
delegate.write(b);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.message.internal;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;

/**
* After Java 9, in the class Inflater the method finalize doesn't call end anymore
* The method end must be called explicitly. But when using the constructor
* InflaterInputStream(InputStream out, Inflater def), the Inflater.end() method will
* not be called when closing the stream.
* This lead to memory leaks in the off-heap.
*
* @see java.util.zip.Inflater
* @see java.util.zip.InflaterInputStream
*/
public class ClosingInflaterInputStream extends InputStream {

private final Inflater inflater;

private final InflaterInputStream delegate;

private boolean closed = false;

public ClosingInflaterInputStream(InputStream inputStream, boolean nowrap) {
inflater = new Inflater(nowrap);
delegate = new InflaterInputStream(inputStream, inflater);
}

@Override
public int read() throws IOException {
return delegate.read();
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
return delegate.read(b, off, len);
}

@Override
public int available() throws IOException {
return delegate.available();
}

@Override
public long skip(long n) throws IOException {
return delegate.skip(n);
}

@Override
public void close() throws IOException {
if (!closed) {
inflater.end();
delegate.close();
closed = true;
}
}

@Override
public boolean markSupported() {
return delegate.markSupported();
}

@Override
public void mark(int readlimit) {
delegate.mark(readlimit);
}

@Override
public void reset() throws IOException {
delegate.reset();
}

@Override
public int read(byte[] b) throws IOException {
return delegate.read(b);
}

}

0 comments on commit 8be1f9e

Please sign in to comment.