From 2da4412bcc9fa8750780c4ca40b49c336bf1de61 Mon Sep 17 00:00:00 2001 From: jansupol Date: Wed, 4 Dec 2024 14:36:54 +0100 Subject: [PATCH] Make FilteringOutputStream FlushedClosable by default Signed-off-by: jansupol --- .../glassfish/jersey/io/spi/FlushedCloseable.java | 14 ++++++++++++-- .../message/internal/CommittingOutputStream.java | 2 +- .../message/internal/OutboundMessageContext.java | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/core-common/src/main/java/org/glassfish/jersey/io/spi/FlushedCloseable.java b/core-common/src/main/java/org/glassfish/jersey/io/spi/FlushedCloseable.java index 12aa7144d8..6b9f3009d3 100644 --- a/core-common/src/main/java/org/glassfish/jersey/io/spi/FlushedCloseable.java +++ b/core-common/src/main/java/org/glassfish/jersey/io/spi/FlushedCloseable.java @@ -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; @@ -27,8 +28,8 @@ * That way, {@link #flush()} method is not called twice. * *

- * 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)}. *

* *

@@ -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); + } } diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/CommittingOutputStream.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/CommittingOutputStream.java index 446dbcec13..f87c80da87 100644 --- a/core-common/src/main/java/org/glassfish/jersey/message/internal/CommittingOutputStream.java +++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/CommittingOutputStream.java @@ -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(); } } diff --git a/core-common/src/main/java/org/glassfish/jersey/message/internal/OutboundMessageContext.java b/core-common/src/main/java/org/glassfish/jersey/message/internal/OutboundMessageContext.java index d119ff56ca..db496827ba 100644 --- a/core-common/src/main/java/org/glassfish/jersey/message/internal/OutboundMessageContext.java +++ b/core-common/src/main/java/org/glassfish/jersey/message/internal/OutboundMessageContext.java @@ -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 {