Example usage for com.google.common.io ByteSink openStream

List of usage examples for com.google.common.io ByteSink openStream

Introduction

In this page you can find the example usage for com.google.common.io ByteSink openStream.

Prototype

public abstract OutputStream openStream() throws IOException;

Source Link

Document

Opens a new OutputStream for writing to this sink.

Usage

From source file:be.fror.password.vault.core.VaultFormat.java

public static void write(ByteSink sink, Vault vault) throws IOException {
    try (OutputStream out = sink.openStream()) {
        Vault1Format.INSTANCE.write(out, vault);
    }// www .j  a va  2 s  . c o m
}

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

/**
 * Retry copy attempts from input stream to output stream. Does *not* check to make sure data was intact during the transfer
 *
 * @param byteSource  Supplier for input streams to copy from. The stream is closed on every retry.
 * @param byteSink    Supplier for output streams. The stream is closed on every retry.
 * @param shouldRetry Predicate to determine if the throwable is recoverable for a retry
 * @param maxAttempts Maximum number of retries before failing
 *//* ww w .  j a v a  2 s  .  c om*/
public static long retryCopy(final ByteSource byteSource, final ByteSink byteSink,
        final Predicate<Throwable> shouldRetry, final int maxAttempts) {
    try {
        return RetryUtils.retry(() -> {
            try (InputStream inputStream = byteSource.openStream()) {
                try (OutputStream outputStream = byteSink.openStream()) {
                    final long retval = ByteStreams.copy(inputStream, outputStream);
                    // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf
                    outputStream.flush();
                    return retval;
                }
            }
        }, shouldRetry, maxAttempts);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:io.druid.java.util.common.StreamUtils.java

/**
 * Retry copy attempts from input stream to output stream. Does *not* check to make sure data was intact during the transfer
 *
 * @param byteSource  Supplier for input streams to copy from. The stream is closed on every retry.
 * @param byteSink    Supplier for output streams. The stream is closed on every retry.
 * @param shouldRetry Predicate to determine if the throwable is recoverable for a retry
 * @param maxAttempts Maximum number of retries before failing
 *///  w  w  w. ja  v a 2 s  .  c o m
public static long retryCopy(final ByteSource byteSource, final ByteSink byteSink,
        final Predicate<Throwable> shouldRetry, final int maxAttempts) {
    try {
        return RetryUtils.retry(new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                try (InputStream inputStream = byteSource.openStream()) {
                    try (OutputStream outputStream = byteSink.openStream()) {
                        final long retval = ByteStreams.copy(inputStream, outputStream);
                        // Workarround for http://hg.openjdk.java.net/jdk8/jdk8/jdk/rev/759aa847dcaf
                        outputStream.flush();
                        return retval;
                    }
                }
            }
        }, shouldRetry, maxAttempts);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

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

/**
 * Retry copy attempts from input stream to output stream. Does *not* check to make sure data was intact during the transfer
 *
 * @param byteSource  Supplier for input streams to copy from. The stream is closed on every retry.
 * @param byteSink    Supplier for output streams. The stream is closed on every retry.
 * @param shouldRetry Predicate to determine if the throwable is recoverable for a retry
 * @param maxAttempts Maximum number of retries before failing
 *//*w w  w  .  ja  v  a  2  s.  co m*/
public static long retryCopy(final ByteSource byteSource, final ByteSink byteSink,
        final Predicate<Throwable> shouldRetry, final int maxAttempts) {
    try {
        return RetryUtils.retry(new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                InputStream inputStream = null;
                OutputStream outputStream = null;
                try {
                    inputStream = byteSource.openStream();
                    outputStream = byteSink.openStream();
                    return ByteStreams.copy(inputStream, outputStream);
                } finally {
                    CloseQuietly.close(inputStream);
                    CloseQuietly.close(outputStream);
                }
            }
        }, shouldRetry, maxAttempts);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

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

public static long gzip(final ByteSource in, final ByteSink out, Predicate<Throwable> shouldRetry)
        throws IOException {
    return StreamUtils.retryCopy(in, new ByteSink() {
        @Override/* w ww  .  ja v  a2 s . c o m*/
        public OutputStream openStream() throws IOException {
            return new GZIPOutputStream(out.openStream());
        }
    }, shouldRetry, DEFAULT_RETRY_COUNT);
}

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

public static long gzip(final ByteSource in, final ByteSink out, Predicate<Throwable> shouldRetry) {
    return StreamUtils.retryCopy(in, new ByteSink() {
        @Override/*w w w .  j  ava2s.  c  o  m*/
        public OutputStream openStream() throws IOException {
            return new GZIPOutputStream(out.openStream());
        }
    }, shouldRetry, DEFAULT_RETRY_COUNT);
}

From source file:org.richfaces.resource.optimizer.resource.writer.impl.ThroughputResourceProcessor.java

@Override
public void process(String outputName, ByteSource byteSource, ByteSink byteSink, boolean closeAtFinish)
        throws IOException {
    process(outputName, byteSource.openStream(), byteSink.openStream(), closeAtFinish);
}

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

@Override
public void closeAndConsolidate(ByteSink consolidatedOut) throws IOException {
    close();//from   w  w  w  .j a v a2 s. c om
    try (OutputStream out = consolidatedOut.openStream();
            InputStream meta = ioPeon.makeInputStream(metaFile);
            InputStream value = ioPeon.makeInputStream(valueFile)) {
        ByteStreams.copy(meta, out);
        ByteStreams.copy(value, out);
    }
}

From source file:eu.lp0.cursus.xml.common.AbstractXMLFile.java

public final void to(ByteSink out) throws ExportException {
    try {/*from www  .j a v a2s  .c  om*/
        to(out.openStream());
    } catch (IOException e) {
        throw new ExportException(e);
    }
}

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

@Override
public void closeAndConsolidate(ByteSink consolidatedOut) throws IOException {
    close();/*ww  w . j ava2  s  .c  o m*/
    try (OutputStream out = consolidatedOut.openStream(); InputStream meta = ioPeon.makeInputStream(metaFile)) {
        ByteStreams.copy(meta, out);
        ByteStreams.copy(flattener.combineStreams(), out);
    }
}