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:com.enonic.cms.framework.blob.BlobStoreHelper.java

public static BlobKey createKey(final InputStream in, final OutputStream out) throws BlobStoreException {
    try {/*from ww  w .  j a v  a2s. c  o  m*/
        final MessageDigest digest = createMessageDigest();
        final DigestOutputStream digestOut = new DigestOutputStream(out, digest);

        try {
            ByteStreams.copy(in, digestOut);
        } finally {
            digestOut.close();
            in.close();
        }

        return new BlobKey(digest.digest());
    } catch (IOException e) {
        throw new BlobStoreException("Failed to create blob key", e);
    }
}

From source file:com.google.devtools.build.lib.bazel.repository.downloader.DownloaderTestUtils.java

static void sendLines(@WillNotClose Socket socket, String... data) throws IOException {
    ByteStreams.copy(new ByteArrayInputStream(Joiner.on("\r\n").join(data).getBytes(ISO_8859_1)),
            socket.getOutputStream());//from   w  w w.  j a v a  2s . c  o  m
}

From source file:mx.bigdata.sat.cfd.CFD2Factory.java

private static CFD2 getCFD2(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteStreams.copy(in, baos);
    byte[] data = baos.toByteArray();
    if (getVersion(data).equals("2.2")) {
        return new CFDv22(new ByteArrayInputStream(data));
    } else {/*ww w. j  a  v a 2  s . c  o m*/
        return new CFDv2(new ByteArrayInputStream(data));
    }
}

From source file:mx.bigdata.sat.cfdi.CFDIFactory.java

private static CFDI getCFDI(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteStreams.copy(in, baos);
    byte[] data = baos.toByteArray();
    if (getVersion(data).equals("3.2")) {
        return new CFDv32(new ByteArrayInputStream(data));
    } else {/*from   ww w  .  j av  a  2  s  .  com*/
        return new CFDv3(new ByteArrayInputStream(data));
    }
}

From source file:org.trancecode.io.TcByteStreams.java

public static long copy(final InputStream in, final OutputStream out, final boolean close) {
    try {/*from  w w w  .j a  v a2 s  .  c  o  m*/
        final long bytes = ByteStreams.copy(in, out);
        Closeables.close(in, false);
        Closeables.close(out, false);
        return bytes;
    } catch (final IOException e) {
        throw new RuntimeIOException(e);
    } finally {
        if (close) {
            Closeables.closeQuietly(in);
            Closeables.closeQuietly(out);
        }
    }
}

From source file:org.apli.modelbeans.facturacion.cfdi.CFDIFactory.java

private static CFDI getCFDI(InputStream in) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteStreams.copy(in, baos);
    byte[] data = baos.toByteArray();
    return new CFDv32(new ByteArrayInputStream(data));
}

From source file:br.com.tecsinapse.exporter.util.ZipUtil.java

public static void addFile(ZipOutputStream zos, File file) throws IOException {
    if (file.isDirectory()) {
        ZipEntry entry = new ZipEntry(file.getName());
        zos.putNextEntry(entry);/*from   w  w w .  ja  v  a 2  s .  c  o m*/
        for (File subFile : file.listFiles()) {
            addFile(zos, subFile);
        }
        zos.closeEntry();
    }

    try (FileInputStream fis = new FileInputStream(file)) {
        ZipEntry entry = new ZipEntry(file.getName());
        zos.putNextEntry(entry);
        ByteStreams.copy(fis, zos);
        zos.closeEntry();
    }
}

From source file:org.cryptomator.filesystem.Copier.java

public static void copy(File source, File destination) {
    try (OpenFiles openFiles = DeadlockSafeFileOpener.withReadable(source).andWritable(destination).open()) {
        ReadableFile readable = openFiles.readable(source);
        WritableFile writable = openFiles.writable(destination);
        writable.truncate();//w  ww  .j  a  v  a2 s .co m
        ByteStreams.copy(readable, writable);
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

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

/**
 * Utility method: write input stream to temporary file
 *
 * @param inputStream input stream//w  w  w.  jav a 2  s.  c om
 * @return File object which points temporary file being made
 * @throws IOException
 */
public static File writeInputStreamToTempFile(InputStream inputStream, String fileNameSuffix)
        throws IOException {
    OutputStream outputStream = null;
    try {
        File tmpFile = File.createTempFile(UUID.randomUUID().toString(), fileNameSuffix);
        tmpFile.deleteOnExit();
        outputStream = new FileOutputStream(tmpFile);
        ByteStreams.copy(inputStream, outputStream);
        return tmpFile;
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException ex) {
            // swallow
        }
    }
}

From source file:org.jclouds.io.ByteStreams2.java

public static HashCode hashAndClose(InputStream input, HashFunction hashFunction) throws IOException {
    checkNotNull(input, "input");
    checkNotNull(hashFunction, "hashFunction");
    try {//from  w  w w.ja v a  2s.c om
        HashingInputStream his = new HashingInputStream(hashFunction, input);
        ByteStreams.copy(his, ByteStreams.nullOutputStream());
        return his.hash();
    } finally {
        closeQuietly(input);
    }
}