Example usage for com.google.common.io CharStreams copy

List of usage examples for com.google.common.io CharStreams copy

Introduction

In this page you can find the example usage for com.google.common.io CharStreams copy.

Prototype

public static long copy(Readable from, Appendable to) throws IOException 

Source Link

Document

Copies all characters between the Readable and Appendable objects.

Usage

From source file:com.google.dart.compiler.backend.dart.DartBackend.java

private static void packageLibs(Collection<LibraryUnit> libraries, Writer w, DartCompilerContext context)
        throws IOException {
    for (LibraryUnit libUnit : libraries) {
        for (DartUnit unit : libUnit.getUnits()) {
            DartSource src = unit.getSource();
            if (src != null) {
                Reader r = context.getArtifactReader(src, "", EXTENSION_DART);
                boolean failed = true;
                try {
                    CharStreams.copy(r, w);
                    failed = false;/*from w  ww.  j  a  v a  2  s  .  c  o m*/
                } finally {
                    Closeables.close(r, failed);
                }
            }
        }
    }
}

From source file:se.softhouse.common.testlib.Streams.java

/**
 * Reads from {@code source} asynchronously. Use {@link Future#get()} to get the
 * characters read. Characters will be read using {@link Charsets#UTF_8 UTF-8}. A typical use
 * case is to avoid <a//from   w w w  . j  av a2 s.c o  m
 * href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4">deadlock
 * pitfalls</a> when launching {@link Process processes}.
 */
public static ListenableFuture<String> readAsynchronously(InputStream source) {
    final Readable sourceInUTF8 = new InputStreamReader(source, Charsets.UTF_8);
    Callable<String> reader = new Callable<String>() {
        @Override
        public String call() throws Exception {
            StringBuilder result = new StringBuilder();
            CharStreams.copy(sourceInUTF8, result);
            return result.toString();
        }
    };
    return Concurrently.run(reader);
}

From source file:org.stjs.testing.driver.StreamUtils.java

public static boolean copy(ClassLoader classLoader, String url, Writer out)
        throws IOException, URISyntaxException {
    URI uri = new URI(url);

    if (uri.getPath() == null) {
        throw new IllegalArgumentException("Wrong path in uri:" + url);
    }//  w  ww.j a v a  2 s .c  om

    InputStream is = classLoader.getResourceAsStream(uri.getPath().substring(1));
    if (is == null) {
        return false;
    }
    Reader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
    try {
        CharStreams.copy(reader, out);
        out.flush();
    } finally {
        reader.close();
    }
    return true;
}

From source file:org.eclipse.che.plugin.grunt.runner.GruntApplicationLogger.java

@Override
public void getLogs(Appendable output) throws IOException {
    try (Reader r = new InputStreamReader(new FileInputStream(logFile), "UTF-8")) {
        CharStreams.copy(r, output);
    }//  w  w w.j ava2 s  .  com
}

From source file:com.publicuhc.pluginframework.util.YamlUtil.java

/**
 * Attempts to pull a Yaml file from the JAR
 *
 * @param path the path to check for/* w ww . ja va2  s.c o  m*/
 * @param loader the classloader to look in
 * @return optional. Present if found, null if not
 *
 * @throws IOException
 * @throws InvalidConfigurationException if the file can't be parsed
 */
public static Optional<YamlConfiguration> loadYamlFromJAR(String path, ClassLoader loader)
        throws IOException, InvalidConfigurationException {
    Validate.notNull(path, "Path to file cannot be null");

    URL url = loader.getResource(path);

    if (url == null) {
        return Optional.absent();
    }

    StringBuilder builder = new StringBuilder();
    CharStreams.copy(Resources.newReaderSupplier(url, Charsets.UTF_8), builder);

    return Optional.of(loadYamlFromString(builder.toString()));
}

From source file:org.jooby.internal.AssetFormatter.java

@Override
public void format(final Object body, final Body.Writer writer) throws Exception {
    Asset asset = (Asset) body;//w w  w. j a  va  2  s  .  c  o  m
    MediaType type = asset.type();

    if (type.isText()) {
        writer.text(to -> {
            try (Reader from = new InputStreamReader(asset.stream(), writer.charset())) {
                CharStreams.copy(from, to);
            }
        });
    } else {
        writer.bytes(to -> {
            try (InputStream from = asset.stream()) {
                ByteStreams.copy(from, to);
            }
        });
    }
}

From source file:com.planet57.gshell.branding.LicenseSupport.java

@Override
@Nullable//from w ww  . jav  a 2 s.  c  o  m
public String getContent() throws IOException {
    if (uri == null) {
        return null;
    }

    StringWriter buff = new StringWriter();
    try (InputStream input = uri.toURL().openStream()) {
        CharStreams.copy(new InputStreamReader(input), buff);
    }
    return buff.toString();
}

From source file:com.bennavetta.appsite.postprocessor.SourcePostProcessor.java

@Override
public void postProcess(InputStream inStream, OutputStream outStream, Request request, Response response)
        throws IOException {
    log.debug("Processing {}", request.getURI());
    response.setContentType(MediaType.HTML_UTF_8);
    response.setCharacterEncoding(Charsets.UTF_8);
    Writer out = new OutputStreamWriter(outStream, Charsets.UTF_8);
    out.write("<html><head><title>");
    out.write(request.getURI());/*from  ww  w .j a v  a  2  s  .c om*/
    out.write("</title></head></body><pre>");
    Reader in = detector.getReader(inStream, Charsets.UTF_8.name());
    if (in == null) {
        in = new InputStreamReader(inStream, Charsets.UTF_8);
    }
    CharStreams.copy(in, out);
    out.write("</pre></body></html>");
    out.flush();
}

From source file:com.google.gxp.base.GxpClosures.java

/**
 * Create a {@code GxpClosure} that will emit all the data avaliable from the
 * {@code File} at the time the closure is evaluated.
 *//*from  w  ww. j  a  v  a 2 s .c  o  m*/
public static GxpClosure fromFile(final File file, final Charset charset) {
    Preconditions.checkNotNull(file);
    Preconditions.checkNotNull(charset);
    return new GxpClosure() {
        public void write(Appendable out, GxpContext gxpContext) throws IOException {
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis, charset);
            CharStreams.copy(isr, out);
            isr.close();
            fis.close();
        }
    };
}

From source file:org.eclipse.scada.configuration.world.lib.deployment.CommonPackageDeploymentContext.java

@Override
public void addPostInstallationScript(final Reader reader) throws IOException {
    try {//from w ww  . jav  a  2  s. c  o  m
        CharStreams.copy(reader, this.postInstallation);
    } finally {
        reader.close();
    }
}