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

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

Introduction

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

Prototype

public static long copy(ReadableByteChannel from, WritableByteChannel to) throws IOException 

Source Link

Document

Copies all bytes from the readable channel to the writable channel.

Usage

From source file:org.eclipse.packagedrone.repo.web.utils.ChannelCacheHandler.java

public void process(final ReadableChannel channel, final HttpServletRequest request,
        final HttpServletResponse response) throws IOException {
    if (!channel.streamCacheEntry(this.key, (cacheEntry) -> {
        response.setContentType(cacheEntry.getMimeType());
        response.setContentLengthLong(cacheEntry.getSize());
        response.setDateHeader("Last-Modified", cacheEntry.getTimestamp().toEpochMilli());
        final long len = ByteStreams.copy(cacheEntry.getStream(), response.getOutputStream());
        logger.trace("Transfered {} bytes of data from cache entry: {}", len, this.key);
    })) {/*w ww  .j  a va2s.  com*/
        logger.warn("Unable to find channel cache entry: {}", this.key);
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        response.setContentType("text/plain");
        response.getWriter().format("Unable to find: %s", request.getRequestURI());
    }
}

From source file:org.mrgeo.services.utils.ImageTestUtils.java

public static void writeBaselineImage(final InputStream stream, final String path) throws IOException {
    final OutputStream outputStream = new FileOutputStream(new File(path));
    ByteStreams.copy(stream, outputStream);
    outputStream.close();//from w  w w . j  a v a 2 s  .  co  m

}

From source file:org.apache.sentry.core.common.utils.PolicyFiles.java

public static void copyFilesToDir(FileSystem fs, Path dest, File inputFile) throws IOException {
    try (InputStream input = new FileInputStream(inputFile.getPath());
            FSDataOutputStream out = fs.create(new Path(dest, inputFile.getName()))) {
        ByteStreams.copy(input, out);
        input.close();//from   ww w .  j  av a2 s . com
        out.hflush();
        out.close();
    }
}

From source file:org.sonatype.nexus.common.io.TempStreamSupplier.java

public TempStreamSupplier(final InputStream inputStream) throws IOException {
    tempFile = Files.createTempFile("", "");
    try (OutputStream outputStream = Files.newOutputStream(tempFile)) {
        ByteStreams.copy(inputStream, outputStream);
    } finally {/* w w  w. j  av  a 2 s. com*/
        Closeables.closeQuietly(inputStream);
    }
}

From source file:com.google.devtools.build.android.desugar.io.ZipOutputFileProvider.java

@Override
public void copyFrom(String filename, InputFileProvider inputFileProvider) throws IOException {
    // TODO(bazel-team): Avoid de- and re-compressing resource files
    out.putNextEntry(inputFileProvider.getZipEntry(filename));
    try (InputStream is = inputFileProvider.getInputStream(filename)) {
        ByteStreams.copy(is, out);
    }//  ww w.j  a v a2 s  .  c o  m
    out.closeEntry();
}

From source file:com.orange.clara.cloud.servicedbdumper.filer.compression.GzipCompressing.java

@Async
public Future<Boolean> gziptIt(InputStream inputStream, OutputStream outputStream) throws IOException {
    logger.debug("Start compressing...");
    GZIPOutputStream gout = new GZIPOutputStream(outputStream);
    ByteStreams.copy(inputStream, gout);
    gout.flush();//from   www.j  a v  a 2s  .c  o  m
    gout.close();
    outputStream.flush();
    outputStream.close();
    inputStream.close();
    logger.debug("Finish compressing");
    return new AsyncResult<Boolean>(true);
}

From source file:de.nx42.maps4cim.util.Network.java

/**
 * Downloads the contents of a resource specified by the src URL to
 * the dest File. The resulting file is a byte-by-byte copy.
 * gzip transfer encoding is accepted and handled transparently, no further
 * action required.//  w w w.ja  va  2  s .  com
 * @param src the source URL to read from
 * @param dest the destination file to write to
 * @param connTimeout seconds until timeout, if no connection can be established
 * (this does NOT include download time, just a server response)
 * @param readTimeout seconds until read timeout. This is the time until
 * the server has to provide a file to transfer. Set this to a reasonably
 * high value, if you ask for computationally intensive query results from
 * the server.
 * @throws IOException if the file can't be downloaded
 * @throws SocketTimeoutException if the connection times out
 */
public static void downloadToFile(URL src, File dest, double connTimeout, double readTimeout)
        throws IOException, SocketTimeoutException, UnknownHostException {

    URLConnection conn = src.openConnection();
    conn.setRequestProperty("Accept-Encoding", "gzip");
    conn.setConnectTimeout((int) (connTimeout * 1000));
    conn.setReadTimeout((int) (readTimeout * 1000));

    InputStream in = conn.getInputStream();
    if ("gzip".equals(conn.getContentEncoding())) {
        in = new GZIPInputStream(in);
    }
    OutputStream out = new FileOutputStream(dest);

    ByteStreams.copy(in, out);

    in.close();
    out.close();
}

From source file:org.isisaddons.module.docx.dom.IoHelper.java

public ByteArrayOutputStream asBaos(String fileName) throws IOException {
    final ByteArrayInputStream bais = asBais(fileName);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteStreams.copy(bais, baos);
    return baos;/* w w  w .  j av  a2  s.co m*/
}

From source file:nbm.center.catalog.CompressedCatalogResource.java

private byte[] compress(String catalogXmlContent) throws IOException {
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    GZIPOutputStream output = new GZIPOutputStream(data);
    ByteStreams.copy(new ByteArrayInputStream(catalogXmlContent.getBytes()), output);
    output.close();// w  w  w.  j av  a2 s.  c o m
    return data.toByteArray();
}

From source file:com.palantir.util.crypto.Sha256Hash.java

/**
 * This method will read all the bytes in the stream and digest them.
 * It will attempt to close the stream as well.
 *///from  ww  w  . j  a  v a2  s.  c  om
public static Sha256Hash createFrom(InputStream is) throws IOException {
    try {
        MessageDigest digest = getMessageDigest();
        DigestInputStream digestInputStream = new DigestInputStream(is, digest);
        ByteStreams.copy(digestInputStream, ByteStreams.nullOutputStream());
        digestInputStream.close();
        return createFrom(digest);
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            /* don't throw from finally*/}
    }
}