@Override public boolean isDirectory(String url) { try { return toFile(url).isDirectory(); } catch (URISyntaxException | MalformedURLException e) { logger.warn("Could not inspect directory.", e); return false; } }
@Override public InputStream open(String url) throws IOException { try { File file = toFile(url); return new FileInputStream(file); } catch (URISyntaxException e) { throw new IOException("Could not process the given URL.", e); } }
@Override public long getFileSize(String fileUrl) throws FileNotFoundException { try { File file = toFile(fileUrl); return file.length(); } catch (MalformedURLException | URISyntaxException e) { logger.error("Illegal URL: \"{}\"", fileUrl); throw new FileNotFoundException("File not found, because the URL is not correct."); } }
private static File toFile(String fileUrl) throws URISyntaxException, MalformedURLException { if (fileUrl.startsWith("file:")) { return new File(new URL(fileUrl).toURI()); } else { logger.warn("Expect URLs, but got {}. Converting it to file:{}...", fileUrl, fileUrl); return toFile("file:" + fileUrl); } }
@Override public boolean delete(String url, boolean isRecursiveDelete) throws IOException { try { final File file = toFile(url); if (!isRecursiveDelete && file.isDirectory()) return false; return this.delete(file); } catch (URISyntaxException e) { throw new IOException("Cannot access file.", e); } }
@Override public Collection<String> listChildren(String url) { try { final File[] files = toFile(url).listFiles(); if (files == null) { return Collections.emptyList(); } return Arrays.stream(files).map(File::toURI).map(Object::toString).collect(Collectors.toList()); } catch (URISyntaxException | MalformedURLException e) { logger.warn("Could not inspect directory.", e); return Collections.emptyList(); } }
@Override public OutputStream create(String url, Boolean forceCreateParentDirs) throws IOException { File file = null; try { file = toFile(url); if (forceCreateParentDirs && file.getParentFile()!=null) file.getParentFile().mkdirs(); return new FileOutputStream(file, false); } catch (URISyntaxException e) { throw new IOException("Could not process the given URL.", e); } catch (IOException e) { if (file != null) { file.delete(); } throw e; } }