@Override public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { Path r = unroot(path); FileSystemProvider p = provider(r); return p.newByteChannel(r, options, attrs); }
@Override public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { Path r = unroot(path); FileSystemProvider p = provider(r); return p.newByteChannel(r, options, attrs); }
@Override public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { Path r = unroot(path); FileSystemProvider p = provider(r); return p.newByteChannel(r, options, attrs); }
@Test @Category( Writable.class ) public void testWriteNonExistent() throws IOException { assertThatThrownBy( () -> { try( SeekableByteChannel ch = FS.provider().newByteChannel( absTA(), singleton( WRITE ) ) ) { } } ). isInstanceOf( NoSuchFileException.class ); }
@Test @Category( Writable.class ) public void testWriteBeyondFileSize() throws IOException { Path file = fileTA(); try( SeekableByteChannel out = FS.provider().newByteChannel( file, singleton( WRITE ) ) ) { out.position( 100 ); out.write( ByteBuffer.wrap( "ha".getBytes( "UTF-8" ) ) ); } assertThat( Files.size( file ) ).isEqualTo( 102L ); }
@Test @Category( Writable.class ) public void testOverwriteTruncateExisting() throws IOException { Path there = fileTA(); try( SeekableByteChannel ch = FS.provider().newByteChannel( there, asSet( WRITE, TRUNCATE_EXISTING ) ) ) { } assertThat( there ).exists(); assertThat( Files.size( there ) ).isEqualTo( 0L ); }
@Test public void testWriteToReadOnlyChannelThrows() throws IOException { Path file = getFile(); try( SeekableByteChannel channel = FS.provider().newByteChannel( file, asSet( READ ) ) ) { assertThatThrownBy( () -> channel.write( ByteBuffer.allocate( 2 ) ) ).isInstanceOf( NonWritableChannelException.class ); } }
@Test @Category( Writable.class ) public void testWriteOnlyNewIfCreateIsThereToo() throws IOException { Path there = fileTA(); assertThatThrownBy( () -> { try( SeekableByteChannel ch = FS.provider().newByteChannel( there, asSet( WRITE, CREATE_NEW, CREATE ) ) ) { } } ).isInstanceOf( FileAlreadyExistsException.class ); }
@Test @Category( Writable.class ) public void testAppendAndReadThrows() throws IOException { assertThatThrownBy( () -> { try( SeekableByteChannel ch = FS.provider().newByteChannel( fileTA(), asSet( APPEND, READ ) ) ) { } } ). isInstanceOf( IllegalArgumentException.class ); }
@Test @Category( Writable.class ) public void testAppendAndTruncateExistingThrows() throws IOException { assertThatThrownBy( () -> { try( SeekableByteChannel ch = FS.provider().newByteChannel( fileTA(), asSet( APPEND, TRUNCATE_EXISTING ) ) ) { } } ). isInstanceOf( IllegalArgumentException.class ); }
@Test @Category( Writable.class ) public void testWriteChannelOfDir() throws IOException { assertThatThrownBy( () -> { try( SeekableByteChannel channel = FS.provider().newByteChannel( dirTA(), asSet( WRITE ) ) ) { } } ).isInstanceOf( FileSystemException.class ); }
@Test @Category( Windows.class ) public void testReadChannelOfDirThrows() throws IOException { assertThatThrownBy( () -> { try( SeekableByteChannel channel = FS.provider().newByteChannel( getNonEmptyDir(), asSet( READ ) ) ) { } } ).isInstanceOf( FileSystemException.class ); }
@Test @Category( Writable.class ) public void testTruncate() throws Exception { Path file = fileTA(); try( SeekableByteChannel channel = FS.provider().newByteChannel( file, asSet( WRITE ) ) ) { channel.truncate( 2 ); } assertThat( Files.size( file ) ).isEqualTo( 2L ); }
@Test @Category( { SlowTest.class, Writable.class, Attributes.class } ) public void testOverwriteDoesNotSetLastAccessTimeOfParent() throws IOException, InterruptedException { Path there = fileTA(); FileTime before = Files.readAttributes( childGetParent( there ), BasicFileAttributes.class ).lastAccessTime(); waitForAttribute(); Set<StandardOpenOption> options = asSet( WRITE, TRUNCATE_EXISTING ); try( SeekableByteChannel ch = FS.provider().newByteChannel( there, options ) ) { } assertThat( Files.readAttributes( there.getParent(), BasicFileAttributes.class ).lastAccessTime() ).isEqualTo( before ); }
@Test @Category( Writable.class ) public void testTruncateOnAppendChannelThrows() throws Exception { Path file = fileTA(); try( SeekableByteChannel channel = FS.provider().newByteChannel( file, asSet( APPEND ) ) ) { assertThatThrownBy( () -> channel.truncate( 2 )).isInstanceOf( FileSystemException.class ); } }
@Test @Category( Writable.class ) public void testTruncateOnReadChannelThrows() throws Exception { Path file = fileTA(); try( SeekableByteChannel channel = FS.provider().newByteChannel( file, asSet( READ ) ) ) { assertThatThrownBy( () -> channel.truncate( 2 ) ).isInstanceOf( NonWritableChannelException.class ); } }
@Test @Category( Writable.class ) public void testTruncateOnClosedChannelThrows() throws Exception { Path file = fileTA(); try( SeekableByteChannel channel = FS.provider().newByteChannel( file, asSet( WRITE ) ) ) { channel.close(); assertThatThrownBy( () -> channel.truncate( 2 ) ).isInstanceOf( ClosedChannelException.class ); } }
@Test @Category( Writable.class ) public void testWriteAndCreateExistent() throws IOException { Path there = fileTA(); try( SeekableByteChannel ch = FS.provider().newByteChannel( there, asSet( WRITE, CREATE ) ) ) { } assertThat( there ).exists(); }
@Test @Category( Writable.class ) public void testWriteAndCreateNewNonExistent() throws IOException { Path notthere = absTA(); try( SeekableByteChannel ch = FS.provider().newByteChannel( notthere, asSet( WRITE, CREATE_NEW ) ) ) { } assertThat( notthere ).exists(); }
@Test @Category( Writable.class ) public void testWriteAndCreateNonExistentCreatesIt() throws IOException { try( SeekableByteChannel ch = FS.provider().newByteChannel( absTA(), asSet( WRITE, CREATE ) ) ) { } assertThat( absTA() ).exists(); }