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.jooby.internal.AssetFormatter.java

@Override
public void format(final Object body, final Body.Writer writer) throws Exception {
    Asset asset = (Asset) body;/*from w  ww. j  a v  a 2s.  co m*/
    MediaType type = asset.type();

    if (type.isText()) {
        writer.text(to -> {
            try (Reader from = new InputStreamReader(asset.stream(), writer.charset())) {
                CharStreams.copy(from, to);
            }
        });
    } else {
        writer.bytes(to -> {
            try (InputStream from = asset.stream()) {
                ByteStreams.copy(from, to);
            }
        });
    }
}

From source file:br.com.caelum.vraptor.interceptor.download.InputStreamDownload.java

public void write(HttpServletResponse response) throws IOException {
    writeDetails(response);//from   w  w  w.  j av a  2 s. c om

    OutputStream out = response.getOutputStream();
    ByteStreams.copy(stream, out);

    stream.close();
}

From source file:org.cryptomator.frontend.webdav.jackrabbitservlet.DavFileWithUnsatisfiableRange.java

@Override
public void spool(OutputContext outputContext) throws IOException {
    outputContext.setModificationTime(node.lastModified().toEpochMilli());
    if (!outputContext.hasStream()) {
        return;//from ww  w.j  a va 2 s  .  co  m
    }
    try (ReadableFile src = node.openReadable(); OutputStream out = outputContext.getOutputStream()) {
        final long contentLength = src.size();
        outputContext.setContentLength(contentLength);
        outputContext.setProperty(HttpHeader.CONTENT_RANGE.asString(), "bytes */" + contentLength);
        ByteStreams.copy(src, Channels.newChannel(out));
    }
}

From source file:org.anarres.qemu.image.QEmuImage.java

/**
 * Creates this image./*  w w w .  j a v  a  2  s.  c om*/
 *
 * @param format The image format for the new image.
 * @param size The virtual size of the new image.
 */
public void create(@Nonnull QEmuImageFormat format, @Nonnegative long size) throws IOException {
    ProcessBuilder builder = new ProcessBuilder("qemu-img", "create", "-f", format.name(),
            file.getAbsolutePath(), String.valueOf(size));
    Process process = builder.start();
    ByteStreams.copy(process.getInputStream(), System.err);
}

From source file:org.eclipse.packagedrone.repo.aspect.aggregate.AggregationContext.java

/**
 * Create a persisted cache entry/*  w w  w  .j  a  va  2 s  . com*/
 *
 * @param id
 *            the id of the cache entry, must be unique for this aspect and
 *            channel
 * @param stream
 *            the stream providing the data for the cache entry
 * @throws IOException
 */
public default void createCacheEntry(final String id, final String name, final String mimeType,
        final InputStream stream) throws IOException {
    createCacheEntry(id, name, mimeType, (output) -> ByteStreams.copy(stream, output));
}

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

@Override
public void retrieve(OutputStream outputStream, String filename) throws IOException {
    File dumpfile = this.getFile(filename);
    logger.debug("Retrieving from file " + dumpfile.getAbsolutePath());
    InputStream dumpFileInputStream = Files.asByteSource(dumpfile).openStream();
    ByteStreams.copy(dumpFileInputStream, outputStream);
    outputStream.flush();//from  www .  j a v  a2s .  co m
    dumpFileInputStream.close();
    outputStream.close();
    logger.debug("Retrieve finished file " + dumpfile.getAbsolutePath());
}

From source file:org.envirocar.server.rest.SchemaServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String file = req.getRequestURI().replace(req.getContextPath(), "").replace(req.getServletPath(), "");
    InputStream is = null;/*from  www.j  av a  2  s  . c  o m*/
    try {
        is = SchemaServlet.class.getResourceAsStream("/schema" + file);
        if (is == null) {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
        } else {
            resp.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
            ByteStreams.copy(is, resp.getOutputStream());
        }
    } finally {
        Closeables.closeQuietly(is);
    }
}

From source file:br.com.caelum.vraptor.observer.download.InputStreamDownload.java

@Override
public void write(HttpServletResponse response) throws IOException {
    writeDetails(response);//from ww w. j  ava2  s  .  co  m

    OutputStream out = response.getOutputStream();
    ByteStreams.copy(stream, out);
}

From source file:com.codeminders.socketio.protocol.EngineIOProtocol.java

public static void binaryEncode(EngineIOPacket packet, OutputStream os) throws IOException {
    if (packet.getBinaryData() != null) {
        ByteArrayInputStream bytes;
        InputStream is = packet.getBinaryData();
        if (is instanceof ByteArrayInputStream) {
            bytes = (ByteArrayInputStream) is;
        } else {/*from ww w . j a  v  a2 s .  c o  m*/
            // Cannot avoid double copy. The protocol requires to send the length before the data
            //TODO: ask user to provide length? Could be useful to send files
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            ByteStreams.copy(is, buffer);
            bytes = new ByteArrayInputStream(buffer.toByteArray());
        }
        os.write(1); // binary packet
        os.write(encodeLength(bytes.available() + 1)); // +1 for packet type
        os.write(255);
        os.write(packet.getType().value());
        ByteStreams.copy(bytes, os);
    } else {
        assert (packet.getTextData() != null);

        os.write(0); // text packet
        os.write(encodeLength(packet.getTextData().length() + 1)); // +1 for packet type
        os.write(255);
        os.write(packet.getType().value() + '0');
        os.write(packet.getTextData().getBytes("UTF-8"));
    }

}

From source file:org.openscada.protocol.ngp.extension.bzip2.Bzip2FrameCompressionFilter.java

private IoBuffer compress(final IoBuffer data) throws IOException {
    final ByteArrayInputStream input = new ByteArrayInputStream(data.array());
    final ByteArrayOutputStream output = new ByteArrayOutputStream();
    final BZip2OutputStream bzip2 = new BZip2OutputStream(output);

    ByteStreams.copy(input, bzip2);
    bzip2.close();//from w w w. j  av a2 s. c  om

    final IoBuffer result = IoBuffer.wrap(output.toByteArray());
    return result;
}