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:fr.putnami.gwt.gradle.util.ResourceUtils.java

public static File copy(String resourcePath, File target, Map<String, String> model) throws IOException {

    InputStream input = ResourceUtils.class.getResource(resourcePath).openStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    ByteStreams.copy(input, out);

    String template = new String(out.toByteArray());
    // Resources.toString(url, Charsets.UTF_8);

    if (model != null) {
        for (Map.Entry<String, String> entry : model.entrySet()) {
            template = template.replace(entry.getKey(), entry.getValue());
        }//w  w w  .jav a2 s.c o  m
    }

    target.getParentFile().mkdirs();
    FileWriter writer = new FileWriter(target);
    writer.write(template);
    writer.close();
    return target;
}

From source file:org.apache.druid.java.util.common.StreamUtils.java

/**
 * Copy from an input stream to a file (and buffer it) and close the input stream.
 * <p>//from w ww . j  ava2s . co  m
 * It is highly recommended to use FileUtils.retryCopy whenever possible, and not use a raw `InputStream`
 *
 * @param is   The input stream to copy bytes from. `is` is closed regardless of the copy result.
 * @param file The file to copy bytes to. Any parent directories are automatically created.
 *
 * @return The count of bytes written to the file
 *
 * @throws IOException
 */
public static long copyToFileAndClose(InputStream is, File file) throws IOException {
    file.getParentFile().mkdirs();
    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
        final long result = ByteStreams.copy(is, os);
        // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf
        os.flush();
        return result;
    } finally {
        is.close();
    }
}

From source file:com.broadwave.android.brut.androlib.res.decoder.ResRawStreamDecoder.java

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {/*  www  .  j  av  a2  s.  com*/
        ByteStreams.copy(in, out);
    } catch (IOException ex) {
        throw new AndrolibException("Could not decode raw stream", ex);
    }
}

From source file:org.eclipse.scada.configuration.setup.common.lib.InputStreamContentProvider.java

public InputStreamContentProvider(final InputStream resource) throws IOException {
    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {/*from   w ww .j  a  v a 2s.co m*/
        ByteStreams.copy(resource, bos);
    } finally {
        resource.close();
    }
    this.data = bos.toByteArray();
}

From source file:org.apache.sentry.provider.file.PolicyFiles.java

public static void copyToDir(FileSystem fs, Path dest, String... resources)
        throws FileNotFoundException, IOException {
    for (String resource : resources) {
        InputStream in = Resources.getResource(resource).openStream();
        FSDataOutputStream out = fs.create(new Path(dest, resource));
        long bytes = ByteStreams.copy(in, out);
        in.close();/*from  ww  w. j a v a 2  s  .co m*/
        out.hflush();
        out.close();
        LOGGER.info("Copying " + resource + " to " + dest + ", bytes " + bytes);
    }
}

From source file:ru.runa.wfe.commons.IoCommons.java

public static byte[] jarToBytesArray(File file) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    JarOutputStream jarOutputStream = new JarOutputStream(byteArrayOutputStream, new Manifest());
    File[] files = listRecursive(file);
    int symbolsToSubstract = (file.isDirectory()) ? file.getAbsolutePath().length() + 1
            : file.getAbsolutePath().length() - file.getName().length();
    for (int i = 0; i < files.length; i++) {
        JarEntry jarEntry = new JarEntry(
                files[i].getAbsolutePath().substring(symbolsToSubstract).replace("\\", "/"));
        jarEntry.setTime(files[i].lastModified());
        jarOutputStream.putNextEntry(jarEntry);
        FileInputStream fileInputStream = new FileInputStream(files[i]);
        ByteStreams.copy(fileInputStream, jarOutputStream);
        jarOutputStream.closeEntry();//from  w w w.  j a v a  2  s.co  m
    }
    jarOutputStream.close();
    return byteArrayOutputStream.toByteArray();
}

From source file:com.metamx.common.StreamUtils.java

/**
 * Copy from an input stream to a file (and buffer it) and close the input stream.
 * <p/>/*w ww .  ja va2s. c  o m*/
 * It is highly recommended to use FileUtils.retryCopy whenever possible, and not use a raw `InputStream`
 *
 * @param is   The input stream to copy bytes from. `is` is closed regardless of the copy result.
 * @param file The file to copy bytes to. Any parent directories are automatically created.
 *
 * @return The count of bytes written to the file
 *
 * @throws IOException
 */
public static long copyToFileAndClose(InputStream is, File file) throws IOException {
    file.getParentFile().mkdirs();
    try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
        return ByteStreams.copy(is, os);
    } finally {
        CloseQuietly.close(is);
    }
}

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

public static void copyToDir(FileSystem fs, Path dest, String... resources)
        throws FileNotFoundException, IOException {
    for (String resource : resources) {
        InputStream in = Resources.getResource(resource).openStream();
        FSDataOutputStream out = fs.create(new Path(dest, resource));
        long bytes = ByteStreams.copy(in, out);
        in.close();/*www.ja  v  a 2s  .co  m*/
        out.hflush();
        out.close();
        LOGGER.debug("Copying " + resource + " to " + dest + ", bytes " + bytes);
    }
}

From source file:com.google.security.zynamics.binnavi.Gui.IdaSelectionDialog.CBinExportInstaller.java

/**
 * Copies a file from the BinNavi source directory to the IDA Pro target directory.
 *
 * @param directory The target directory.
 * @param file Name of the file to copy.
 *
 * @throws FileNotFoundException Thrown if an input file could not be found.
 * @throws IOException Thrown if an output file could not be written.
 *///  w w  w  . jav a  2 s .  c om
private static void copyFile(final File directory, final String file)
        throws FileNotFoundException, IOException {
    final InputStream inFile = CMain.class.getResourceAsStream("exporters/BinExport/" + file);
    final FileOutputStream outFile = new FileOutputStream(directory.getAbsolutePath() + "/plugins/" + file);

    ByteStreams.copy(inFile, outFile);

    inFile.close();
    outFile.close();
}

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

@Async
public Future<Boolean> gunziptIt(OutputStream outputStream, InputStream inputStream) throws IOException {
    logger.debug("Start uncompressing...");
    GZIPInputStream gzis = new GZIPInputStream(inputStream);
    ByteStreams.copy(gzis, outputStream);

    outputStream.flush();// w  w  w. j a v a  2  s  .  co  m
    outputStream.close();
    gzis.close();
    inputStream.close();
    logger.debug("Finish uncompressing");
    return new AsyncResult<Boolean>(true);
}