Skip to content
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 @@ -153,19 +153,17 @@ private void flushBuffer( ByteBuffer writeBuffer ) throws IOException
@Override
public void close() throws IOException
{
flush();
long position = channel.position();
if ( position != channel.size() )
if ( channel.isOpen() )
{
if ( !modified )
flush();
long position = channel.position();
if ( position != channel.size() )
{
FileTime now = FileTime.from( Instant.now() );
Files.setLastModifiedTime( path, now );
modified = true;
channel.truncate( position );
}
channel.truncate( position );
channel.close();
}
channel.close();
}

public boolean isModified()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -39,7 +38,6 @@ public class CachingOutputStreamTest

Path tempDir;
Path checkLastModified;
FileTime lm;

@Before
public void setup() throws IOException
Expand All @@ -48,19 +46,18 @@ public void setup() throws IOException
Files.createDirectories( dir );
tempDir = Files.createTempDirectory( dir, "temp-" );
checkLastModified = tempDir.resolve( ".check" );
Files.newOutputStream( checkLastModified ).close();
lm = Files.getLastModifiedTime( checkLastModified );
}

private void waitLastModified() throws IOException, InterruptedException
{
Files.newOutputStream( checkLastModified ).close();
FileTime lm = Files.getLastModifiedTime( checkLastModified );
while ( true )
{
Files.newOutputStream( checkLastModified ).close();
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
if ( !Objects.equals( nlm, lm ) )
{
lm = nlm;
break;
}
Thread.sleep( 10 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
Expand All @@ -38,7 +37,6 @@ public class CachingWriterTest

Path tempDir;
Path checkLastModified;
FileTime lm;

@Before
public void setup() throws IOException
Expand All @@ -47,19 +45,18 @@ public void setup() throws IOException
Files.createDirectories( dir );
tempDir = Files.createTempDirectory( dir, "temp-" );
checkLastModified = tempDir.resolve( ".check" );
Files.newOutputStream( checkLastModified ).close();
lm = Files.getLastModifiedTime( checkLastModified );
}

private void waitLastModified() throws IOException, InterruptedException
{
Files.newOutputStream( checkLastModified ).close();
FileTime lm = Files.getLastModifiedTime( checkLastModified );
while ( true )
{
Files.newOutputStream( checkLastModified ).close();
FileTime nlm = Files.getLastModifiedTime( checkLastModified );
if ( !Objects.equals( nlm, lm ) )
{
lm = nlm;
break;
}
Thread.sleep( 10 );
Expand Down