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

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

Introduction

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

Prototype

protected ByteSource() 

Source Link

Document

Constructor for use by subclasses.

Usage

From source file:com.google.devtools.build.lib.vfs.FileSystem.java

/**
 * Returns the MD5 digest of the file denoted by {@code path}. See
 * {@link Path#getMD5Digest} for specification.
 *//*  ww  w .j av  a2  s .  co m*/
protected byte[] getMD5Digest(final Path path) throws IOException {
    // Naive I/O implementation.  Subclasses may (and do) optimize.
    // This code is only used by the InMemory or Zip or other weird FSs.
    return new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
            return getInputStream(path);
        }
    }.hash(Hashing.md5()).asBytes();
}

From source file:com.google.devtools.build.lib.vfs.FileSystem.java

/**
 * Returns the MD5 digest of the file denoted by {@code path}. See
 * {@link Path#getMD5Digest} for specification.
 *//*from   w w w. j a va2  s  .co  m*/
protected byte[] getSHA1Digest(final Path path) throws IOException {
    // Naive I/O implementation.  TODO(olaola): optimize!
    return new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
            return getInputStream(path);
        }
    }.hash(Hashing.sha1()).asBytes();
}

From source file:com.google.devtools.build.lib.vfs.FileSystemUtils.java

public static ByteSource asByteSource(final Path path) {
    return new ByteSource() {
        @Override/*from w  w w.  j  a v a2  s  .  c  o m*/
        public InputStream openStream() throws IOException {
            return path.getInputStream();
        }
    };
}

From source file:org.opendaylight.yangtools.yang.parser.impl.YangParserImpl.java

@Override
public Map<InputStream, Module> parseYangModelsFromStreamsMapped(
        final Collection<InputStream> yangModelStreams) {
    if (yangModelStreams == null || yangModelStreams.isEmpty()) {
        return Collections.emptyMap();
    }/*from   w w  w.  j a  v  a  2  s.  c  o  m*/

    Map<ByteSource, InputStream> sourceToStream = new HashMap<>();
    for (final InputStream stream : yangModelStreams) {
        ByteSource source = new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                return NamedByteArrayInputStream.create(stream);
            }
        };
        sourceToStream.put(source, stream);
    }

    Map<ByteSource, Module> sourceToModule;
    try {
        sourceToModule = parseYangModelSources(sourceToStream.keySet(), null);
    } catch (IOException | YangSyntaxErrorException e) {
        throw new YangParseException("Failed to parse yang data", e);
    }
    Map<InputStream, Module> result = new LinkedHashMap<>();
    for (Map.Entry<ByteSource, Module> entry : sourceToModule.entrySet()) {
        result.put(sourceToStream.get(entry.getKey()), entry.getValue());
    }
    return result;
}

From source file:io.druid.indexing.overlord.RemoteTaskRunner.java

@Override
public Optional<ByteSource> streamTaskLog(final String taskId, final long offset) {
    final ZkWorker zkWorker = findWorkerRunningTask(taskId);

    if (zkWorker == null) {
        // Worker is not running this task, it might be available in deep storage
        return Optional.absent();
    } else {/*from   w w w .j a  v a2s  . c om*/
        // Worker is still running this task
        final URL url = makeWorkerURL(zkWorker.getWorker(),
                String.format("/task/%s/log?offset=%d", taskId, offset));
        return Optional.<ByteSource>of(new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                try {
                    return httpClient.go(new Request(HttpMethod.GET, url), new InputStreamResponseHandler())
                            .get();
                } catch (InterruptedException e) {
                    throw Throwables.propagate(e);
                } catch (ExecutionException e) {
                    // Unwrap if possible
                    Throwables.propagateIfPossible(e.getCause(), IOException.class);
                    throw Throwables.propagate(e);
                }
            }
        });
    }
}

From source file:org.ambraproject.rhino.service.impl.ArticleCrudServiceImpl.java

@Override
public Archive repack(ArticleIngestionIdentifier ingestionId) {
    ArticleIngestion ingestion = readIngestion(ingestionId);
    @SuppressWarnings("unchecked")
    List<ArticleFile> files = hibernateTemplate.execute(session -> {
        Query query = session.createQuery("FROM ArticleFile WHERE ingestion = :ingestion");
        query.setParameter("ingestion", ingestion);
        return (List<ArticleFile>) query.list();
    });//w w w  . ja v a2  s .c  o m

    Map<String, ByteSource> archiveMap = files.stream()
            .collect(Collectors.toMap(ArticleFile::getIngestedFileName, (ArticleFile file) -> new ByteSource() {
                @Override
                public InputStream openStream() throws IOException {
                    return contentRepoService.getRepoObject(file.getCrepoVersion());
                }
            }));

    return Archive.pack(extractFilenameStub(ingestionId.getDoiName()) + ".zip", archiveMap);
}

From source file:org.pantsbuild.tools.jar.JarBuilder.java

private static ByteSource manifestSupplier(final Manifest mf) {
    return new ByteSource() {
        @Override//from   w  w w.j av a2  s  . c  om
        public InputStream openStream() throws IOException {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            mf.write(out);
            return new ByteArrayInputStream(out.toByteArray());
        }
    };
}

From source file:io.druid.indexing.overlord.ForkingTaskRunner.java

@Override
public Optional<ByteSource> streamTaskLog(final String taskid, final long offset) {
    final ProcessHolder processHolder;

    synchronized (tasks) {
        final ForkingTaskRunnerWorkItem taskWorkItem = tasks.get(taskid);
        if (taskWorkItem != null && taskWorkItem.processHolder != null) {
            processHolder = taskWorkItem.processHolder;
        } else {//from ww w .  java2 s  . c  o  m
            return Optional.absent();
        }
    }

    return Optional.<ByteSource>of(new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
            return LogUtils.streamFile(processHolder.logFile, offset);
        }
    });
}

From source file:com.google.devtools.build.lib.ideinfo.AndroidStudioInfoAspect.java

private static BinaryFileWriteAction makeProtoWriteAction(ActionOwner actionOwner, final MessageLite message,
        Artifact artifact) {//from  w  w  w .  ja  v  a 2s  .co  m
    return new BinaryFileWriteAction(actionOwner, artifact, new ByteSource() {
        @Override
        public InputStream openStream() throws IOException {
            return message.toByteString().newInput();
        }
    }, /*makeExecutable =*/ false);
}

From source file:org.apache.druid.indexing.overlord.RemoteTaskRunner.java

@Override
public Optional<ByteSource> streamTaskLog(final String taskId, final long offset) {
    final ZkWorker zkWorker = findWorkerRunningTask(taskId);

    if (zkWorker == null) {
        // Worker is not running this task, it might be available in deep storage
        return Optional.absent();
    } else {//from   ww w  .j  av  a 2 s. c  o m
        // Worker is still running this task
        final URL url = TaskRunnerUtils.makeWorkerURL(zkWorker.getWorker(),
                "/druid/worker/v1/task/%s/log?offset=%d", taskId, offset);
        return Optional.of(new ByteSource() {
            @Override
            public InputStream openStream() throws IOException {
                try {
                    return httpClient.go(new Request(HttpMethod.GET, url), new InputStreamResponseHandler())
                            .get();
                } catch (InterruptedException e) {
                    throw Throwables.propagate(e);
                } catch (ExecutionException e) {
                    // Unwrap if possible
                    Throwables.propagateIfPossible(e.getCause(), IOException.class);
                    throw Throwables.propagate(e);
                }
            }
        });
    }
}