This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/CachedWriter in repository https://gitbox.apache.org/repos/asf/cxf.git
commit b3649fcb0b9dc6b13e4b3ace3df416b6ebc68d64 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Nov 25 14:50:49 2026 +0000 Reset CachedWriter.cosClosed when opening a new temp file writer --- .../main/java/org/apache/cxf/io/CachedWriter.java | 2 ++ .../java/org/apache/cxf/io/CachedWriterTest.java | 27 +++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/cxf/io/CachedWriter.java b/core/src/main/java/org/apache/cxf/io/CachedWriter.java index 01785631def..1aabf45a854 100644 --- a/core/src/main/java/org/apache/cxf/io/CachedWriter.java +++ b/core/src/main/java/org/apache/cxf/io/CachedWriter.java @@ -611,6 +611,8 @@ public class CachedWriter extends Writer { } private OutputStreamWriter createOutputStreamWriter(File file) throws IOException { + // a new file writer is being opened; the previous one (if any) may have been closed + cosClosed = false; OutputStream out = new BufferedOutputStream(Files.newOutputStream(file.toPath())); if (cipherTransformation != null) { try { diff --git a/core/src/test/java/org/apache/cxf/io/CachedWriterTest.java b/core/src/test/java/org/apache/cxf/io/CachedWriterTest.java index e15b78da2f4..8ffcda03e3e 100644 --- a/core/src/test/java/org/apache/cxf/io/CachedWriterTest.java +++ b/core/src/test/java/org/apache/cxf/io/CachedWriterTest.java @@ -23,6 +23,13 @@ import java.io.IOException; import java.io.Reader; import java.io.StringWriter; +import org.apache.cxf.helpers.IOUtils; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + public class CachedWriterTest extends CachedStreamTestBase { @Override protected void reloadDefaultProperties() { @@ -81,6 +88,24 @@ public class CachedWriterTest extends CachedStreamTestBase { protected String readPartiallyFromStreamObject(Object cache, int len) throws IOException { return readPartiallyFromReader((Reader)cache, len); } -} + @Test + public void testSpillToFileAgainAfterResetOut() throws IOException { + CachedWriter cw = new CachedWriter(); + cw.setThreshold(16); + String first = "first-content-that-exceeds-the-threshold"; + cw.write(first); + assertNotNull("first write should have spilled to a temp file", cw.getTempFile()); + // closes the file writer and moves the content back in memory + cw.resetOut(null, true); + + String second = "second-content-that-exceeds-the-threshold"; + cw.write(second); + assertNotNull("second write should have spilled to a temp file", cw.getTempFile()); + try (Reader r = cw.getReader()) { + assertEquals(first + second, IOUtils.toString(r)); + } + cw.close(); + } +}
