public static File unzipToTemp(ZipFile file, ZipEntry entry) throws IOException { InputStream in = null; OutputStream out = null; try { String entryExtension = StringUtils.substringAfterLast(entry.getName(), "."); File temp = new File(OperatingSystemUtils.getTempDirectory(), UUID.randomUUID().toString() + "." + entryExtension); in = file.getInputStream(entry); out = new FileOutputStream(temp); Streams.write(in, out); log.log(Level.INFO, "Extracting entry: " + entry.getName() + " to: " + temp.getAbsolutePath()); return temp; } catch (Exception e) { throw new IOException("Exception extracting entry: " + entry.getName(), e); } finally { Streams.closeQuietly(in); Streams.closeQuietly(out); } }
@Override public String getContents() { InputStream stream = getResourceInputStream(); try { return Streams.toString(stream); } finally { Streams.closeQuietly(stream); } }
Streams.write(zipInputStream, outputStream);
@Override public boolean compatibleWith(final Resource<?> template) { String contents = Streams.toString(template.getResourceInputStream()); for (String section : Arrays.asList("main")) { if (!contents.matches(".*:\\s*insert\\s+name\\s*=\\s*\"" + section + "\".*")) { return false; } } return true; }
/** * Execute the given {@link Callable} by creating an {@link URLClassLoader} from the given {@link URL} array. * <p/> * Return the result, if any. */ public static <T> T executeIn(URL[] urls, Callable<T> callable) throws Exception { ClassLoader savedClassLoader = Thread.currentThread().getContextClassLoader(); URLClassLoader newClassLoader = null; try { newClassLoader = new URLClassLoader(urls, savedClassLoader); return executeIn(newClassLoader, callable); } finally { Streams.closeQuietly(newClassLoader); } }
Streams.write(zipInputStream, outputStream);
String result = Streams.toString(template.openStream());
Streams.write(is, fos); Streams.closeQuietly(is); Streams.closeQuietly(fos);
Streams.write(zipInputStream, outputStream);
@Override public String getContents(Charset charset) { InputStream stream = getResourceInputStream(); try { return Streams.toString(stream, charset); } finally { Streams.closeQuietly(stream); } }
/** * @param project */ private void generateReadme(Project project) { String readmeTemplate = Streams.toString(getClass().getResourceAsStream("README.asciidoc")); FileResource<?> child = project.getRoot().reify(DirectoryResource.class) .getChildOfType(FileResource.class, "README.asciidoc"); // TODO: Replace with template addon MetadataFacet metadata = project.getFacet(MetadataFacet.class); readmeTemplate = readmeTemplate.replaceAll("\\{\\{ADDON_GROUP_ID\\}\\}", metadata.getProjectGroupName()); readmeTemplate = readmeTemplate.replaceAll("\\{\\{ADDON_ARTIFACT_ID\\}\\}", metadata.getProjectName()); child.createNewFile(); child.setContents(readmeTemplate); }
Streams.write(data, out); out.flush(); Streams.closeQuietly(data);
Streams.write(zip, bos);
@Override @SuppressWarnings("unchecked") public String contentOf(String template) { DirectoryResource[] candidates = new DirectoryResource[] { cloneDir(), cloneDir().getChildDirectory(GLOBAL_TEMPLATES) }; for (DirectoryResource dir : candidates) { if (listGitignores(dir).contains(template)) { FileResource<?> file = dir.getChildOfType(FileResource.class, template + GITIGNORE); return Streams.toString(file.getResourceInputStream()); } } return ""; }
Streams.write(new ByteArrayInputStream(baos.toByteArray()), resourceOutputStream);