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

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

Introduction

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

Prototype

public abstract InputStream openStream() throws IOException;

Source Link

Document

Opens a new InputStream for reading from this source.

Usage

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

/**
 * Unzip the byteSource to the output directory. If cacheLocally is true, the byteSource is cached to local disk before unzipping.
 * This may cause more predictable behavior than trying to unzip a large file directly off a network stream, for example.
 * * @param byteSource The ByteSource which supplies the zip data
 *
 * @param byteSource   The ByteSource which supplies the zip data
 * @param outDir       The output directory to put the contents of the zip
 * @param shouldRetry  A predicate expression to determine if a new InputStream should be acquired from ByteSource and the copy attempted again
 * @param cacheLocally A boolean flag to indicate if the data should be cached locally
 *
 * @return A FileCopyResult containing the result of writing the zip entries to disk
 *
 * @throws IOException/*from  www . j  av a2  s .  c  o  m*/
 */
public static FileUtils.FileCopyResult unzip(final ByteSource byteSource, final File outDir,
        final Predicate<Throwable> shouldRetry, boolean cacheLocally) throws IOException {
    if (!cacheLocally) {
        try {
            return RetryUtils.retry(new Callable<FileUtils.FileCopyResult>() {
                @Override
                public FileUtils.FileCopyResult call() throws Exception {
                    return unzip(byteSource.openStream(), outDir);
                }
            }, shouldRetry, DEFAULT_RETRY_COUNT);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    } else {
        final File tmpFile = File.createTempFile("compressionUtilZipCache", ZIP_SUFFIX);
        try {
            FileUtils.retryCopy(byteSource, tmpFile, shouldRetry, DEFAULT_RETRY_COUNT);
            return unzip(tmpFile, outDir);
        } finally {
            if (!tmpFile.delete()) {
                log.warn("Could not delete zip cache at [%s]", tmpFile.toString());
            }
        }
    }
}

From source file:google.registry.testing.FakeServletInputStream.java

/**
 * Use a {@link ByteSource} as input for the servlet. Be sure to call {@link #close} after
 * your servlet runs so the resource opened via {@code bytes} gets closed.
 * @throws IOException/*from   www  .j  a va  2s .co  m*/
 */
public FakeServletInputStream(ByteSource bytes) throws IOException {
    this.input = bytes.openStream();
}

From source file:com.arcbees.website.server.HomeServlet.java

private Properties loadVelocityProperties() {
    Properties properties = new Properties();

    try {/*from   w w  w . j  av a 2s .  com*/
        URL url = Resources.getResource(VELOCITY_PROPERTIES);
        ByteSource byteSource = Resources.asByteSource(url);

        properties.load(byteSource.openStream());
    } catch (Exception e) {
        throw new RuntimeException("Unable to load velocity properties from '" + VELOCITY_PROPERTIES + "'.", e);
    }

    return properties;
}

From source file:com.arcbees.beestore.server.servlets.RootServlet.java

private Properties loadVelocityProperties() {
    Properties properties = new Properties();

    try {//  w w  w  . j a  v a  2s . com
        URL url = Resources.getResource(VELOCITY_PROPERTIES);
        ByteSource byteSource = Resources.asByteSource(url);

        properties.load(byteSource.openStream());
    } catch (IOException e) {
        throw new RuntimeException("Unable to load velocity properties from '" + VELOCITY_PROPERTIES + "'.", e);
    }

    return properties;
}

From source file:com.android.builder.internal.packaging.zip.utils.ByteTracker.java

/**
 * Creates a new byte source from another byte source.
 * @param source the byte source to copy data from
 * @return the tracked byte source//from  w  w  w. jav  a 2  s.co  m
 * @throws IOException failed to read data from the byte source
 */
public CloseableDelegateByteSource fromSource(@NonNull ByteSource source) throws IOException {
    return fromStream(source.openStream());
}

From source file:org.haiku.haikudepotserver.support.web.JobDataWriteListener.java

private int readToBuffer(ByteSource byteSource) throws IOException {
    try (InputStream inputStream = byteSource.openStream()) {
        int lastBytesRead;
        int bufferUpto = 0;

        do {//from w  ww  . ja v  a 2s.  c om
            lastBytesRead = inputStream.read(subPayloadBuffer, bufferUpto,
                    subPayloadBuffer.length - bufferUpto);
            bufferUpto += -1 != lastBytesRead ? lastBytesRead : 0;
        } while (bufferUpto < subPayloadBuffer.length && -1 != lastBytesRead);

        return bufferUpto;
    }
}

From source file:org.queeg.hadoop.tar.TarExtractor.java

public void extract(ByteSource source) throws IOException {
    TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(source.openStream());

    TarArchiveEntry entry;/*from www.ja v a 2  s .c om*/
    while ((entry = archiveInputStream.getNextTarEntry()) != null) {
        if (entry.isFile()) {
            BoundedInputStream entryInputStream = new BoundedInputStream(archiveInputStream, entry.getSize());
            ByteSink sink = new PathByteSink(conf, new Path(destination, entry.getName()));
            sink.writeFrom(entryInputStream);
        } else if (entry.isDirectory()) {
            ByteStreams.skipFully(archiveInputStream, entry.getSize());
            fs.mkdirs(new Path(destination, entry.getName()));
        }
    }

    archiveInputStream.close();
}

From source file:com.gwtplatform.dispatch.rest.rebind.DispatchRestRebindModule.java

@Provides
@Singleton//from  w w  w . j  av a 2s. c  o m
@VelocityProperties
Properties getVelocityProperties(Logger logger) throws UnableToCompleteException {
    Properties properties = new Properties();

    try {
        URL url = Resources.getResource(VELOCITY_PROPERTIES);
        ByteSource byteSource = Resources.asByteSource(url);

        properties.load(byteSource.openStream());
    } catch (Exception e) {
        logger.die("Unable to load velocity properties from '%s'.", e, VELOCITY_PROPERTIES);
    }

    return properties;
}

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:google.registry.tools.LoggingParameters.java

void configureLogging() throws IOException {
    ByteSource baseConfig = (configFile != null) ? Files.asByteSource(configFile.toFile()) : DEFAULT_LOG_CONFIG;
    if (logLevel != null) {
        configLines.add(".level = " + logLevel);
    }/* www  .  jav  a  2s.  com*/
    // Add an extra leading newline in case base properties file does not end in a newline.
    String customProperties = "\n" + Joiner.on('\n').join(configLines);
    ByteSource logConfig = ByteSource.concat(baseConfig, ByteSource.wrap(customProperties.getBytes()));
    try (InputStream input = logConfig.openStream()) {
        LogManager.getLogManager().readConfiguration(input);
    }
}