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.jclouds.glacier.util.AWSRequestSignerV4.java

private static HashCode buildHashedPayload(HttpRequest request) {
    HashingInputStream his = null;//from ww w  . ja  va2s  .  c  o  m
    try {
        his = new HashingInputStream(Hashing.sha256(),
                request.getPayload() == null ? ByteSource.empty().openStream()
                        : request.getPayload().openStream());
        ByteStreams.copy(his, ByteStreams.nullOutputStream());
        return his.hash();
    } catch (IOException e) {
        throw new HttpException("Error signing request", e);
    } finally {
        closeQuietly(his);
    }
}

From source file:com.indeed.imhotep.iql.cache.HDFSQueryCache.java

@Override
public void writeFromFile(String cachedFileName, File localFile) throws IOException {
    final OutputStream cacheStream = getOutputStream(cachedFileName);
    final InputStream fileIn = new BufferedInputStream(new FileInputStream(localFile));
    ByteStreams.copy(fileIn, cacheStream);
    fileIn.close();//from   ww  w .j  a  va 2s .c  om
    cacheStream.close();
}

From source file:com.hortonworks.streamline.common.util.WSUtils.java

public static StreamingOutput wrapWithStreamingOutput(final InputStream inputStream) {
    return new StreamingOutput() {
        public void write(OutputStream os) throws IOException, WebApplicationException {
            OutputStream wrappedOutputStream = os;
            if (!(os instanceof BufferedOutputStream)) {
                wrappedOutputStream = new BufferedOutputStream(os);
            }/*  w  ww  . ja  v a2 s.c  om*/

            ByteStreams.copy(inputStream, wrappedOutputStream);

            wrappedOutputStream.flush();
        }
    };
}

From source file:org.nickelproject.util.IoUtil.java

/**
 * Uncompress the given byte array using the gzip algorithm and return the
 * uncompressed byte array.//w  w  w  .ja v a2s . c  om
 */
public static byte[] gunzip(final byte[] pInput) throws IOException {
    final ByteArrayInputStream vByteArrayStream = new ByteArrayInputStream(pInput);
    final ByteArrayOutputStream vByteArrayOutputStream = new ByteArrayOutputStream(pInput.length);
    final InputStream vInflaterStream = new GZIPInputStream(vByteArrayStream);
    ByteStreams.copy(vInflaterStream, vByteArrayOutputStream);
    vInflaterStream.close();
    final byte[] vUnCompressedBytes = vByteArrayOutputStream.toByteArray();
    return vUnCompressedBytes;
}

From source file:com.vectrace.MercurialEclipse.commands.HgPatchClient.java

private static void copy(InputStream in, File patchFile) throws HgException {
    FileOutputStream out = null;//from w w  w.j a  v a  2  s.c o m
    try {
        out = new FileOutputStream(patchFile);
        ByteStreams.copy(in, out);
        in = null;
    } catch (FileNotFoundException e) {
        throw new HgException(e.getMessage(), e);
    } catch (IOException e) {
        throw new HgException(e.getMessage(), e);
    } finally {
        try {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    throw new HgException(e.getMessage(), e);
                }
            }
        } finally {
            if (in != null) {
                try {
                    Utils.consumeAll(in);
                } catch (IOException e) {
                    throw new HgException(e.getMessage(), e);
                }
            }
        }
    }
}

From source file:io.druid.segment.data.CompressedVSizeIntsIndexedWriter.java

@Override
public void writeToChannel(WritableByteChannel channel) throws IOException {
    channel.write(ByteBuffer.wrap(new byte[] { VERSION, (byte) numBytes }));
    channel.write(ByteBuffer.wrap(Ints.toByteArray(numInserted)));
    channel.write(ByteBuffer.wrap(Ints.toByteArray(chunkFactor)));
    channel.write(ByteBuffer.wrap(new byte[] { compression.getId() }));
    final ReadableByteChannel from = Channels.newChannel(flattener.combineStreams().getInput());
    ByteStreams.copy(from, channel);
}

From source file:org.fenixedu.bennu.core.presentationTier.actions.BaseAction.java

protected ActionForward download(final HttpServletResponse response, final String filename, InputStream stream,
        final String contentType) throws IOException {
    try (OutputStream outputStream = response.getOutputStream()) {
        response.setContentType(contentType);
        response.addHeader("Content-Disposition",
                "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
        int byteCount = (int) ByteStreams.copy(stream, outputStream);
        response.setContentLength(byteCount);
        outputStream.flush();//w  w w. j a v a 2s .  c  om
        return null;
    } finally {
        stream.close();
    }
}

From source file:org.codehaus.httpcache4j.urlconnection.URLConnectionResponseResolver.java

private void writeRequest(HTTPRequest request, HttpURLConnection connection) throws IOException {
    if (request.hasPayload()) {
        InputStream requestStream = request.getPayload().getInputStream();
        OutputStream connectionStream;
        try {//w w w  .j  ava  2 s.c om
            if (getConfiguration().isUseChunked()) {
                connection.setChunkedStreamingMode(2048);
            }
            connectionStream = connection.getOutputStream();
            ByteStreams.copy(requestStream, connectionStream);
        } finally {
            Closeables.closeQuietly(requestStream);
        }
    }
}

From source file:com.googlesource.gerrit.plugins.serverconfig.ServerConfigServlet.java

private void respondInvalidConfig(HttpServletRequest req, HttpServletResponse res, String messageTxt)
        throws IOException {
    String message = MessageFormat.format("Invalid config file {0}: {1}", req.getPathInfo(), messageTxt);
    res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    res.setContentType("application/octet-stream");
    res.setContentLength(message.length());
    byte[] bytes = message.getBytes(Charsets.UTF_8);
    OutputStream out = res.getOutputStream();
    try (ByteArrayInputStream in = new ByteArrayInputStream(bytes)) {
        ByteStreams.copy(in, out);
    }//from w ww .  j a va  2 s  . co  m
}

From source file:npanday.msbuild.xdt.XmlDocumentTransformer.java

private File extractResource(String resourceName) throws IOException {
    final URL resource = Resources.getResource(getClass(), resourceName);
    final InputSupplier<InputStream> in = Resources.newInputStreamSupplier(resource);

    final File outFile = new File(new File(workingFolder, "tmp"), resourceName);
    outFile.getParentFile().mkdirs();/*from w  ww  .  java2 s. c  o m*/
    outFile.createNewFile();
    final OutputSupplier<FileOutputStream> out = Files.newOutputStreamSupplier(outFile);

    ByteStreams.copy(in, out);

    return outFile;
}