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.orange.clara.cloud.servicedbdumper.filer.DiskFiler.java

@Override
public void store(InputStream inputStream, String filename) throws IOException {
    File dumpfile = this.getFile(filename);
    logger.debug("Storing in file " + dumpfile.getAbsolutePath());
    this.createNewDumpFile(dumpfile);
    FileOutputStream dumpFileOutputStream = new FileOutputStream(dumpfile);
    ByteStreams.copy(inputStream, dumpFileOutputStream);
    dumpFileOutputStream.flush();/*w w w  .j a va  2  s .  co m*/
    dumpFileOutputStream.close();
    inputStream.close();
    logger.debug("Store finished in file " + dumpfile.getAbsolutePath());
}

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

@Override
public void copyFrom(String filename, InputFileProvider inputFileProvider) throws IOException {
    Path path = root.resolve(filename);
    createParentFolder(path);/*w  w w  .j  a  v  a2  s . c o  m*/
    try (InputStream is = inputFileProvider.getInputStream(filename);
            OutputStream os = Files.newOutputStream(path)) {
        ByteStreams.copy(is, os);
    }
}

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

public byte[] asBytes(File file) throws IOException {
    final FileInputStream fis = new FileInputStream(file);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteStreams.copy(fis, baos);
    return baos.toByteArray();
}

From source file:io.v.android.apps.reader.PdfViewWrapper.java

/**
 * Loads the PDF file at the given path into the pdf.js component within WebView.
 *//*from   w w  w . j av  a  2 s.  c  o  m*/
public void loadPdfFile(final String fileId, final int initialPage) throws IOException {
    File pdfFile = new File(getContext().getCacheDir(), fileId);

    try (InputStream in = DB.Singleton.get(getContext()).getInputStreamForFile(fileId);
            FileOutputStream out = new FileOutputStream(pdfFile)) {
        ByteStreams.copy(in, out);
    }

    mRenderer = new PdfRenderer(ParcelFileDescriptor.open(pdfFile, ParcelFileDescriptor.MODE_READ_ONLY));

    setPage(initialPage);
}

From source file:com.gmail.tracebachi.GateKeeper.ConfigUtil.java

/***
 * Source for the majority of this method can be found at:
 * https://www.spigotmc.org/threads/bungeecords-configuration-api.11214/#post-119017
 * <p>//from w  w w. j av  a2s.c  o m
 * Originally authored by: vemacs, Feb 15, 2014
 */
static File saveResource(Plugin plugin, String resourceName, String destinationName, boolean replace) {
    File folder = plugin.getDataFolder();
    if (!folder.exists() && !folder.mkdir()) {
        return null;
    }

    File destinationFile = new File(folder, destinationName);
    try {
        if (!destinationFile.exists() || replace) {
            if (destinationFile.createNewFile()) {
                try (InputStream in = plugin.getResourceAsStream(resourceName);
                        OutputStream out = new FileOutputStream(destinationFile)) {
                    ByteStreams.copy(in, out);
                }
            } else {
                return null;
            }
        }
        return destinationFile;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.facebook.buck.artifact_cache.FullyReadOnCloseInputStream.java

@Override
public void close() throws IOException {
    if (!closed) {
        closed = true;/*from   w  w  w.j a  va 2  s.c o  m*/
        try {
            ByteStreams.copy(in, ByteStreams.nullOutputStream());
        } catch (IOException e) {
            LOGGER.info(e, "Exception when attempting to fully read the stream.");
        }
    }
    super.close();
}

From source file:org.b1.pack.cli.FsFileContent.java

@Override
public void writeTo(OutputStream stream, long start, Long end) throws IOException {
    System.out.println("Adding " + file);
    long length = (end != null ? end : file.length()) - start;
    InputSupplier<InputStream> slice = ByteStreams.slice(Files.newInputStreamSupplier(file), start, length);
    Preconditions.checkState(ByteStreams.copy(slice, stream) == length);
}

From source file:org.eclipse.scada.configuration.world.lib.utils.Helper.java

public static void createFile(final File file, final InputStream resource, final IProgressMonitor monitor,
        final boolean exec) throws Exception {
    file.getParentFile().mkdirs();/*from  w ww. j a va  2 s  .  c o m*/

    try {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            ByteStreams.copy(resource, fos);
        }
        file.setExecutable(exec);
    } finally {
        resource.close();
    }
}

From source file:com.orange.clara.cloud.servicedbdumper.fake.filer.EchoFiler.java

@Override
public void retrieve(OutputStream outputStream, String filename) throws IOException {
    InputStream is = new ByteArrayInputStream(this.text.getBytes());
    ByteStreams.copy(is, outputStream);
    this.lastTextInStream = this.text;
    try {/*from w  ww  .  ja va  2  s  .c  om*/
        outputStream.flush();
    } catch (IOException e) {
        this.lastTextInStream = "";
    }
    is.close();
    try {
        outputStream.close();
    } catch (IOException e) {
        this.lastTextInStream = "";
    }
}

From source file:com.gmail.tracebachi.DeltaRedis.Bungee.ConfigUtil.java

/***
 * Source for the majority of this method can be found at:
 * https://www.spigotmc.org/threads/bungeecords-configuration-api.11214/#post-119017
 * <p>//from www.j  ava2s . com
 * Originally authored by: vemacs, Feb 15, 2014
 */
static File saveResource(Plugin plugin, String resourceName, String destinationName, boolean replace) {
    File folder = plugin.getDataFolder();

    if (!folder.exists() && !folder.mkdir()) {
        return null;
    }

    File destinationFile = new File(folder, destinationName);

    try {
        if (!destinationFile.exists() || replace) {
            if (destinationFile.createNewFile()) {
                try (InputStream in = plugin.getResourceAsStream(resourceName);
                        OutputStream out = new FileOutputStream(destinationFile)) {
                    ByteStreams.copy(in, out);
                }
            } else {
                return null;
            }
        }

        return destinationFile;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}