private static String getMessage(HttpURLConnection conn) { try (InputStream in = conn.getErrorStream()) { if (in != null) return IO.collect(in); } catch (Exception e) { // Ignore } return ""; }
public static Path copy(InputStream in, Path path) throws IOException { try (FileChannel out = writeChannel(path)) { copy(in, out); } return path; }
/** * Deletes and creates directories */ public static void initialize(File dir) { try { deleteWithException(dir); mkdirs(dir); } catch (IOException e) { throw new RuntimeException(e); } }
public void save(File location) throws IOException { if (location.getName() .endsWith(".gz")) compress = true; IO.mkdirs(location.getParentFile()); File tmp = IO.createTempFile(location.getParentFile(), "index", ".xml"); try (OutputStream out = IO.outputStream(tmp)) { save(out); } IO.rename(tmp, location); }
private synchronized void save() throws Exception { Path index = indexFile.toPath(); Path tmp = Files.createTempFile(IO.mkdirs(index.getParent()), "index", null); try (PrintWriter pw = IO.writer(tmp)) { archives.keySet() .stream() .sorted() .forEachOrdered(archive -> pw.println(archive)); } IO.rename(tmp, index); lastModified = indexFile.lastModified(); }
public static URL toURL(String s, File base) throws MalformedURLException { int n = s.indexOf(':'); if (n > 0 && n < 10) { // is url return new URL(s); } return getFile(base, s).toURI() .toURL(); }
@Override public void close() throws IOException { /* * Allow original buffer to be garbage collected and prevent it being * remapped for this FileResouce. */ buffer = CLOSED; if (deleteOnClose) IO.delete(file); }
private void copyResource(File dir, String path, Resource resource) throws Exception { File to = IO.getBasedFile(dir, path); IO.mkdirs(to.getParentFile()); IO.copy(resource.openInputStream(), to); }
/** * Deletes the specified file. Folders are recursively deleted.<br> * Throws exception if any of the files could not be deleted. * * @param file file to be deleted * @throws IOException if the file (or contents of a folder) could not be * deleted */ public static void deleteWithException(File file) throws IOException { deleteWithException(file.toPath()); }
public LineCollection(File in) throws IOException { this(IO.reader(in, UTF_8)); }