/** * Copies all the bytes it can read from the input stream into the output * stream; input and output streams management (opening, flushing, closing) * are all up to the caller. * * @param input * an open and ready-to-be-read input stream. * @param output * an open output stream. * @return * the total number of bytes copied. * @throws IOException */ public static long copy(InputStream input, OutputStream output) throws IOException { return copy(input, output, false); }
/** * Returns an input stream from a string representing its path. * * @param filepath * a string representing the file path. * @return * a {@code FileInputStream} object. * @throws FileNotFoundException */ public static InputStream fromFile(String filepath) throws FileNotFoundException { if(Strings.isValid(filepath)) { return fromFile(new File(filepath)); } return null; }
/** * @see org.dihedron.patterns.cache.Storage#clear() */ @Override public void clear() { logger.debug("clearing storage"); for(String resource : contents.keySet()) { Streams.safelyClose(contents.get(resource)); } contents.clear(); } }
/** * Constructor. * * @param name * the name of the library; the "<library>.properties" (where <<library> * is the name of the library) will be loaded from the root of the classpath. */ protected Library(String name) { this.name = name; String path = PROPERTIES_FILE.replaceAll("\\$\\{library\\}", name); logger.trace("loading from '{}'", path); try(InputStream stream = Streams.fromURL(path)){ properties.load(stream); } catch (IOException e) { logger.error("error loading library properties from input stream", e); } catch (PropertiesException e) { logger.error("error loading library properties from input stream", e); } }
if(input != null) { output = new ByteArrayOutputStream(); long copied = Streams.copy(input, output); logger.trace("copied {} bytes from cache", copied); return output.toByteArray(); throw new CacheException("error copying data from cache to byte array", e); } finally { Streams.safelyClose(input); Streams.safelyClose(output);
String path = PROPERTIES_FILE.replaceAll("\\$\\{library\\}", name); logger.trace("loading from '{}'", path); stream = Streams.fromURL(path); properties.load(stream); } catch (IOException e) {
if(output != null) { input = new ByteArrayInputStream(data); long copied = Streams.copy(input, output); logger.trace("copied {} bytes into cache", copied); return copied; throw new CacheException("error copying data from byte array to cache", e); } finally { Streams.safelyClose(input); Streams.safelyClose(output);
/** * Copies all the bytes it can read from the input stream into the output * stream; input and output streams management (opening, flushing, closing) * are all up to the caller. * * @param input * an open and ready-to-be-read input stream. * @param output * an open output stream. * @return * the total number of bytes copied. * @throws IOException */ public static long copy(InputStream input, OutputStream output) throws IOException { return copy(input, output, false); }
String path = PROPERTIES_FILE.replaceAll("\\$\\{library\\}", name); logger.trace("loading from '{}'", path); stream = Streams.fromURL(path); properties.load(stream); } catch (IOException e) {
/** * Returns an input stream from a string representing its path. * * @param filepath * a string representing the file path. * @return * a {@code FileInputStream} object. * @throws FileNotFoundException */ public static InputStream fromFile(String filepath) throws FileNotFoundException { if(Strings.isValid(filepath)) { return fromFile(new File(filepath)); } return null; }
/** * @see org.dihedron.patterns.cache.Storage#delete(org.dihedron.core.regex.Regex) */ @Override public void delete(Regex regex) { Set<String> resources = new HashSet<String>(contents.keySet()); for (String resource : resources) { if(regex.matches(resource)){ logger.debug("removing resource '{}'", resource); Streams.safelyClose(contents.get(resource)); contents.remove(resource); } } }
/** * Copies all the bytes it can read from the input stream into the output * stream; input and output streams management (opening, flushing, closing) * are all up to the caller. * * @param input * an open and ready-to-be-read input stream. * @param output * an open output stream. * @return * the total number of bytes copied. * @throws IOException */ public static long copy(InputStream input, OutputStream output) throws IOException { return copy(input, output, false); }
/** * Reads a resource from an URL, specified as a string; since this class makes * use of the URL factory, URLs may also represent resources in the class * path, re (in the format "classpath:org/dihedron/resources/MyResource.png"). * * @param url * a string presenting an URL. * @return * an input stream to access the URL. * @throws IOException * if the URL is malformed or an error occurs opening the stream. */ public static InputStream fromURL(String url) throws IOException { if(Strings.isValid(url)) { return fromURL(URLFactory.makeURL(url)); } return null; }
/** * Returns an input stream from a string representing its path. * * @param filepath * a string representing the file path. * @return * a {@code FileInputStream} object. * @throws FileNotFoundException */ public static InputStream fromFile(String filepath) throws FileNotFoundException { if(Strings.isValid(filepath)) { return fromFile(new File(filepath)); } return null; }
/** * @see org.dihedron.patterns.cache.Storage#delete(java.lang.String, boolean) */ @Override public void delete(String resource, boolean caseSensitive) { Set<String> resources = new HashSet<String>(contents.keySet()); for (String string : resources) { if(caseSensitive && string.equals(resource)) { logger.debug("removing resource '{}'", resource); Streams.safelyClose(contents.get(resource)); contents.remove(resource); } else if(!caseSensitive && string.equalsIgnoreCase(resource)){ logger.debug("removing resource '{}'", resource); Streams.safelyClose(contents.get(resource)); contents.remove(resource); } else { logger.trace("keeping resource '{}'", resource); } } }
/** * Adds a file to the ZIP archive, given its content as an input stream. * * @param zipEntry * name of the entry in the archive. * @param input * the stream from which data will be read to be added to the ZIP archive; * the stream must be open and will not be closed once the operation is * complete, so it is up to the caller to release it. */ public void addFile(String zipEntry, InputStream input) throws IOException { if(input != null) { stream.putNextEntry(new ZipEntry(zipEntry)); Streams.copy(input, stream); } }
/** * Reads a resource from an URL, specified as a string; since this class makes * use of the URL factory, URLs may also represent resources in the class * path, re (in the format "classpath:org/dihedron/resources/MyResource.png"). * * @param url * a string presenting an URL. * @return * an input stream to access the URL. * @throws IOException * if the URL is malformed or an error occurs opening the stream. */ public static InputStream fromURL(String url) throws IOException { if(Strings.isValid(url)) { return fromURL(URLFactory.makeURL(url)); } return null; }
/** * @see org.dihedron.patterns.cache.Storage#store(java.lang.String, java.io.InputStream) */ @Override public OutputStream store(String resource) throws CacheException { if(Strings.isValid(resource)) { Streams.safelyClose(contents.get(resource)); logger.debug("storing resource '{}'", resource); CacheOutputStream<ByteArrayOutputStream> stream = new CacheOutputStream<ByteArrayOutputStream>(new ByteArrayOutputStream()); contents.put(resource, stream); return stream; } return null; }
/** * Dumps the response data as a string. * * @see java.lang.Object#toString() */ @Override public String toString() { try(InputStream input = connection.getInputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream()) { Streams.copy(input, output); String data = new String(output.toByteArray()); // logger.trace("response data:\n{}", data); return data; } catch (IOException e) { logger.error("error reading response data from stream", e); } return null; } }
/** * Reads a resource from an URL, specified as a string; since this class makes * use of the URL factory, URLs may also represent resources in the class * path, re (in the format "classpath:org/dihedron/resources/MyResource.png"). * * @param url * a string presenting an URL. * @return * an input stream to access the URL. * @throws IOException * if the URL is malformed or an error occurs opening the stream. */ public static InputStream fromURL(String url) throws IOException { if(Strings.isValid(url)) { return fromURL(URLFactory.makeURL(url)); } return null; }