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

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

Introduction

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

Prototype

public static ByteSource concat(ByteSource... sources) 

Source Link

Document

Concatenates multiple ByteSource instances into a single source.

Usage

From source file:net.derquinse.common.io.Chunks.java

/**
 * Constructor./*from   w w  w  .j  a va  2 s.  c  o m*/
 * @param sources Backing sources, there must be at least two.
 */
Chunks(List<? extends T> sources) {
    checkNotNull(sources);
    checkArgument(sources.size() > 1, "There must be at least two chunks");
    this.sources = ImmutableList.copyOf(sources);
    int total = Ints.saturatedCast(this.sources.get(0).size());
    this.chunkSize = total;
    for (int i = 1; i < this.sources.size(); i++) {
        total += Ints.saturatedCast(this.sources.get(i).size());
    }
    this.totalSize = total;
    this.supplier = ByteSource.concat(this.sources);
}

From source file:org.nickelproject.nickel.blobStore.S3BlobStore.java

@Override
public final InputStream getAsStream(final BlobRef blobRef) {
    final ObjectMetadata metaData = getMetadata(blobRef);
    if (metaData != null) {
        long start = 0;
        final List<ByteSource> byteSources = Lists.newArrayList();
        for (; start + downloadPartSize + 1 < metaData.getInstanceLength(); start += downloadPartSize + 1) {
            byteSources.add(new FutureByteSource(
                    executor.submit(new GetPartCallable(blobRef, start, start + downloadPartSize))));
        }/*  ww w .  j a v a2  s .  c om*/
        byteSources.add(new FutureByteSource(
                executor.submit(new GetPartCallable(blobRef, start, metaData.getInstanceLength() - 1))));
        try {
            return ByteSource.concat(byteSources).openStream();
        } catch (IOException e) {
            throw RethrownException.rethrow(e);
        }
    } else {
        return null;
    }
}

From source file:io.druid.segment.writeout.ByteBufferWriteOutBytes.java

@Override
public InputStream asInputStream() throws IOException {
    checkOpen();//from   ww  w.j a va 2s  .  c o m
    Function<ByteBuffer, ByteSource> byteBufferToByteSource = buf -> new ByteSource() {
        @Override
        public InputStream openStream() {
            ByteBuffer inputBuf = buf.duplicate();
            inputBuf.flip();
            return new ByteBufferInputStream(inputBuf);
        }
    };
    return ByteSource.concat(buffers.stream().map(byteBufferToByteSource).collect(Collectors.toList()))
            .openStream();
}

From source file:com.proofpoint.event.collector.combiner.S3StorageSystem.java

private StoredObject createCombinedObjectSmall(CombinedStoredObject combinedObject) {
    ImmutableList.Builder<ByteSource> builder = ImmutableList.builder();
    List<URI> sourceParts = Lists.transform(combinedObject.getSourceParts(),
            StoredObject.GET_LOCATION_FUNCTION);
    for (URI sourcePart : sourceParts) {
        builder.add(getInputSupplier(sourcePart));
    }//from w  w w.j  a  v a 2 s. c om
    ByteSource source = ByteSource.concat(builder.build());

    File tempFile = null;
    try {
        tempFile = File.createTempFile(S3StorageHelper.getS3FileName(combinedObject.getLocation()),
                ".small.s3.data");
        source.copyTo(Files.asByteSink(tempFile));
        StoredObject result = putObject(combinedObject.getLocation(), tempFile);
        return result;
    } catch (IOException e) {
        throw Throwables.propagate(e);
    } finally {
        if (tempFile != null) {
            tempFile.delete();
        }
    }
}

From source file:org.jclouds.kinetic.strategy.internal.KineticStorageStrategyImpl.java

