Search in sources :

Example 11 with FileSystemException

use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.

the class FileSystemImpl method createFileInternal.

protected BlockingAction<Void> createFileInternal(String p, String perms, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(p);
    FileAttribute<?> attrs = perms == null ? null : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms));
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path target = vertx.resolveFile(p).toPath();
                if (attrs != null) {
                    Files.createFile(target, attrs);
                } else {
                    Files.createFile(target);
                }
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileSystemException(io.vertx.core.file.FileSystemException) IOException(java.io.IOException)

Example 12 with FileSystemException

use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.

the class FileSystemImpl method mkdirInternal.

protected BlockingAction<Void> mkdirInternal(String path, String perms, boolean createParents, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(path);
    FileAttribute<?> attrs = perms == null ? null : PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString(perms));
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path source = vertx.resolveFile(path).toPath();
                if (createParents) {
                    if (attrs != null) {
                        Files.createDirectories(source, attrs);
                    } else {
                        Files.createDirectories(source);
                    }
                } else {
                    if (attrs != null) {
                        Files.createDirectory(source, attrs);
                    } else {
                        Files.createDirectory(source);
                    }
                }
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileSystemException(io.vertx.core.file.FileSystemException) IOException(java.io.IOException)

Example 13 with FileSystemException

use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.

the class FileSystemImpl method copyInternal.

private BlockingAction<Void> copyInternal(String from, String to, boolean recursive, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(from);
    Objects.requireNonNull(to);
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path source = vertx.resolveFile(from).toPath();
                Path target = vertx.resolveFile(to).toPath();
                if (recursive) {
                    Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {

                        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                            Path targetDir = target.resolve(source.relativize(dir));
                            try {
                                Files.copy(dir, targetDir);
                            } catch (FileAlreadyExistsException e) {
                                if (!Files.isDirectory(targetDir)) {
                                    throw e;
                                }
                            }
                            return FileVisitResult.CONTINUE;
                        }

                        @Override
                        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                            Files.copy(file, target.resolve(source.relativize(file)));
                            return FileVisitResult.CONTINUE;
                        }
                    });
                } else {
                    Files.copy(source, target);
                }
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) FileSystemException(io.vertx.core.file.FileSystemException) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 14 with FileSystemException

use of io.vertx.core.file.FileSystemException in project vert.x by eclipse.

the class FileSystemImpl method readFileInternal.

private BlockingAction<Buffer> readFileInternal(String path, Handler<AsyncResult<Buffer>> handler) {
    Objects.requireNonNull(path);
    return new BlockingAction<Buffer>(handler) {

        public Buffer perform() {
            try {
                Path target = vertx.resolveFile(path).toPath();
                byte[] bytes = Files.readAllBytes(target);
                Buffer buff = Buffer.buffer(bytes);
                return buff;
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
        }
    };
}
Also used : Path(java.nio.file.Path) Buffer(io.vertx.core.buffer.Buffer) FileSystemException(io.vertx.core.file.FileSystemException) IOException(java.io.IOException)

Aggregations

FileSystemException (io.vertx.core.file.FileSystemException)14 IOException (java.io.IOException)14 Path (java.nio.file.Path)11 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3 RandomAccessFile (java.io.RandomAccessFile)2 FileVisitResult (java.nio.file.FileVisitResult)2 Buffer (io.vertx.core.buffer.Buffer)1 AsyncFile (io.vertx.core.file.AsyncFile)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 FileStore (java.nio.file.FileStore)1 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)1 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)1 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)1 UserPrincipal (java.nio.file.attribute.UserPrincipal)1 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)1 ArrayList (java.util.ArrayList)1