/** copies and closes both */ public static void copyClose(InputStream input, OutputStream output) { try { copy(input, output); } finally { closeQuietly(input); closeQuietly(output); } }
/** @deprecated since 0.7.0 use {@link #newInputStreamWithContents(String)} */ @Deprecated public static InputStream fromString(String contents) { return newInputStreamWithContents(contents); }
/** takes {@link #getResourceFromUrl(String)} and reads fully, into a string */ public String getResourceAsString(String url) { try { return Streams.readFullyStringAndClose(getResourceFromUrl(url)); } catch (Exception e) { log.debug("ResourceUtils got error reading "+url+(context==null?"":" "+context)+" (rethrowing): "+e); throw Throwables.propagate(e); } }
public static byte[] readFullyAndClose(InputStream is) { try { return readFully(is); } finally { Streams.closeQuietly(is); } }
public static String readFullyStringAndClose(InputStream is) { try { return readFullyString(is); } finally { Streams.closeQuietly(is); } }
@Override public void close() { for (MachineProvisioningLocation<?> provisioner : provisioners) { if (provisioner instanceof Closeable) { Streams.closeQuietly((Closeable)provisioner); } } }
public void testCreateListDestroyContainer() throws IOException { context.getBlobStore().createContainerInLocation(null, testContainerName); context.getBlobStore().list(testContainerName); PageSet<? extends StorageMetadata> ps = context.getBlobStore().list(); assertHasItemNamed(ps, testContainerName); Blob b = context.getBlobStore().blobBuilder("my-blob-1").payload(Streams.newInputStreamWithContents("hello world")).build(); context.getBlobStore().putBlob(testContainerName, b); Blob b2 = context.getBlobStore().getBlob(testContainerName, "my-blob-1"); Assert.assertEquals(Streams.readFullyStringAndClose(b2.getPayload().openStream()), "hello world"); context.getBlobStore().deleteContainer(testContainerName); }
/** * Consumes the input stream entirely and then cleanly closes the connection. * Ignores all exceptions completely, not even logging them! * * Consuming the stream fully is useful for preventing idle TCP connections. * See {@linkplain http://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html}. */ public static void consumeAndCloseQuietly(HttpURLConnection connection) { try { Streams.readFully(connection.getInputStream()); } catch (Exception e) {} closeQuietly(connection); }
private String readBom(URL bom) { try (final InputStream ins = bom.openStream()) { return Streams.readFullyString(ins); } catch (IOException e) { throw Exceptions.propagateAnnotated("Error loading Catalog BOM from " + bom, e); } }
private static String tagForEquivalentPlan(String input) { // plans may be trimmed by yaml parser so do that before checking equivalence // it does mean a format change will be ignored return "equivalent-plan("+Streams.getMd5Checksum(Streams.newInputStreamWithContents(input.trim()))+")"; }
public Object parseDslExpression(String input) { String s1 = Yamls.getAs( Yamls.parseAll( Streams.reader(Streams.newInputStreamWithContents(input)) ), String.class ); BasicCampPlatform p = new BasicCampPlatform(); p.pdp().addInterpreter(new BrooklynDslInterpreter()); Object out = p.pdp().applyInterpreters(MutableMap.of("key", s1)).get("key"); log.debug("parsed "+input+" as "+out+" ("+(out==null ? "null" : out.getClass())+")"); return out; }
protected RuntimeException logWithDetailsAndThrow(String message, Throwable optionalCause) { LOG.warn(message + " (throwing)"); int maxLength = 1024; LOG.warn(message + " (throwing)"); Streams.logStreamTail(LOG, "STDERR of problem in "+Tasks.current(), stderr, maxLength); Streams.logStreamTail(LOG, "STDOUT of problem in "+Tasks.current(), stdout, maxLength); Streams.logStreamTail(LOG, "STDIN of problem in "+Tasks.current(), Streams.byteArrayOfString(command != null ? command : psCommand), 4096); if (optionalCause!=null) throw new IllegalStateException(message, optionalCause); throw new IllegalStateException(message); }
protected KubernetesMachineLocation createKubernetesResourceLocation(Entity entity, ConfigBag setup) { String resourceUri = entity.config().get(KubernetesResource.RESOURCE_FILE); InputStream resource = ResourceUtils.create(entity).getResourceFromUrl(resourceUri); String templateContents = Streams.readFullyString(resource); String processedContents = TemplateProcessor.processTemplateContents(templateContents, (EntityInternal) entity, setup.getAllConfig()); InputStream processedResource = Streams.newInputStreamWithContents(processedContents);
@Test public void testSimpleYamlArtifactParse() throws IOException { BasicCampPlatform platform = MockWebPlatform.populate(new BasicCampPlatform()); Reader input = Streams.reader(getClass().getResourceAsStream("pdp-single-artifact.yaml")); DeploymentPlan plan = platform.pdp().parseDeploymentPlan(input); log.info("DP is:\n"+plan.toString()); Assert.assertEquals(plan.getName(), "sample"); Assert.assertEquals(plan.getArtifacts().size(), 1); Assert.assertEquals(plan.getServices().size(), 0); Artifact artifact1 = plan.getArtifacts().iterator().next(); Assert.assertEquals(artifact1.getName(), "sample WAR"); }
public static String getMd5Checksum(InputStream in) { MessageDigest md; try { md = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw Exceptions.propagate(e); } DigestInputStream dis = new DigestInputStream(in, md); readFullyAndClose(dis); byte[] digest = md.digest(); StringBuilder result = new StringBuilder(); for (byte b: digest) { result.append(Strings.padStart(Integer.toHexString((256+b)%256), 2, '0')); } return result.toString().toUpperCase(); }
protected void runCatalogTest(String catalogFile, Reader yamlApp, Predicate<? super Application> assertion) throws Exception { Reader catalogInput = Streams.reader(new ResourceUtils(this).getResourceFromUrl(catalogFile)); String catalogContent = Streams.readFullyAndClose(catalogInput); Iterable<? extends CatalogItem<?, ?>> items = launcher.getManagementContext().getCatalog().addItems(catalogContent); try { final Application app = launcher.launchAppYaml(yamlApp); assertNoFires(app); assertTrue(assertion.apply(app)); Application newApp = rebind(); assertNoFires(newApp); assertTrue(assertion.apply(app)); } finally { for (CatalogItem<?, ?> item : items) { launcher.getManagementContext().getCatalog().deleteCatalogItem(item.getSymbolicName(), item.getVersion()); } } }
/** @deprecated since 0.7.0 use {@link Streams#copy(InputStream, OutputStream)} */ @Deprecated public static void copy(InputStream input, OutputStream output) throws IOException { Streams.copy(input, output); }
protected void customizeConfiguration() throws ExecutionException, InterruptedException, URISyntaxException { log.info("Copying creation script " + getEntity().toString()); // TODO check these semantics are what we really want? String configScriptUrl = entity.getConfig(RubyRepNode.CONFIGURATION_SCRIPT_URL); Reader configContents; if (configScriptUrl != null) { // If set accept as-is configContents = Streams.reader(resource.getResourceFromUrl(configScriptUrl)); } else { String configScriptContents = processTemplate(entity.getConfig(RubyRepNode.TEMPLATE_CONFIGURATION_URL)); configContents = Streams.newReaderWithContents(configScriptContents); } getMachine().copyTo(configContents, getRunDir() + "/rubyrep.conf"); }
/** creates a tag suitable for attaching a snapshot of an environment var map as a "stream" on a task; * mainly for use with STREAM_ENV */ public static WrappedStream tagForEnvStream(String streamEnv, Map<?, ?> env) { StringBuilder sb = new StringBuilder(); for (Map.Entry<?,?> kv: env.entrySet()) { Object val = kv.getValue(); sb.append(kv.getKey()+"=" + (val!=null ? BashStringEscapes.wrapBash(val.toString()) : "") + "\n"); } return BrooklynTaskTags.tagForStream(BrooklynTaskTags.STREAM_ENV, Streams.byteArrayOfString(sb.toString())); }