Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012 JBoss Inc
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *       http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.uberfire.java.nio.fs.file;
 
 import java.io.File;
 import java.net.URI;
 import java.util.Map;
 import java.util.Set;
 
 import  org.uberfire.java.nio.IOException;
 import  org.uberfire.java.nio.base.BasicFileAttributesImpl;
 import  org.uberfire.java.nio.base.ExtendedAttributeView;
 import  org.uberfire.java.nio.base.GeneralPathImpl;
 import  org.uberfire.java.nio.base.SeekableByteChannelFileBasedImpl;
 import  org.uberfire.java.nio.channels.AsynchronousFileChannel;
 import  org.uberfire.java.nio.channels.SeekableByteChannel;
 import  org.uberfire.java.nio.file.AccessDeniedException;
 import  org.uberfire.java.nio.file.AccessMode;
 import  org.uberfire.java.nio.file.AtomicMoveNotSupportedException;
 import  org.uberfire.java.nio.file.CopyOption;
 import  org.uberfire.java.nio.file.DeleteOption;
 import  org.uberfire.java.nio.file.DirectoryNotEmptyException;
 import  org.uberfire.java.nio.file.DirectoryStream;
 import  org.uberfire.java.nio.file.FileAlreadyExistsException;
 import  org.uberfire.java.nio.file.FileStore;
 import  org.uberfire.java.nio.file.FileSystem;
 import  org.uberfire.java.nio.file.FileSystemAlreadyExistsException;
 import  org.uberfire.java.nio.file.FileSystemNotFoundException;
 import  org.uberfire.java.nio.file.LinkOption;
 import  org.uberfire.java.nio.file.NoSuchFileException;
 import  org.uberfire.java.nio.file.NotDirectoryException;
 import  org.uberfire.java.nio.file.NotLinkException;
 import  org.uberfire.java.nio.file.OpenOption;
 import  org.uberfire.java.nio.file.Path;
 import  org.uberfire.java.nio.file.StandardDeleteOption;
 import  org.uberfire.java.nio.file.attribute.BasicFileAttributeView;
 import  org.uberfire.java.nio.file.attribute.BasicFileAttributes;
 import  org.uberfire.java.nio.file.attribute.FileAttribute;
 import  org.uberfire.java.nio.file.attribute.FileAttributeView;
 import  org.uberfire.java.nio.file.spi.FileSystemProvider;
 
 import static org.uberfire.commons.validation.Preconditions.*;
 import static org.uberfire.java.nio.file.StandardOpenOption.*;
 
 public class SimpleFileSystemProvider implements FileSystemProvider {
 
     private static final String USER_DIR = "user.dir";
     private final BaseSimpleFileSystem fileSystem;
     private boolean isDefault;
     private final OSType osType;
     private final File[] roots;
 
     enum OSType {
         WINDOWS, UNIX_LIKE;
 
         public static OSType currentOS() {
             if ( System.getProperty"os.name" ).toLowerCase().indexOf"win" ) >= 0 ) {
                 return ;
             }
             return ;
         }
     }
 
     public SimpleFileSystemProvider() {
         this( File.listRoots(), OSType.currentOS() );
     }
 
     SimpleFileSystemProviderfinal File[] roots,
                               final OSType osType ) {
         final String defaultPath = System.getProperty );
         this. = checkNotNull"osType"osType );
         this. = checkNotNull"roots"roots );
         if ( osType == . ) {
             this. = new SimpleWindowsFileSystemthisdefaultPath );
        } else {
            this. = new SimpleUnixFileSystemthisdefaultPath );
        }
    }
    @Override
    public synchronized void forceAsDefault() {
        this. = true;
    }
    @Override
    public boolean isDefault() {
        return ;
    }
    @Override
    public String getScheme() {
        return "file";
    }
    @Override
    public FileSystem getFileSystemfinal URI uri )
            throws IllegalArgumentException, FileSystemNotFoundException, SecurityException {
        return getDefaultFileSystem();
    }
    @Override
    public Path getPathfinal URI uri )
            throws IllegalArgumentException, FileSystemNotFoundException, SecurityException {
        checkNotNull"uri"uri );
        checkCondition"uri scheme not supported"uri.getScheme().equalsgetScheme() ) || uri.getScheme().equals"default" ) );
        return getDefaultFileSystem().getPath( uri.getPath() );
    }
    @Override
    public FileSystem newFileSystemfinal URI uri,
                                     final Map<String, ?> env )
            throws IllegalArgumentException, IOException, SecurityException, FileSystemAlreadyExistsException {
        checkNotNull"uri"uri );
        checkNotNull"env"env );
        throw new FileSystemAlreadyExistsException();
    }
    @Override
    public FileSystem newFileSystemfinal Path path,
                                     final Map<String, ?> env )
            throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        checkNotNull( "path"path );
        checkNotNull"env"env );
        throw new FileSystemAlreadyExistsException();
    }
    @Override
    public InputStream newInputStreamfinal Path path,
                                       final OpenOption... options )
            throws IllegalArgumentException, NoSuchFileException, IOException, SecurityException {
        checkNotNull( "path"path );
        final File file = path.toFile();
        if ( !file.exists() ) {
            throw new NoSuchFileException( file.toString() );
        }
        try {
            return new FileInputStreampath.toFile() );
        } catch ( FileNotFoundException e ) {
            throw new NoSuchFileException( e.getMessage() );
        }
    }
    @Override
    public OutputStream newOutputStreamfinal Path path,
                                         final OpenOption... options )
            throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        checkNotNull( "path"path );
        try {
            return new FileOutputStreampath.toFile() );
        } catch ( Exception e ) {
            throw new IOException( e );
        }
    }
    @Override
    public FileChannel newFileChannelfinal Path path,
                                       final Set<? extends OpenOption> options,
                                       final FileAttribute<?>... attrs )
            throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        checkNotNull( "path"path );
        return ( (FileOutputStreamnewOutputStreampath ) ).getChannel();
    }
    @Override
    public AsynchronousFileChannel newAsynchronousFileChannelfinal Path path,
                                                               final Set<? extends OpenOption> options,
                                                               final ExecutorService executor,
                                                               FileAttribute<?>... attrs )
            throws IllegalArgumentExceptionUnsupportedOperationException, IOException, SecurityException {
        checkNotNull( "path"path );
        throw new UnsupportedOperationException();
    }
    @Override
    public SeekableByteChannel newByteChannelfinal Path path,
                                               final Set<? extends OpenOption> options,
                                               final FileAttribute<?>... attrs )
            throws IllegalArgumentExceptionUnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        final File file = checkNotNull( "path"path ).toFile();
        if ( file.exists() ) {
            if ( !shouldCreateOrOpenAByteChanneloptions ) ) {
                throw new FileAlreadyExistsException( path.toString() );
            }
        }
        try {
            if ( options != null && options.contains( READ ) ) {
                return openAByteChannelpath );
            } else {
                return createANewByteChannelfile );
            }
        } catch ( java.io.IOException e ) {
            throw new IOException( e );
        }
    }
    private SeekableByteChannelFileBasedImpl createANewByteChannelfinal File file ) throws FileNotFoundException {
        return new SeekableByteChannelFileBasedImpl( new RandomAccessFilefile"rw" ).getChannel() ) {
            @Override
            public void close() throws java.io.IOException {
                super.close();
            }
        };
    }
    private SeekableByteChannelFileBasedImpl openAByteChannel( Path path ) throws FileNotFoundException {
        return new SeekableByteChannelFileBasedImpl( new RandomAccessFilepath.toFile(), "r" ).getChannel() );
    }
    private boolean shouldCreateOrOpenAByteChannelfinal Set<? extends OpenOption> options ) {
        return ( options != null && ( options.contains( TRUNCATE_EXISTING ) || options.contains( READ ) ) );
    }
    @Override
    public void createDirectoryfinal Path dir,
                                 final FileAttribute<?>... attrs )
            throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        checkNotNull( "dir"dir );
        final Path realDir = dir.toAbsolutePath();
        if ( realDir.toFile().exists() ) {
            throw new FileAlreadyExistsException( dir.toString() );
        }
        realDir.toFile().mkdirs();
    }
    @Override
    public DirectoryStream<Path> newDirectoryStream( final Path dir,
                                                     final DirectoryStream.Filter<Path> filter )
            throws NotDirectoryException, IOException, SecurityException {
        checkNotNull( "filter"filter );
        final File file = checkNotNull( "dir"dir ).toFile();
        if ( !file.isDirectory() ) {
            throw new NotDirectoryException( dir.toString() );
        }
        final File[] content = file.listFiles();
        if ( content == null ) {
            throw new NotDirectoryException( dir.toString() );
        }
        return new DirectoryStream<Path>() {
            boolean isClosed = false;
            @Override
            public void close() throws IOException {
                if (  ) {
                    throw new IOException();
                }
                 = true;
            }
            @Override
            public Iterator<Path> iterator() {
                if (  ) {
                    throw new IOException();
                }
                return new Iterator<Path>() {
                    private int i = -1;
                    private Path nextEntry = null;
                    public boolean atEof = false;
                    @Override
                    public boolean hasNext() {
                        if (  == null && ! ) {
                             = readNextEntry();
                        }
                        return  != null;
                    }
                    @Override
                    public Path next() {
                        final Path result;
                        if (  == null && ! ) {
                            result = readNextEntry();
                        } else {
                            result = ;
                             = null;
                        }
                        if ( result == null ) {
                            throw new NoSuchElementException();
                        }
                        return result;
                    }
                    private Path readNextEntry() {
                        if (  ) {
                            return null;
                        }
                        Path result = null;
                        while ( true ) {
                            ++;
                            if (  >= content.length ) {
                                 = true;
                                break;
                            }
                            final Path path = GeneralPathImpl.newFromFile( getDefaultFileSystem(), content ] );
                            if ( filter.accept( path ) ) {
                                result = path;
                                break;
                            }
                        }
                        return result;
                    }
                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                };
            }
        };
    }
    @Override
    public void createSymbolicLinkfinal Path link,
                                    final Path target,
                                    final FileAttribute<?>... attrs )
            throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        checkNotNull( "link"link );
        checkNotNull( "target"target );
        checkCondition( "link and target can't be same", !link.equals( target ) );
        checkCondition( "target must already exists"target.toFile().exists() );
        if ( link.toFile().exists() ) {
            throw new FileAlreadyExistsException( link.toString() );
        }
        throw new UnsupportedOperationException();
    }
    @Override
    public void createLinkfinal Path link,
                            final Path existing )
            throws UnsupportedOperationException, FileAlreadyExistsException, IOException, SecurityException {
        checkNotNull( "link"link );
        checkNotNull( "existing"existing );
        checkCondition( "existing must already exists"existing.toFile().exists() );
        checkCondition( "link and target can't be same", !link.equals( existing ) );
        if ( link.toFile().exists() ) {
            throw new FileAlreadyExistsException( link.toString() );
        }
        throw new UnsupportedOperationException();
    }
    @Override
    public void deletefinal Path path,
                        final DeleteOption... options ) throws NoSuchFileException, DirectoryNotEmptyException, IOException, SecurityException {
        checkNotNull( "path"path );
        if ( !path.toFile().exists() ) {
            throw new NoSuchFileException( path.toString() );
        }
        deleteIfExistspathoptions );
    }
    @Override
    public boolean deleteIfExistsfinal Path path,
                                   final DeleteOption... options )
            throws DirectoryNotEmptyException, IOException, SecurityException {
        checkNotNull( "path"path );
        synchronized ( this ) {
            final File file = path.toFile();
            try {
                if ( file.isDirectory() && !deleteNonEmptyDirectoryoptions ) && file.list().length > 0 ) {
                    throw new DirectoryNotEmptyException( path.toString() );
                }
                final boolean result = file.exists();
                try {
                    FileUtils.forceDeletefile );
                } catch ( final FileNotFoundException ignore ) {
                } catch ( java.io.IOException e ) {
                    throw new IOException( e );
                }
                return result;
            } finally {
                toGeneralPathImplpath ).clearCache();
            }
        }
    }
    private boolean deleteNonEmptyDirectoryfinal DeleteOption... options ) {
        for ( final DeleteOption option : options ) {
            if ( option.equals( StandardDeleteOption.NON_EMPTY_DIRECTORIES ) ) {
                return true;
            }
        }
        return false;
    }
    @Override
    public Path readSymbolicLinkfinal Path link )
            throws UnsupportedOperationException, NotLinkException, IOException, SecurityException {
        checkNotNull( "link"link );
        if ( !link.toFile().exists() ) {
            throw new NotLinkException( link.toString() );
        }
        throw new UnsupportedOperationException();
    }
    @Override
    public boolean isSameFilefinal Path path,
                               final Path path2 )
            throws IOException, SecurityException {
        checkNotNull( "path"path );
        checkNotNull( "path2"path2 );
        return path.equals( path2 );
    }
    @Override
    public boolean isHiddenfinal Path path ) throws IllegalArgumentException, IOException, SecurityException {
        checkNotNull( "path"path );
        return path.toFile().isHidden();
    }
    @Override
    public void checkAccessfinal Path path,
                             AccessMode... modes )
            throws UnsupportedOperationException, NoSuchFileException, AccessDeniedException, IOException, SecurityException {
        checkNotNull( "path"path );
        checkNotNull( "modes"modes );
        if ( !path.toFile().exists() ) {
            throw new NoSuchFileException( path.toString() );
        }
        if ( path.toFile() != null ) {
            for ( final AccessMode mode : modes ) {
                checkNotNull( "mode"mode );
                switch ( mode ) {
                    case READ:
                        if ( !path.toFile().canRead() ) {
                            throw new AccessDeniedException( path.toString() );
                        }
                        break;
                    case EXECUTE:
                        if ( !path.toFile().canExecute() ) {
                            throw new AccessDeniedException( path.toString() );
                        }
                        break;
                    case WRITE:
                        if ( !path.toFile().canWrite() ) {
                            throw new AccessDeniedException( path.toString() );
                        }
                        break;
                }
            }
        }
    }
    @Override
    public FileStore getFileStorefinal Path path ) throws IOException, SecurityException {
        checkNotNull( "path"path );
        if (  == . ) {
            return new SimpleWindowsFileStorepath );
        }
        return new SimpleUnixFileStorepath );
    }
    @Override
    public <V extends FileAttributeView> V getFileAttributeViewfinal Path path,
                                                                 final Class<V> type,
                                                                 final LinkOption... options )
            throws NoSuchFileException {
        checkNotNull( "path"path );
        checkNotNull"type"type );
        if ( !path.toFile().exists() ) {
            throw new NoSuchFileException( path.toString() );
        }
        final GeneralPathImpl gPath = toGeneralPathImplpath );
        final V view = gPath.getAttrView( type );
        if ( view == null && type == BasicFileAttributeView.class || type == SimpleBasicFileAttributeView.class ) {
            final V newView = (V) new SimpleBasicFileAttributeViewgPath );
            gPath.addAttrView( newView );
            return newView;
        }
        return view;
    }
    private ExtendedAttributeView getFileAttributeViewfinal Path path,
                                                        final String name,
                                                        final LinkOption... options ) {
        final GeneralPathImpl gPath = toGeneralPathImplpath );
        final ExtendedAttributeView view = gPath.getAttrView( name );
        if ( view == null && name.equals"basic" ) ) {
            final SimpleBasicFileAttributeView newView = new SimpleBasicFileAttributeViewgPath );
            gPath.addAttrView( newView );
            return newView;
        }
        return view;
    }
    @Override
    public <A extends BasicFileAttributes> A readAttributesfinal Path path,
                                                             final Class<A> type,
                                                             final LinkOption... options )
            throws NoSuchFileException, UnsupportedOperationException, IOException, SecurityException {
        checkNotNull( "path"path );
        checkNotNull"type"type );
        if ( !path.toFile().exists() ) {
            throw new NoSuchFileException( path.toString() );
        }
        if ( type == BasicFileAttributesImpl.class || type == BasicFileAttributes.class ) {
            final SimpleBasicFileAttributeView view = getFileAttributeViewpathSimpleBasicFileAttributeView.classoptions );
            return (A) view.readAttributes();
        }
        return null;
    }
    @Override
    public Map<StringObjectreadAttributesfinal Path path,
                                               final String attributes,
                                               final LinkOption... options )
            throws UnsupportedOperationExceptionIllegalArgumentException, IOException, SecurityException {
        checkNotNull( "path"path );
        checkNotEmpty"attributes"attributes );
        final String[] s = splitattributes );
        if ( s[ 0 ].length() == 0 ) {
            throw new IllegalArgumentExceptionattributes );
        }
        final ExtendedAttributeView view = getFileAttributeViewpaths[ 0 ], options );
        if ( view == null ) {
            throw new UnsupportedOperationException"View '" + s[ 0 ] + "' not available" );
        }
        return view.readAttributes( s[ 1 ].split"," ) );
    }
    @Override
    public void setAttributefinal Path path,
                              final String attribute,
                              final Object value,
                              final LinkOption... options )
        checkNotNull( "path"path );
        checkNotEmpty"attributes"attribute );
        final String[] s = splitattribute );
        if ( s[ 0 ].length() == 0 ) {
            throw new IllegalArgumentExceptionattribute );
        }
        final ExtendedAttributeView view = getFileAttributeViewpaths[ 0 ], options );
        if ( view == null ) {
            throw new UnsupportedOperationException"View '" + s[ 0 ] + "' not available" );
        }
        view.setAttribute( attributevalue );
    }
    @Override
    public void copyfinal Path source,
                      final Path target,
                      final CopyOption... options )
            throws UnsupportedOperationException, FileAlreadyExistsException, DirectoryNotEmptyException, IOException, SecurityException {
        checkNotNull( "source"source );
        checkNotNull( "target"target );
        checkCondition( "source must exist"source.toFile().exists() );
        if ( target.toFile().exists() ) {
            throw new FileAlreadyExistsException( target.toString() );
        }
        if ( source.toFile().isDirectory() && source.toFile().list().length > 0 ) {
            throw new DirectoryNotEmptyException( source.toString() );
        }
        try {
            if ( source.toFile().isDirectory() ) {
                FileUtils.copyDirectorysource.toFile(), target.toFile() );
            } else {
                FileUtils.copyFilesource.toFile(), target.toFile() );
            }
        } catch ( java.io.IOException ex ) {
            throw new IOException( ex );
        }
    }
    @Override
    public void movefinal Path source,
                      final Path target,
                      final CopyOption... options )
            throws DirectoryNotEmptyException, AtomicMoveNotSupportedException, IOException, SecurityException {
        checkNotNull( "source"source );
        checkNotNull( "target"target );
        checkCondition( "source must exist"source.toFile().exists() );
        if ( target.toFile().exists() ) {
            throw new FileAlreadyExistsException( target.toString() );
        }
        if ( source.toFile().isDirectory() && source.toFile().list().length > 0 ) {
            throw new DirectoryNotEmptyException( source.toString() );
        }
        try {
            if ( source.toFile().isDirectory() ) {
                FileUtils.moveDirectorysource.toFile(), target.toFile() );
            } else {
                FileUtils.moveFilesource.toFile(), target.toFile() );
            }
        } catch ( java.io.IOException ex ) {
            throw new IOException( ex );
        }
    }
    private FileSystem getDefaultFileSystem() {
        return ;
    }
    private GeneralPathImpl toGeneralPathImplfinal Path path ) {
        if ( path instanceof GeneralPathImpl ) {
            return (GeneralPathImpl) path;
        }
        return GeneralPathImpl.create( path.toString(), false );
    }
    private String[] splitfinal String attribute ) {
        final String[] s = new String[ 2 ];
        final int pos = attribute.indexOf':' );
        if ( pos == -1 ) {
            s[ 0 ] = "basic";
            s[ 1 ] = attribute;
        } else {
            s[ 0 ] = attribute.substring( 0, pos );
            s[ 1 ] = ( pos == attribute.length() ) ? "" : attribute.substringpos + 1 );
        }
        return s;
    }
New to GrepCode? Check out our FAQ X