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:org.eclipse.scada.configuration.world.lib.deployment.CommonPackageDeploymentContext.java

@Override
public void addPreInstallationScript(final Reader reader) throws IOException {
    try {//from w w w  .  j  a  va 2s .com
        CharStreams.copy(reader, this.preInstallation);
    } finally {
        reader.close();
    }
}

From source file:org.obiba.opal.core.upgrade.StoredProcedureUpgradeStep.java

@SuppressWarnings("TypeMayBeWeakened")
private String readFully(Resource script) {
    Reader reader = null;/* ww  w  .  ja  va  2  s  .  c  o  m*/
    try {
        reader = new InputStreamReader(script.getInputStream());
        StringBuilder sb = new StringBuilder();
        CharStreams.copy(reader, sb);
        return sb.toString();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException ignored) {
        }
    }
}

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

@Override
public void addPostRemovalScript(final Reader reader) throws IOException {
    try {/*w w  w. ja  v  a  2  s . c  o  m*/
        CharStreams.copy(reader, this.postRemoval);
    } finally {
        reader.close();
    }
}

From source file:eu.itesla_project.modules.histo.tools.HistoDbPrintForecastDiffTool.java

@Override
public void run(CommandLine line) throws Exception {
    OfflineConfig config = OfflineConfig.load();
    try (HistoDbClient histoDbClient = config.getHistoDbClientFactoryClass().newInstance().create()) {
        Interval interval = Interval.parse(line.getOptionValue("interval"));
        try (Reader reader = new InputStreamReader(histoDbClient.queryCsv(HistoQueryType.forecastDiff,
                EnumSet.allOf(Country.class), EnumSet.of(HistoDbEquip.loads, HistoDbEquip.gen),
                EnumSet.of(HistoDbAttr.P), interval, HistoDbHorizon.DACF, false, false))) {
            CharStreams.copy(reader, System.out);
        }//from  w ww  .  j  av  a2 s  .  c  o  m
    }
}

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

@Override
public void addPreRemovalScript(final Reader reader) throws IOException {
    try {// w w w .j a  v  a  2 s.  c  om
        CharStreams.copy(reader, this.preRemoval);
    } finally {
        reader.close();
    }
}

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

/**
 * Create a {@code GxpClosure} that will emit all the data avaliable from the
 * {@code Reader}. If the returned {@code GxpClosure} is evaluated more than
 * once, {@code reset()} will be called to reset the stream back to its origin
 * on all evaluations but the first./*from   w w w.ja  va  2  s  .co  m*/
 */
public static GxpClosure fromReader(final Reader reader) {
    Preconditions.checkNotNull(reader);
    return new GxpClosure() {
        private boolean firstCall = true;

        public void write(Appendable out, GxpContext gxpContext) throws IOException {
            if (!firstCall) {
                reader.reset();
            }
            firstCall = false;
            CharStreams.copy(reader, out);
        }
    };
}

From source file:annis.gui.requesthandler.LoginServletRequestHandler.java

private void doGet(VaadinSession session, VaadinRequest request, VaadinResponse response) {
    response.setContentType("text/html");
    OutputStream out = null;/* w w  w.  j  a  v a 2 s. c  o m*/
    try {
        out = response.getOutputStream();

        String htmlSource = Resources.toString(LoginServletRequestHandler.class.getResource("login.html"),
                Charsets.UTF_8);

        htmlSource = htmlSource.replaceAll("%usercaption%", "Username")
                .replaceAll("%passwordcaption%", "Password").replaceAll("%title%", "ANNIS Login")
                .replaceAll("%logincaption%", "Login").replaceAll("%or%", "or")
                .replaceAll("%cancelcaption%", "Cancel");

        try (OutputStreamWriter writer = new OutputStreamWriter(out, Charsets.UTF_8)) {
            CharStreams.copy(new StringReader(htmlSource), writer);
            out.flush();
        }
    } catch (IOException ex) {
        log.error(null, ex);
    } catch (Exception ex) {
        log.error(null, ex);
    } finally {
        response.setStatus(200);
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                log.error("closing OutputStream filed", ex);
            }
        }
    }
}

From source file:org.vraptor.impl.result.DefaultHttpResult.java

public HttpResult body(Reader body) {
    try {//from   ww  w  . ja va2 s . co m
        CharStreams.copy(body, response.getWriter());
    } catch (IOException e) {
        throw new ResultException("Couldn't write to response body", e);
    }
    return this;
}

From source file:brooklyn.util.stream.Streams.java

public static void copy(Reader input, Writer output) {
    try {//  w  w w  .java 2  s.  com
        CharStreams.copy(input, output);
        output.flush();
    } catch (IOException ioe) {
        throw Exceptions.propagate(ioe);
    }
}

From source file:com.facebook.buck.httpserver.TraceDataHandler.java

private void doGet(Request baseRequest, HttpServletResponse response) throws ServletException, IOException {
    String path = baseRequest.getPathInfo();
    Matcher matcher = ID_PATTERN.matcher(path);

    if (!matcher.matches()) {
        Responses.writeFailedResponse(baseRequest, response);
        return;//from  www. j ava  2s .co  m
    }

    String id = matcher.group(1);

    response.setContentType(MediaType.JAVASCRIPT_UTF_8.toString());
    response.setStatus(HttpServletResponse.SC_OK);

    boolean hasValidCallbackParam = false;
    Writer responseWriter = response.getWriter();
    String callback = baseRequest.getParameter("callback");
    if (callback != null) {
        Matcher callbackMatcher = CALLBACK_PATTERN.matcher(callback);
        if (callbackMatcher.matches()) {
            hasValidCallbackParam = true;
            responseWriter.write(callback);
            responseWriter.write("(");
        }
    }

    InputSupplier<? extends InputStream> inputSupplier = tracesHelper.getInputForTrace(id);
    try (InputStreamReader inputStreamReader = new InputStreamReader(inputSupplier.getInput())) {
        CharStreams.copy(inputStreamReader, responseWriter);
    }

    if (hasValidCallbackParam) {
        responseWriter.write(");\n");
    }

    response.flushBuffer();
    baseRequest.setHandled(true);
}