@Override public synchronized Path move( final Path source, final Path target, final CopyOption... options ) throws UnsupportedOperationException, FileAlreadyExistsException, DirectoryNotEmptyException, AtomicMoveNotSupportedException, IOException, SecurityException { if ( Files.exists( dot( source ) ) ) { Files.move( dot( source ), dot( target ), forceBuildOptions( options ) ); } else if ( Files.exists( dot( target ) ) ) { Files.delete( dot( target ) ); } final Path result = Files.move( source, target, options ); return result; }
@Override public synchronized Path copy( final Path source, final Path target, final CopyOption... options ) throws UnsupportedOperationException, FileAlreadyExistsException, DirectoryNotEmptyException, IOException, SecurityException { if ( Files.exists( dot( source ) ) ) { Files.copy( dot( source ), dot( target ), forceBuildOptions( options ) ); } else if ( Files.exists( dot( target ) ) ) { Files.delete( dot( target ) ); } final Path result = Files.copy( source, target, buildOptions( options ) ); return result; }
@Override public synchronized Path createDirectories( final Path dir, final FileAttribute<?>... attrs ) throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { final Path result = Files.createDirectories( dir, attrs ); buildDotFile( dir, newOutputStream( dot( dir ) ), attrs ); return result; }
@Override public synchronized boolean deleteIfExists( final Path path, final DeleteOption... options ) throws IllegalArgumentException, DirectoryNotEmptyException, IOException, SecurityException { final boolean result = Files.deleteIfExists( path, options ); try { Files.deleteIfExists( dot( path ), options ); } catch ( Exception ex ) { } if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().clear(); } return result; }
protected synchronized Path internalCreateDirectory( final Path dir, final boolean skipAlreadyExistsException, final FileAttribute<?>... attrs ) throws IllegalArgumentException, UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull( "dir", dir ); FileAttribute<?>[] allAttrs = attrs; try { Files.createDirectory( dir, attrs ); } catch ( final FileAlreadyExistsException ex ) { final Properties properties = new Properties(); if ( exists( dot( dir ) ) ) { properties.load( newInputStream( dot( dir ) ) ); } allAttrs = consolidate( properties, attrs ); if ( !skipAlreadyExistsException ) { throw ex; } } buildDotFile( dir, newOutputStream( dot( dir ) ), allAttrs ); return dir; }
@Override public synchronized SeekableByteChannel newByteChannel( final Path path, final Set<? extends OpenOption> options, final FileAttribute<?>... attrs ) throws IllegalArgumentException, UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException { checkNotNull( "path", path ); final Properties properties = new Properties(); if ( exists( dot( path ) ) ) { properties.load( newInputStream( dot( path ) ) ); } final FileAttribute<?>[] allAttrs = consolidate( properties, attrs ); final SeekableByteChannel result = Files.newByteChannel( path, buildOptions( options ), allAttrs ); if ( isFileScheme( path ) ) { buildDotFile( path, newOutputStream( dot( path ) ), allAttrs ); } return result; }
@Override public synchronized void delete( final Path path, final DeleteOption... options ) throws IllegalArgumentException, NoSuchFileException, DirectoryNotEmptyException, IOException, SecurityException { Files.delete( path, options ); try { Files.deleteIfExists( dot( path ), options ); } catch ( Exception ex ) { } if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().clear(); } }
protected void loadDotFile( final Path path ) { final Properties content = new Properties(); content.load( newInputStream( dot( path ) ) ); if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().loadContent( content ); } }
@Override public Map<String, Object> readAttributes( final Path path, final String attributes ) throws UnsupportedOperationException, NoSuchFileException, IllegalArgumentException, IOException, SecurityException { checkNotNull( "path", path ); checkNotEmpty( "attributes", attributes ); final Properties original = new Properties( Files.readAttributes( path, attributes ) ); if ( attributes.equals( "*" ) && exists( dot( path ) ) ) { boolean isAttrHolder = path instanceof AttrHolder; if ( isAttrHolder && ( (AttrHolder) path ).getAttrStorage().getContent().size() > 0 ) { return ( (AttrHolder) path ).getAttrStorage().getAllContent(); } final Properties content = new Properties(); content.load( newInputStream( dot( path ) ) ); content.putAll( original ); if ( isAttrHolder ) { ( (AttrHolder) path ).getAttrStorage().loadContent( content ); } return content; } return original; }
deleteIfExists( dot( path ) ); tempDot = File.createTempFile( "meta", "dot" ); buildDotFile( path, new FileOutputStream( tempDot ), attrs );
public static void buildDotFile( final Path path, final OutputStream out, final FileAttribute<?>... attrs ) { if ( attrs != null && attrs.length > 0 ) { final Properties properties = new Properties(); for ( final FileAttribute<?> attr : attrs ) { if ( attr.value() instanceof Serializable ) { properties.put( attr.name(), attr.value() ); } } try { properties.store( out ); } catch ( final Exception e ) { throw new IOException( e ); } if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().loadContent( properties ); } } else { path.getFileSystem().provider().deleteIfExists( dot( path ) ); } }
@Test public void testCopyFile() { final Path sfile = getFilePath(); final Path tfile = getTargetPath(); ioService().deleteIfExists( sfile ); ioService().deleteIfExists( tfile ); ioService().write( sfile, "wow", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() { @Override public String name() { return "custom"; } @Override public Object value() { return dateValue; } } ); assertTrue( ioService().exists( sfile ) ); assertTrue( ioService().exists( dot( sfile ) ) ); assertFalse( ioService().exists( tfile ) ); assertFalse( ioService().exists( dot( tfile ) ) ); ioService().copy( sfile, tfile ); assertTrue( ioService().exists( sfile ) ); assertTrue( ioService().exists( dot( tfile ) ) ); assertTrue( ioService().exists( tfile ) ); assertTrue( ioService().exists( dot( tfile ) ) ); }