Skip to content

Commit

Permalink
Fix false difference detected with CachingOutputStream/CachingWriter …
Browse files Browse the repository at this point in the history
…when streams are flushed (#252)
  • Loading branch information
gnodet authored May 22, 2023
1 parent b4ee91f commit 3bd741d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
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

0 comments on commit 3bd741d

Please sign in to comment.