This page was generated using Webiyo. See its source code and unit tests.
|
org/webiyo/util/Files.javapackage org.webiyo.util; import org.webiyo.web.HtmlWriter; import org.webiyo.web.Renderable; import java.io.*; public class Files { private Files() { } public static void copyBytes(InputStream source, OutputStream dest) throws IOException { while (true) { int c = source.read(); if (c == -1) break; dest.write(c); } } public static void copyBytes(InputStream source, File dest) throws IOException { OutputStream out = new FileOutputStream(dest); try { copyBytes(source, out); } finally { out.close(); } } public static void copyChars(Reader source, Appendable dest) throws IOException { while (true) { int c = source.read(); if (c == -1) break; dest.append((char) c); } } public static void deleteDir(File dir) throws IOException { for (File file : dir.listFiles()) { if (file.isDirectory()) { deleteDir(file); } else { if (!file.delete()) { throw new IOException("unable to delete file: " + file.getAbsolutePath()); } } } if (!dir.delete()) { throw new IOException("unable to delete directory: " + dir.getAbsolutePath()); } } public static byte[] readBytes(File file) throws IOException { return readBytes(new FileInputStream(file)); } public static byte[] readBytes(InputStream in) throws IOException { try { ByteArrayOutputStream result = new ByteArrayOutputStream(); copyBytes(in, result); return result.toByteArray(); } finally { in.close(); } } public static String readString(File file) throws IOException { return readString(new FileInputStream(file)); } public static String readString(InputStream is) throws IOException { return readString(new InputStreamReader(is)); } public static String readString(Reader reader) throws IOException { try { StringBuilder result = new StringBuilder(); copyChars(reader, result); return result.toString(); } finally { reader.close(); } } public static void mkdirs(File dirToCreate) throws IOException { dirToCreate.mkdirs(); if (!dirToCreate.isDirectory()) { throw new IOException("can't create directory: " + dirToCreate.getPath()); } } public static void writePage(Renderable page, File file) throws IOException { mkdirs(file.getParentFile()); Writer out = new FileWriter(file); try { page.render(new HtmlWriter(out)); } finally { out.close(); } } } |