@Override public void copyFile(URI source, URI target) throws IOException, CatalogIOException { checkUriExists(source); if ("file".equals(source.getScheme()) && "file".equals(target.getScheme())) { Files.copy(Paths.get(source), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); } else { throw new CatalogIOException("Expected posix file system URIs."); } }
@Override public void moveFile(URI source, URI target) throws IOException, CatalogIOException { checkUriExists(source); if (source.getScheme().equals("file") && target.getScheme().equals("file")) { Files.move(Paths.get(source), Paths.get(target), StandardCopyOption.REPLACE_EXISTING); } else { throw new CatalogIOException("Can't move from " + source.getScheme() + " to " + target.getScheme()); } }
@Override public void rename(URI oldName, URI newName) throws CatalogIOException { String parent; if (isDirectory(oldName)) { // if oldName is a file parent = ".."; } else { parent = "."; } checkUriExists(oldName); checkDirectoryUri(oldName.resolve(parent), true); try { if (!Files.exists(Paths.get(newName))) { Files.move(Paths.get(oldName), Paths.get(newName)); } else { throw new CatalogIOException("Unable to rename. File \"" + newName + "\" already exists"); } } catch (IOException e) { throw new CatalogIOException("Unable to rename file", e); } }
@Override public List<URI> listFiles(URI directory) throws CatalogIOException { checkUriExists(directory); class ListFiles extends SimpleFileVisitor<Path> { private List<String> filePaths = new LinkedList<>(); @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { filePaths.add(file.toAbsolutePath().toString()); return super.visitFile(file, attrs); } public List<String> getFilePaths() { return filePaths; } } ListFiles fileVisitor = new ListFiles(); try { Files.walkFileTree(Paths.get(directory.getPath()), fileVisitor); } catch (IOException e) { throw new CatalogIOException("Unable to walkFileTree", e); } List<URI> fileUris = new LinkedList<>(); for (String filePath : fileVisitor.getFilePaths()) { try { fileUris.add(new URI("file", filePath, null)); } catch (URISyntaxException e) { throw CatalogIOException.uriSyntaxException(filePath, e); } } return fileUris; }