Skip to content
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

Fix false difference detected with CachingOutputStream/CachingWriter when streams are flushed #252

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private void flushBuffer( ByteBuffer writeBuffer ) throws IOException
{
readBuffer = this.readBuffer;
( ( Buffer ) readBuffer ).clear();
readBuffer.limit( len );
}
else
{
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/codehaus/plexus/util/io/CachingWriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -63,6 +64,32 @@ private void waitLastModified() throws IOException, InterruptedException
}
}

@Test
public void testNoOverwriteWithFlush() throws IOException, InterruptedException
{
String data = "Hello world!";
Path path = tempDir.resolve("file-bigger.txt");
assertFalse( Files.exists(path));

try (Writer w = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
for (int i = 0; i < 10; i++) {
w.write(data);
}
}
FileTime modified = Files.getLastModifiedTime(path);

waitLastModified();

try (Writer w = new CachingWriter(path, StandardCharsets.UTF_8)) {
for (int i = 0; i < 10; i++) {
w.write(data);
w.flush();
}
}
FileTime newModified = Files.getLastModifiedTime(path);
assertEquals(modified, newModified);
}

@Test
public void testWriteNoExistingFile() throws IOException, InterruptedException
{
Expand Down