-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create delegate on InflaterInputStream and DeflaterOutputStream to ca…
…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
1 parent
ff5f5a8
commit 8be1f9e
Showing
3 changed files
with
183 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...mmon/src/main/java/org/glassfish/jersey/message/internal/ClosingDeflaterOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
96 changes: 96 additions & 0 deletions
96
...ommon/src/main/java/org/glassfish/jersey/message/internal/ClosingInflaterInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |