Search in sources :

Example 51 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project vert.x by eclipse.

the class FileCache method cacheFile.

File cacheFile(String fileName, File resource, boolean overwrite) throws IOException {
    File cacheFile = new File(getCacheDir(), fileName);
    fileNameCheck(cacheFile);
    boolean isDirectory = resource.isDirectory();
    if (!isDirectory) {
        cacheFile.getParentFile().mkdirs();
        if (!overwrite) {
            try {
                Files.copy(resource.toPath(), cacheFile.toPath());
            } catch (FileAlreadyExistsException ignore) {
            }
        } else {
            Files.copy(resource.toPath(), cacheFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        }
    } else {
        cacheFile.mkdirs();
    }
    return cacheFile;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) File(java.io.File)

Example 52 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project vert.x by eclipse.

the class FileCache method cacheFile.

void cacheFile(String fileName, InputStream is, boolean overwrite) throws IOException {
    File cacheFile = new File(getCacheDir(), fileName);
    fileNameCheck(cacheFile);
    cacheFile.getParentFile().mkdirs();
    if (!overwrite) {
        try {
            Files.copy(is, cacheFile.toPath());
        } catch (FileAlreadyExistsException ignore) {
        }
    } else {
        Files.copy(is, cacheFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) File(java.io.File)

Example 53 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project elasticsearch by elastic.

the class StreamOutput method writeException.

public void writeException(Throwable throwable) throws IOException {
    if (throwable == null) {
        writeBoolean(false);
    } else {
        writeBoolean(true);
        boolean writeCause = true;
        boolean writeMessage = true;
        if (throwable instanceof CorruptIndexException) {
            writeVInt(1);
            writeOptionalString(((CorruptIndexException) throwable).getOriginalMessage());
            writeOptionalString(((CorruptIndexException) throwable).getResourceDescription());
            writeMessage = false;
        } else if (throwable instanceof IndexFormatTooNewException) {
            writeVInt(2);
            writeOptionalString(((IndexFormatTooNewException) throwable).getResourceDescription());
            writeInt(((IndexFormatTooNewException) throwable).getVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMinVersion());
            writeInt(((IndexFormatTooNewException) throwable).getMaxVersion());
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof IndexFormatTooOldException) {
            writeVInt(3);
            IndexFormatTooOldException t = (IndexFormatTooOldException) throwable;
            writeOptionalString(t.getResourceDescription());
            if (t.getVersion() == null) {
                writeBoolean(false);
                writeOptionalString(t.getReason());
            } else {
                writeBoolean(true);
                writeInt(t.getVersion());
                writeInt(t.getMinVersion());
                writeInt(t.getMaxVersion());
            }
            writeMessage = false;
            writeCause = false;
        } else if (throwable instanceof NullPointerException) {
            writeVInt(4);
            writeCause = false;
        } else if (throwable instanceof NumberFormatException) {
            writeVInt(5);
            writeCause = false;
        } else if (throwable instanceof IllegalArgumentException) {
            writeVInt(6);
        } else if (throwable instanceof AlreadyClosedException) {
            writeVInt(7);
        } else if (throwable instanceof EOFException) {
            writeVInt(8);
            writeCause = false;
        } else if (throwable instanceof SecurityException) {
            writeVInt(9);
        } else if (throwable instanceof StringIndexOutOfBoundsException) {
            writeVInt(10);
            writeCause = false;
        } else if (throwable instanceof ArrayIndexOutOfBoundsException) {
            writeVInt(11);
            writeCause = false;
        } else if (throwable instanceof FileNotFoundException) {
            writeVInt(12);
            writeCause = false;
        } else if (throwable instanceof FileSystemException) {
            writeVInt(13);
            if (throwable instanceof NoSuchFileException) {
                writeVInt(0);
            } else if (throwable instanceof NotDirectoryException) {
                writeVInt(1);
            } else if (throwable instanceof DirectoryNotEmptyException) {
                writeVInt(2);
            } else if (throwable instanceof AtomicMoveNotSupportedException) {
                writeVInt(3);
            } else if (throwable instanceof FileAlreadyExistsException) {
                writeVInt(4);
            } else if (throwable instanceof AccessDeniedException) {
                writeVInt(5);
            } else if (throwable instanceof FileSystemLoopException) {
                writeVInt(6);
            } else {
                writeVInt(7);
            }
            writeOptionalString(((FileSystemException) throwable).getFile());
            writeOptionalString(((FileSystemException) throwable).getOtherFile());
            writeOptionalString(((FileSystemException) throwable).getReason());
            writeCause = false;
        } else if (throwable instanceof IllegalStateException) {
            writeVInt(14);
        } else if (throwable instanceof LockObtainFailedException) {
            writeVInt(15);
        } else if (throwable instanceof InterruptedException) {
            writeVInt(16);
            writeCause = false;
        } else if (throwable instanceof IOException) {
            writeVInt(17);
        } else {
            ElasticsearchException ex;
            if (throwable instanceof ElasticsearchException && ElasticsearchException.isRegistered(throwable.getClass(), version)) {
                ex = (ElasticsearchException) throwable;
            } else {
                ex = new NotSerializableExceptionWrapper(throwable);
            }
            writeVInt(0);
            writeVInt(ElasticsearchException.getId(ex.getClass()));
            ex.writeTo(this);
            return;
        }
        if (writeMessage) {
            writeOptionalString(throwable.getMessage());
        }
        if (writeCause) {
            writeException(throwable.getCause());
        }
        ElasticsearchException.writeStackTraces(throwable, this);
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AccessDeniedException(java.nio.file.AccessDeniedException) FileNotFoundException(java.io.FileNotFoundException) NoSuchFileException(java.nio.file.NoSuchFileException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ElasticsearchException(org.elasticsearch.ElasticsearchException) FileSystemException(java.nio.file.FileSystemException) NotDirectoryException(java.nio.file.NotDirectoryException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) EOFException(java.io.EOFException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) IOException(java.io.IOException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) FileSystemLoopException(java.nio.file.FileSystemLoopException)

Example 54 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project vert.x by eclipse.

the class FileResolver method unpackFromFileURL.

private synchronized File unpackFromFileURL(URL url, String fileName, ClassLoader cl) {
    File resource;
    try {
        resource = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new VertxException(e);
    }
    boolean isDirectory = resource.isDirectory();
    File cacheFile = new File(cacheDir, fileName);
    if (!isDirectory) {
        cacheFile.getParentFile().mkdirs();
        try {
            if (ENABLE_CACHING) {
                Files.copy(resource.toPath(), cacheFile.toPath());
            } else {
                Files.copy(resource.toPath(), cacheFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
        } catch (FileAlreadyExistsException ignore) {
        } catch (IOException e) {
            throw new VertxException(e);
        }
    } else {
        cacheFile.mkdirs();
        String[] listing = resource.list();
        for (String file : listing) {
            String subResource = fileName + "/" + file;
            URL url2 = cl.getResource(subResource);
            unpackFromFileURL(url2, subResource, cl);
        }
    }
    return cacheFile;
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) VertxException(io.vertx.core.VertxException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile) URL(java.net.URL)

Example 55 with FileAlreadyExistsException

use of java.nio.file.FileAlreadyExistsException in project vert.x by eclipse.

the class FileResolver method unpackFromJarURL.

private synchronized File unpackFromJarURL(URL url, String fileName, ClassLoader cl) {
    ZipFile zip = null;
    try {
        String path = url.getPath();
        int idx1 = path.lastIndexOf(".jar!");
        if (idx1 == -1) {
            idx1 = path.lastIndexOf(".zip!");
        }
        int idx2 = path.lastIndexOf(".jar!", idx1 - 1);
        if (idx2 == -1) {
            idx2 = path.lastIndexOf(".zip!", idx1 - 1);
        }
        if (idx2 == -1) {
            File file = new File(URLDecoder.decode(path.substring(5, idx1 + 4), "UTF-8"));
            zip = new ZipFile(file);
        } else {
            String s = path.substring(idx2 + 6, idx1 + 4);
            File file = resolveFile(s);
            zip = new ZipFile(file);
        }
        String inJarPath = path.substring(idx1 + 6);
        String[] parts = JAR_URL_SEP_PATTERN.split(inJarPath);
        StringBuilder prefixBuilder = new StringBuilder();
        for (int i = 0; i < parts.length - 1; i++) {
            prefixBuilder.append(parts[i]).append("/");
        }
        String prefix = prefixBuilder.toString();
        Enumeration<? extends ZipEntry> entries = zip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            String name = entry.getName();
            if (name.startsWith(prefix.isEmpty() ? fileName : prefix + fileName)) {
                File file = new File(cacheDir, prefix.isEmpty() ? name : name.substring(prefix.length()));
                if (name.endsWith("/")) {
                    // Directory
                    file.mkdirs();
                } else {
                    file.getParentFile().mkdirs();
                    try (InputStream is = zip.getInputStream(entry)) {
                        if (ENABLE_CACHING) {
                            Files.copy(is, file.toPath());
                        } else {
                            Files.copy(is, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
                        }
                    } catch (FileAlreadyExistsException ignore) {
                    }
                }
            }
        }
    } catch (IOException e) {
        throw new VertxException(e);
    } finally {
        closeQuietly(zip);
    }
    return new File(cacheDir, fileName);
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ZipFile(java.util.zip.ZipFile) InputStream(java.io.InputStream) VertxException(io.vertx.core.VertxException) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)104 Path (java.nio.file.Path)49 IOException (java.io.IOException)44 File (java.io.File)24 NoSuchFileException (java.nio.file.NoSuchFileException)22 Test (org.junit.Test)15 FileNotFoundException (java.io.FileNotFoundException)10 FileSystemException (java.nio.file.FileSystemException)9 AccessDeniedException (java.nio.file.AccessDeniedException)8 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)8 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)8 NotDirectoryException (java.nio.file.NotDirectoryException)7 AtomicMoveNotSupportedException (java.nio.file.AtomicMoveNotSupportedException)5 CopyOption (java.nio.file.CopyOption)5 FileSystem (java.nio.file.FileSystem)5 FileVisitResult (java.nio.file.FileVisitResult)5 MCRPath (org.mycore.datamodel.niofs.MCRPath)5 FileOutputStream (java.io.FileOutputStream)4 InputStream (java.io.InputStream)4 FileSystemLoopException (java.nio.file.FileSystemLoopException)4