List of usage examples for java.nio.file FileAlreadyExistsException FileAlreadyExistsException
public FileAlreadyExistsException(String file)
From source file:net.dv8tion.jda.player.source.RemoteSource.java
@Override public File asFile(String path, boolean deleteIfExists) throws FileAlreadyExistsException, FileNotFoundException { if (path == null || path.isEmpty()) throw new NullPointerException("Provided path was null or empty!"); File file = new File(path); if (file.isDirectory()) throw new IllegalArgumentException("The provided path is a directory, not a file!"); if (file.exists()) { if (!deleteIfExists) { throw new FileAlreadyExistsException("The provided path already has an existing file " + " and the `deleteIfExists` boolean was set to false."); } else {/* w w w. j a v a2s . c o m*/ if (!file.delete()) throw new UnsupportedOperationException("Cannot delete the file. Is it in use?"); } } Thread currentThread = Thread.currentThread(); FileOutputStream fos = new FileOutputStream(file); InputStream input = asStream(); //Writes the bytes of the downloaded audio into the file. //Has detection to detect if the current thread has been interrupted to respect calls to // Thread#interrupt() when an instance of RemoteSource is in an async thread. //TODO: consider replacing with a Future. try { byte[] buffer = new byte[1024]; int amountRead = -1; int i = 0; while (!currentThread.isInterrupted() && ((amountRead = input.read(buffer)) > -1)) { fos.write(buffer, 0, amountRead); } fos.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { input.close(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } return file; }
From source file:com.splicemachine.storage.HNIOFileSystem.java
@Override public void touchFile(Path path) throws IOException { if (!fs.createNewFile(toHPath(path))) { throw new FileAlreadyExistsException(path.toString()); }//from ww w . j a v a 2s.c o m }
From source file:com.splicemachine.storage.HNIOFileSystem.java
@Override public void touchFile(String dir, String fileName) throws IOException { org.apache.hadoop.fs.Path path = new org.apache.hadoop.fs.Path(dir, fileName); if (!fs.createNewFile(path)) { throw new FileAlreadyExistsException(path.toString()); }// w w w .java2 s. c om }
From source file:org.moe.cli.utils.GrabUtils.java
/** * Download file from remote//from ww w.j a v a2 s . c om * @param link address of remote file * @param output symbolic link to the local file system where the downloaded file will be stored * @throws FileAlreadyExistsException if output file has already exists * @throws FileNotFoundException if link isn't present * @throws UnsupportedTypeException if URI links to file with unsupported type * @throws IOException if operation couldn't be successfully completed because of other reasons */ public static void downloadFileFromRemote(@NonNull URI link, @NonNull File output) throws FileAlreadyExistsException, FileNotFoundException, UnsupportedTypeException, IOException { if (output.exists()) { throw new FileAlreadyExistsException(output.toString() + " already exists!"); } String scheme = link.getScheme(); if (scheme == null) { throw new UnsupportedTypeException("Scheme should not be null!"); } else if (scheme.equals("https")) { // Create a new trust manager that trust all certificates TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; // Activate the new trust manager try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { throw new IOException(e); } } URL url = link.normalize().toURL(); FileUtils.copyURLToFile(url, output); //TODO: Timeout?... }
From source file:com.htmlhifive.visualeditor.persister.LocalFileContentsPersister.java
/** * ??(?)???./* ww w . ja v a2s . c o m*/ * * @param metadata ?urlTree * @param ctx urlTree */ @Override public void mkdir(UrlTreeMetaData<InputStream> metadata, UrlTreeContext ctx) throws BadContentException, FileAlreadyExistsException { Path f = generateFileObj(metadata.getAbsolutePath()); if (Files.exists(f)) { throw new FileAlreadyExistsException("file exists"); } try { Files.createDirectories(f); } catch (IOException e) { logger.error("mkdir failure", e); throw new RuntimeException(e); } }
From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java
void createDirectory(CryptoPath cleartextDir, FileAttribute<?>... attrs) throws IOException { CryptoPath cleartextParentDir = cleartextDir.getParent(); if (cleartextParentDir == null) { return;/*from www .ja v a 2s . c om*/ } Path ciphertextParentDir = cryptoPathMapper.getCiphertextDirPath(cleartextParentDir); if (!Files.exists(ciphertextParentDir)) { throw new NoSuchFileException(cleartextParentDir.toString()); } Path ciphertextFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.FILE); if (Files.exists(ciphertextFile)) { throw new FileAlreadyExistsException(cleartextDir.toString()); } Path ciphertextDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextDir, CiphertextFileType.DIRECTORY); boolean success = false; try { Directory ciphertextDir = cryptoPathMapper.getCiphertextDir(cleartextDir); try (FileChannel channel = FileChannel.open(ciphertextDirFile, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE), attrs)) { channel.write(ByteBuffer.wrap(ciphertextDir.dirId.getBytes(UTF_8))); } Files.createDirectories(ciphertextDir.path); success = true; } finally { if (!success) { Files.delete(ciphertextDirFile); dirIdProvider.delete(ciphertextDirFile); } } }
From source file:org.cryptomator.cryptofs.CryptoFileSystemImpl.java
void copy(CryptoPath cleartextSource, CryptoPath cleartextTarget, CopyOption... options) throws IOException { if (cleartextSource.equals(cleartextTarget)) { return;/* w w w. j av a 2s. co m*/ } Path ciphertextSourceFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.FILE); Path ciphertextSourceDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextSource, CiphertextFileType.DIRECTORY); if (Files.exists(ciphertextSourceFile)) { // FILE: Path ciphertextTargetFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.FILE); Files.copy(ciphertextSourceFile, ciphertextTargetFile, options); } else if (Files.exists(ciphertextSourceDirFile)) { // DIRECTORY (non-recursive as per contract): Path ciphertextTargetDirFile = cryptoPathMapper.getCiphertextFilePath(cleartextTarget, CiphertextFileType.DIRECTORY); if (!Files.exists(ciphertextTargetDirFile)) { // create new: createDirectory(cleartextTarget); } else if (ArrayUtils.contains(options, StandardCopyOption.REPLACE_EXISTING)) { // keep existing (if empty): Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget); try (DirectoryStream<Path> ds = Files.newDirectoryStream(ciphertextTargetDir)) { if (ds.iterator().hasNext()) { throw new DirectoryNotEmptyException(cleartextTarget.toString()); } } } else { throw new FileAlreadyExistsException(cleartextTarget.toString()); } if (ArrayUtils.contains(options, StandardCopyOption.COPY_ATTRIBUTES)) { Path ciphertextSourceDir = cryptoPathMapper.getCiphertextDirPath(cleartextSource); Path ciphertextTargetDir = cryptoPathMapper.getCiphertextDirPath(cleartextTarget); copyAttributes(ciphertextSourceDir, ciphertextTargetDir); } } else { throw new NoSuchFileException(cleartextSource.toString()); } }
From source file:nextflow.fs.dx.DxFileSystemProvider.java
/** * Implements the *copy* operation using the DnaNexus API *clone* * * * <p>/*from w w w .j av a2s. co m*/ * See clone https://wiki.dnanexus.com/API-Specification-v1.0.0/Cloning#API-method%3A-%2Fclass-xxxx%2Fclone * * @param source * @param target * @param options * @throws IOException */ @Override public void copy(Path source, Path target, CopyOption... options) throws IOException { List<CopyOption> opts = Arrays.asList(options); boolean targetExists = Files.exists(target); if (targetExists) { if (Files.isRegularFile(target)) { if (opts.contains(StandardCopyOption.REPLACE_EXISTING)) { Files.delete(target); } else { throw new FileAlreadyExistsException("Copy failed -- target file already exists: " + target); } } else if (Files.isDirectory(target)) { target = target.resolve(source.getFileName()); } else { throw new UnsupportedOperationException(); } } String name1 = source.getFileName().toString(); String name2 = target.getFileName().toString(); if (!name1.equals(name2)) { throw new UnsupportedOperationException( "Copy to a file with a different name is not supported: " + source.toString()); } final DxPath dxSource = toDxPath(source); final DxFileSystem dxFileSystem = dxSource.getFileSystem(); dxFileSystem.fileCopy(dxSource, toDxPath(target)); }