/** * @param out the output stream to write to * @param start First byte to write * @param count Bytes to write or -1 for all of them. * @throws IOException if unable to copy the Resource to the output */ public void writeTo(OutputStream out,long start,long count) throws IOException { try (InputStream in = getInputStream()) { in.skip(start); if (count<0) IO.copy(in,out); else IO.copy(in,out,count); } }
/** * closes a reader, and logs exceptions * * @param reader the reader to close */ public static void close(Reader reader) { close((Closeable)reader); }
/** Read input stream to string. * @param in the stream to read from (until EOF) * @return the String parsed from stream (default Charset) * @throws IOException if unable to read the stream (or handle the charset) */ public static String toString(InputStream in) throws IOException { return toString(in,(Charset)null); }
if (web_app.isAlias()) LOG.debug(web_app + " anti-aliased to " + web_app.getAlias()); web_app = context.newResource(web_app.getAlias()); if (LOG.isDebugEnabled()) LOG.debug("Try webapp=" + web_app + ", exists=" + web_app.exists() + ", directory=" + web_app.isDirectory()+" file="+(web_app.getFile())); IO.delete(extractedWebAppDir); extractedWebAppDir.mkdir(); LOG.debug("Extract " + web_app + " to " + extractedWebAppDir); IO.delete(extractedWebInfDir); extractedWebInfDir.mkdir(); Resource web_inf_lib = web_inf.addPath("lib/"); IO.delete(webInfLibDir); webInfLibDir.mkdir(); IO.delete(webInfClassesDir); webInfClassesDir.mkdir(); LOG.debug("Copying WEB-INF/classes from "+web_inf_classes+" to "+webInfClassesDir.getAbsolutePath());
public DefaultHandler() { try { URL fav = this.getClass().getClassLoader().getResource("org/eclipse/jetty/favicon.ico"); if (fav!=null) { Resource r = Resource.newResource(fav); _favicon=IO.readBytes(r.getInputStream()); } } catch(Exception e) { LOG.warn(e); } }
@Override public void destroy() { for (File file : _files) { if (file.exists()) { if (LOG.isDebugEnabled()) LOG.debug("Destroy {}",file); IO.delete(file); } } }
byte[] bytes = IO.readBytes(content); if (LOG.isDebugEnabled()) LOG.debug("foundClass({}) url={} cl={}",name,url,this);
return; if(LOG.isDebugEnabled()) LOG.debug("Extract "+this+" to "+directory); boolean subEntryIsDir = (subEntryName != null && subEntryName.endsWith("/")?true:false); if (LOG.isDebugEnabled()) LOG.debug("Extracting entry = "+subEntryName+" from jar "+jarFileURL); URLConnection c = jarFileURL.openConnection(); IO.copy(jin,fout);
private Path extractPackedFile(JarFileResource configResource) throws IOException { String uri = configResource.getURI().toASCIIString(); int colon = uri.lastIndexOf(":"); int bang_slash = uri.indexOf("!/"); if (colon < 0 || bang_slash < 0 || colon > bang_slash) throw new IllegalArgumentException("Not resolved JarFile resource: " + uri); String entry_path = uri.substring(colon + 2).replace("!/", "__").replace('/', '_').replace('.', '_'); Path tmpDirectory = Files.createTempDirectory("users_store"); tmpDirectory.toFile().deleteOnExit(); Path extractedPath = Paths.get(tmpDirectory.toString(), entry_path); Files.deleteIfExists(extractedPath); extractedPath.toFile().deleteOnExit(); IO.copy(configResource.getInputStream(), new FileOutputStream(extractedPath.toFile())); if (isHotReload()) { LOG.warn("Cannot hot reload from packed configuration: {}", configResource); setHotReload(false); } return extractedPath; }
/** Copy Reader to Writer out until EOF or exception. * @param in the read to read from (until EOF) * @param out the writer to write to * @throws IOException if unable to copy the streams */ public static void copy(Reader in, Writer out) throws IOException { copy(in,out,-1); }
@Override public void close() throws IOException {this.in=IO.getClosedStream();} };
@Override public PrintWriter getWriter() throws IOException { return IO.getNullPrintWriter(); }
@Override public void deconfigure(WebAppContext context) throws Exception { //if we're not persisting the temp dir contents delete it if (!context.isPersistTempDirectory()) { IO.delete(context.getTempDirectory()); } //if it wasn't explicitly configured by the user, then unset it Boolean tmpdirConfigured = (Boolean)context.getAttribute(TEMPDIR_CONFIGURED); if (tmpdirConfigured != null && !tmpdirConfigured) context.setTempDirectory(null); //reset the base resource back to what it was before we did any unpacking of resources if (context.getBaseResource() != null) context.getBaseResource().close(); context.setBaseResource(_preUnpackBaseResource); }
public void configureTempDirectory (File dir, WebAppContext context) { if (dir == null) throw new IllegalArgumentException("Null temp dir"); //if dir exists and we don't want it persisted, delete it if (dir.exists() && !context.isPersistTempDirectory()) { if (!IO.delete(dir)) throw new IllegalStateException("Failed to delete temp dir "+dir); } //if it doesn't exist make it if (!dir.exists()) dir.mkdirs(); if (!context.isPersistTempDirectory()) dir.deleteOnExit(); //is it useable if (!dir.canWrite() || !dir.isDirectory()) throw new IllegalStateException("Temp dir "+dir+" not useable: writeable="+dir.canWrite()+", dir="+dir.isDirectory()); }
public InMemoryResource(InMemoryResource parent, InputStream stream, String fileName) { this.parent = parent; try { self = new ByteArray(); if (stream != null) { self.bytes = IO.readBytes(stream); } } catch (IOException e) { throw new RuntimeException(String.format("Unable to read input stream for %s", fileName) ); } this.fileName = fileName; }
@Override public void copyTo(File destination) throws IOException { if (isDirectory()) { IO.copyDir(this.path.toFile(),destination); } else { Files.copy(this.path,destination.toPath()); } }