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;
}
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);
}
}
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);
}
}
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;
}
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);
}
Aggregations