@Override
public Blob getBlob(final String container, final String key) {
    BlobBuilder builder = blobBuilders.get();
    builder.name(key);//from w ww .  ja va  2 s .co m
    File file = getFileForBlobKey(container, key);
    TreeMap<Long, Blob> blobs = new TreeMap<Long, Blob>();
    long fileLength = file.length();
    long currentByte = 0;
    while (currentByte < fileLength) {
        byte[] chunkContents = new byte[0];
        try {
            chunkContents = Files.asByteSource(file)
                    .slice(currentByte, KineticConstants.PROPERTY_CHUNK_SIZE_BYTES
                            - KineticConstants.PROPERTY_CHUNK_FULL_HEADER_SIZE_BYTES)
                    .read();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Chunk chunk = new Chunk(this, 0, currentByte);
        chunk.setData(chunkContents);
        chunk.processChunk();
        System.out.printf("Chunk Encoded: %s\n", Arrays.toString(chunk.getData(false)));
        System.out.printf("Chunk Decoded: %s\n", Arrays.toString(chunk.getData(true)));
        Blob chunkBlob = this.getChunkedBlob(container, key, currentByte);
        blobs.put(currentByte, chunkBlob);
        currentByte += KineticConstants.PROPERTY_CHUNK_SIZE_BYTES
                - KineticConstants.PROPERTY_CHUNK_FULL_HEADER_SIZE_BYTES;
    }
    List<ByteSource> byteSources = new ArrayList<ByteSource>();
    for (Map.Entry<Long, Blob> entry : blobs.entrySet()) {
        byteSources.add((ByteSource) (entry.getValue().getPayload().getRawContent()));
    }
    ByteSource finalByteSource = ByteSource.concat(byteSources);
    return createBlobFromByteSource(container, key, finalByteSource);
}

From source file:org.jclouds.blobstore.config.LocalBlobStore.java

@Override
public Blob getBlob(String containerName, String key, GetOptions options) {
    logger.debug("Retrieving blob with key %s from container %s", key, containerName);
    // If the container doesn't exist, an exception is thrown
    if (!storageStrategy.containerExists(containerName)) {
        logger.debug("Container %s does not exist", containerName);
        throw cnfe(containerName);
    }//  www . j a v  a  2  s  .  co  m
    // If the blob doesn't exist, a null object is returned
    if (!storageStrategy.blobExists(containerName, key)) {
        logger.debug("Item %s does not exist in container %s", key, containerName);
        return null;
    }

    Blob blob = loadBlob(containerName, key);

    if (options != null) {
        String eTag = blob.getMetadata().getETag();
        if (eTag != null) {
            eTag = maybeQuoteETag(eTag);
            if (options.getIfMatch() != null) {
                if (!eTag.equals(maybeQuoteETag(options.getIfMatch())))
                    throw returnResponseException(412);
            }
            if (options.getIfNoneMatch() != null) {
                if (eTag.equals(maybeQuoteETag(options.getIfNoneMatch())))
                    throw returnResponseException(304);
            }
        }
        if (options.getIfModifiedSince() != null) {
            Date modifiedSince = options.getIfModifiedSince();
            if (blob.getMetadata().getLastModified().before(modifiedSince)) {
                HttpResponse response = HttpResponse.builder().statusCode(304).build();
                throw new HttpResponseException(String.format("%1$s is before %2$s",
                        blob.getMetadata().getLastModified(), modifiedSince), null, response);
            }

        }
        if (options.getIfUnmodifiedSince() != null) {
            Date unmodifiedSince = options.getIfUnmodifiedSince();
            if (blob.getMetadata().getLastModified().after(unmodifiedSince)) {
                HttpResponse response = HttpResponse.builder().statusCode(412).build();
                throw new HttpResponseException(String.format("%1$s is after %2$s",
                        blob.getMetadata().getLastModified(), unmodifiedSince), null, response);
            }
        }
        blob = copyBlob(blob);

        if (options.getRanges() != null && !options.getRanges().isEmpty()) {
            long size = 0;
            ImmutableList.Builder<ByteSource> streams = ImmutableList.builder();

            // Try to convert payload to ByteSource, otherwise wrap it.
            ByteSource byteSource;
            try {
                byteSource = (ByteSource) blob.getPayload().getRawContent();
            } catch (ClassCastException cce) {
                try {
                    byteSource = ByteSource
                            .wrap(ByteStreams2.toByteArrayAndClose(blob.getPayload().openStream()));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            for (String s : options.getRanges()) {
                // HTTP uses a closed interval while Java array indexing uses a
                // half-open interval.
                long offset = 0;
                long last = blob.getPayload().getContentMetadata().getContentLength() - 1;
                if (s.startsWith("-")) {
                    offset = last - Long.parseLong(s.substring(1)) + 1;
                    if (offset < 0) {
                        offset = 0;
                    }
                } else if (s.endsWith("-")) {
                    offset = Long.parseLong(s.substring(0, s.length() - 1));
                } else if (s.contains("-")) {
                    String[] firstLast = s.split("\\-");
                    offset = Long.parseLong(firstLast[0]);
                    last = Long.parseLong(firstLast[1]);
                } else {
                    throw new IllegalArgumentException("illegal range: " + s);
                }

                if (offset >= blob.getPayload().getContentMetadata().getContentLength()) {
                    throw new IllegalArgumentException("illegal range: " + s);
                }
                if (last + 1 > blob.getPayload().getContentMetadata().getContentLength()) {
                    last = blob.getPayload().getContentMetadata().getContentLength() - 1;
                }
                streams.add(byteSource.slice(offset, last - offset + 1));
                size += last - offset + 1;
                blob.getAllHeaders().put(HttpHeaders.CONTENT_RANGE, "bytes " + offset + "-" + last + "/"
                        + blob.getPayload().getContentMetadata().getContentLength());
            }
            ContentMetadata cmd = blob.getPayload().getContentMetadata();
            blob.setPayload(ByteSource.concat(streams.build()));
            HttpUtils.copy(cmd, blob.getPayload().getContentMetadata());
            blob.getPayload().getContentMetadata().setContentLength(size);
            blob.getMetadata().setSize(size);
        }
    }
    checkNotNull(blob.getPayload(), "payload " + blob);
    return blob;
}

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

private Optional<ReadableEntry> processEntries(Predicate<CharSequence> skipPath,
        DuplicateHandler duplicateHandler, String jarPath, Collection<ReadableEntry> itemEntries) {

    if (skipPath.apply(jarPath)) {
        listener.onSkip(Optional.<Entry>absent(), itemEntries);
        return Optional.absent();
    }//www.j ava  2 s .  c  om

    if (itemEntries.size() < 2) {
        ReadableEntry entry = Iterables.getOnlyElement(itemEntries);
        listener.onWrite(entry);
        return Optional.of(entry);
    }

    DuplicateAction action = duplicateHandler.actionFor(jarPath);
    switch (action) {
    case SKIP: {
        ReadableEntry original = Iterables.get(itemEntries, 0);
        listener.onSkip(Optional.of(original), Iterables.skip(itemEntries, 1));
        return Optional.of(original);
    }

    case REPLACE: {
        ReadableEntry replacement = Iterables.getLast(itemEntries);
        listener.onReplace(Iterables.limit(itemEntries, itemEntries.size() - 1), replacement);
        return Optional.of(replacement);
    }
    case CONCAT: {
        ByteSource concat = ByteSource.concat(Iterables.transform(itemEntries, ReadableEntry.GET_CONTENTS));

        ReadableEntry concatenatedEntry = new ReadableEntry(
                NamedByteSource.create(memorySource(), jarPath, concat), jarPath);

        listener.onConcat(jarPath, itemEntries);
        return Optional.of(concatenatedEntry);
    }

    case CONCAT_TEXT: {
        ByteSource concat_text = ByteSource
                .concat(Iterables.transform(itemEntries, ReadableTextEntry.GET_CONTENTS));

        ReadableEntry concatenatedTextEntry = new ReadableEntry(
                NamedByteSource.create(memorySource(), jarPath, concat_text), jarPath);

        listener.onConcat(jarPath, itemEntries);
        return Optional.of(concatenatedTextEntry);
    }

    case THROW:
        throw new DuplicateEntryException(Iterables.get(itemEntries, 1));

    default:
        throw new IllegalArgumentException("Unrecognized DuplicateAction " + action);
    }
}