Example usage for com.google.common.io ByteSource copyTo

List of usage examples for com.google.common.io ByteSource copyTo

Introduction

In this page you can find the example usage for com.google.common.io ByteSource copyTo.

Prototype

public long copyTo(ByteSink sink) throws IOException 

Source Link

Document

Copies the contents of this byte source to the given ByteSink .

Usage

From source file:com.google.caliper.config.CaliperConfigLoader.java

private static void tryCopyIfNeeded(ByteSource supplier, File rcFile) {
    if (!rcFile.exists()) {
        try {/* w w w.  j  a va2  s . c  o m*/
            supplier.copyTo(Files.asByteSink(rcFile));
        } catch (IOException e) {
            rcFile.delete();
        }
    }
}

From source file:net.derquinse.common.jaxrs.ByteSourceOutput.java

/** Creates an {@link StreamingOutput} from the byte source. */
public static StreamingOutput output(final ByteSource source) {
    checkSource(source);/*from w ww .j a v a2  s.  co  m*/
    return new StreamingOutput() {
        @Override
        public void write(OutputStream output) throws IOException, WebApplicationException {
            source.copyTo(output);
        }
    };
}

From source file:net.derquinse.common.io.DurableFiles.java

/**
 * Copies to a file all bytes from an {@link InputStream} supplied by a factory. The file is
 * sync'd before being closed.//from w  w  w .jav  a 2s  .c  o m
 * @param from the input factory
 * @param to the destination file
 * @throws IOException if an I/O error occurs
 */
public static void copy(ByteSource from, File to) throws IOException {
    boolean threw = true;
    FileOutputStream os = new FileOutputStream(to);
    try {
        from.copyTo(os);
        os.flush();
        os.getFD().sync();
        threw = false;
    } finally {
        Closeables.close(os, threw);
    }
}

From source file:org.locationtech.geogig.cli.Logging.java

@Nullable
private static URL getOrCreateLoggingConfigFile(final File geogigdir) {

    final File logsDir = new File(geogigdir, "log");
    if (!logsDir.exists() && !logsDir.mkdir()) {
        return null;
    }//from  w  w w  .  ja  va  2 s  .c o  m
    final File configFile = new File(logsDir, "logback.xml");
    if (configFile.exists()) {
        try {
            return configFile.toURI().toURL();
        } catch (MalformedURLException e) {
            throw Throwables.propagate(e);
        }
    }
    ByteSource from;
    final URL resource = GeogigCLI.class.getResource("logback_default.xml");
    try {
        from = Resources.asByteSource(resource);
    } catch (NullPointerException npe) {
        LOGGER.warn("Couldn't obtain default logging configuration file");
        return null;
    }
    try {
        from.copyTo(Files.asByteSink(configFile));
        return configFile.toURI().toURL();
    } catch (Exception e) {
        LOGGER.warn("Error copying logback_default.xml to {}. Using default configuration.", configFile, e);
        return resource;
    }
}

From source file:org.locationtech.geogig.cli.app.Logging.java

@Nullable
private static URL getOrCreateLoggingConfigFile(final File geogigdir) {

    final File logsDir = new File(geogigdir, "log");
    if (!logsDir.exists() && !logsDir.mkdir()) {
        return null;
    }//from   ww w  . j ava 2s.  c  o  m
    final File configFile = new File(logsDir, "logback.xml");
    if (configFile.exists()) {
        try {
            return configFile.toURI().toURL();
        } catch (MalformedURLException e) {
            throw Throwables.propagate(e);
        }
    }
    ByteSource from;
    final URL resource = CLI.class.getResource("logback_default.xml");
    try {
        from = Resources.asByteSource(resource);
    } catch (NullPointerException npe) {
        LOGGER.warn("Couldn't obtain default logging configuration file");
        return null;
    }
    try {
        from.copyTo(Files.asByteSink(configFile));
        return configFile.toURI().toURL();
    } catch (Exception e) {
        LOGGER.warn("Error copying logback_default.xml to {}. Using default configuration.", configFile, e);
        return resource;
    }
}

From source file:com.google.template.soy.base.internal.SoyJarFileWriter.java

/** Writes a single entry to the jar. */
public void writeEntry(String path, ByteSource contents) throws IOException {
    stream.putNextEntry(new ZipEntry(path));
    contents.copyTo(stream);
    stream.closeEntry();//from w  w  w .j a  v a  2s.co m
}

From source file:org.apache.airavata.gfac.jclouds.utils.JCloudsFileTransfer.java

public void downloadFilesFromEc2(String localFile, String remoteFile) throws FileTransferException {
    FileOutputStream ops;/*  w  ww  .j av  a2  s. c  o m*/
    try {
        ssh.connect();
        ops = new FileOutputStream(new File(localFile));
        Payload payload = ssh.get(remoteFile);
        InputStream inputStream = payload.openStream();
        byte[] bytes = ByteStreams.toByteArray(inputStream);
        ByteSource byteSource = ByteSource.wrap(bytes);
        byteSource.copyTo(ops);
        inputStream.close();
        ops.close();
    } catch (Exception e) {
        log.error("Error occured while downloading file from ec2");
        throw new FileTransferException(
                "Error occured while downloading file " + remoteFile + " from ec2 host");
    } finally {
        if (ssh != null)
            ssh.disconnect();
    }
}

From source file:edu.si.sidora.excel2tabular.ExcelToTabular.java

public List<File> process(final URL inputUrl) {

    final File spreadsheet = createTempFile(this);

    final ByteSource source = asByteSource(inputUrl);
    final ByteSink sink = asByteSink(spreadsheet);
    try {//from   w w  w .j  av a 2  s  .co m
        source.copyTo(sink);
    } catch (final IOException e) {
        throw new ExcelParsingException("Could not retrieve input URL: " + inputUrl, e);
    }

    try {
        final Worksheets worksheets = new Worksheets(create(spreadsheet));
        final List<File> outputs = new ArrayList<>(size(worksheets));

        log.trace("Translating sheets with data.");

        for (final FilteredSheet sheet : worksheets) {
            final File tabularFile = createTempFile(this);
            outputs.add(tabularFile);
            try (PrintStream output = new PrintStream(tabularFile)) {
                for (final Row row : sheet) {
                    output.println(new TabularRow(row, quote, delimiter));
                }
            }
        }
        spreadsheet.delete();
        return outputs;
    } catch (final InvalidFormatException e) {
        throw new ExcelParsingException(
                "Could not parse input spreadsheet because it is not in a format registered to Apache POI: "
                        + inputUrl,
                e);
    } catch (final IOException e) {
        throw new ExcelParsingException("Could not translate input spreadsheet: " + inputUrl, e);
    }
}

From source file:at.ac.univie.isc.asio.atos.FakeAtosService.java

private void send(final HttpExchange exchange, final ByteSource content) throws IOException {
    exchange.getResponseHeaders().set(HttpHeaders.CONTENT_TYPE, "application/xml");
    exchange.sendResponseHeaders(HttpStatus.SC_OK, content.size());
    content.copyTo(exchange.getResponseBody());
}

From source file:net.derquinse.common.jaxrs.ByteSourceBodyWriter.java

@Override
public void writeTo(ByteSource t, Class<?> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
        throws IOException, WebApplicationException {
    t.copyTo(entityStream);
}