-
Notifications
You must be signed in to change notification settings - Fork 357
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
Create delegate on InflaterInputStream and DeflaterOutputStream #5744
Open
ZongoForSpeed
wants to merge
1
commit into
eclipse-ee4j:2.x
Choose a base branch
from
ZongoForSpeed:2.x
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to make those classes
package-privateprivate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't make this class package-private and keep it in the internal package.
Do you want me to move those classes in the package 'org.glassfish.jersey.message' or to create inner classes ?