private static ByteArrayOutputStream read(InputStream in) throws IOException { if (in == null) { throw new NullPointerException(); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); copy(in, baos); return baos; } }
public static byte[] readAsBytes(InputStream in) throws IOException { return read(in).toByteArray(); }
public Resource(URL url) throws IOException { URLConnection conn = url.openConnection(); this.timestamp = conn.getLastModified(); this.content = IO.readAsBytes(conn.getInputStream()); }
public void file(String fileName, int length, InputStream data) throws IOException { out.write("C0644 ".getBytes()); out.write(Integer.toString(length).getBytes()); out.write(" ".getBytes()); out.write(fileName.getBytes()); out.write("\n".getBytes()); out.flush(); readAck(); IO.copy(data, out); ack(); readAck(); } public void endDirectory(String directoryName) throws IOException {
Resource getResource() throws IOException { InputStream in = open(); byte[] bytes = IO.readAsBytes(in); long lastModified = getLastModified(); return new Resource(bytes, lastModified); }
public static String readAsUTF8(InputStream in) { try { ByteArrayOutputStream baos = read(in); return baos.toString("UTF-8"); } catch (IOException e) { e.printStackTrace(); return null; } }
private void copy(org.crsh.vfs.File src, File dst) throws IOException { if (src.isDir()) { if (!dst.exists()) { if (dst.mkdir()) { log.fine("Could not create dir " + dst.getCanonicalPath()); } } if (dst.exists() && dst.isDirectory()) { for (org.crsh.vfs.File child : src.children()) { copy(child, new File(dst, child.getName())); } } } else { if (!dst.exists()) { Resource resource = src.getResource(); if (resource != null) { log.info("Copied resource " + src.getPath().getValue() + " to " + dst.getCanonicalPath()); IO.copy(new ByteArrayInputStream(resource.getContent()), new FileOutputStream(dst)); } } } }