List of usage examples for com.google.common.io ByteSource slice
public ByteSource slice(long offset, long length)
From source file:de.l3s.concatgz.util.CacheInputStream.java
public InputStream getCacheStream() throws IOException { ByteSource cachedBytes = cache.asByteSource(); InputStream cachedStream = cachedBytes.slice(0, cachedBytes.size() - bufferLeft).openBufferedStream(); return new SequenceInputStream(cachedStream, this); }
From source file:org.haiku.haikudepotserver.support.web.JobDataWriteListener.java
@Override public void onWritePossible() throws IOException { Optional<JobDataWithByteSource> jobDataWithByteSourceOptional = jobService.tryObtainData(jobDataGuid); if (!jobDataWithByteSourceOptional.isPresent()) { LOGGER.error("unable to find the job data for; " + jobDataGuid); async.complete();// w ww . j a va 2 s . co m } else { ByteSource byteSource = jobDataWithByteSourceOptional.get().getByteSource(); while (payloadOffset >= 0 && outputStream.isReady()) { ByteSource subPayloadByteStream = byteSource.slice(payloadOffset, BUFFER_SIZE); int subPayloadBufferFillLength = readToBuffer(subPayloadByteStream); if (0 == subPayloadBufferFillLength) { async.complete(); LOGGER.info("did complete async stream job data; {}", jobDataGuid); payloadOffset = -1; } else { outputStream.write(subPayloadBuffer, 0, subPayloadBufferFillLength); payloadOffset += subPayloadBufferFillLength; } } } }
From source file:org.codice.ddf.security.crl.generator.CrlGenerator.java
/** * Determines the CRL encoding and creates a CRL file. * * @param byteSource - CRL content as a byte source * @return - the created file//from w w w . ja v a 2 s. co m * @throws IOException */ private File getCrlFile(ByteSource byteSource) throws IOException { // Determine the file extension File crlFile; try (InputStream inputStream = byteSource.slice(0, 300).openStream()) { if (IOUtils.toString(inputStream, StandardCharsets.UTF_8).contains("-----BEGIN")) { crlFile = new File(crlFileLocation + PEM_CRL); } else { crlFile = new File(crlFileLocation + DEM_CRL); } } return crlFile; }
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); }//from w ww.j a va 2 s.c om // 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; }