Skip to content

Commit

Permalink
Make FilteringOutputStream FlushedClosable by default
Browse files Browse the repository at this point in the history
Signed-off-by: jansupol <[email protected]>
  • Loading branch information
jansupol committed Dec 4, 2024
1 parent b481675 commit 2da4412
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.glassfish.jersey.io.spi;

import java.io.Closeable;
import java.io.FilterOutputStream;
import java.io.Flushable;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -27,8 +28,8 @@
* That way, {@link #flush()} method is not called twice.
*
* <p>
* Usable by {@link javax.ws.rs.client.ClientRequestContext#setEntityStream(OutputStream)}.
* Usable by {@link javax.ws.rs.container.ContainerResponseContext#setEntityStream(OutputStream)}.
* Usable by {@link jakarta.ws.rs.client.ClientRequestContext#setEntityStream(OutputStream)}.
* Usable by {@link jakarta.ws.rs.container.ContainerResponseContext#setEntityStream(OutputStream)}.
* </p>
*
* <p>
Expand All @@ -52,4 +53,13 @@ public interface FlushedCloseable extends Flushable, Closeable {
* @throws IOException if an I/O error occurs
*/
public void close() throws IOException;

/**
* Determine if the stream {@link OutputStream#flush() flushes} on {@link OutputStream#close()}.
* @param stream the provided {@link OutputStream}
* @return {@code true} if the stream ensures to call {@link OutputStream#flush()} on {@link OutputStream#close()}.
*/
public static boolean flushOnClose(OutputStream stream) {
return FilterOutputStream.class.isInstance(stream) || FlushedCloseable.class.isInstance(stream);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void setStreamProvider(OutboundMessageContext.StreamProvider streamProvid
}

/* package */ void flushOnClose() throws IOException {
if (!FlushedCloseable.class.isInstance(adaptedOutput)) {
if (!FlushedCloseable.flushOnClose(adaptedOutput)) {
flush();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ public void close() {
if (hasEntity()) {
try {
final OutputStream es = getEntityStream();
if (!FlushedCloseable.class.isInstance(es)) {
if (!FlushedCloseable.flushOnClose(es)) {
if (CommittingOutputStream.class.isInstance(es)) {
((CommittingOutputStream) es).flushOnClose();
} else {
Expand Down

0 comments on commit 2da4412

Please sign in to comment.