List of usage examples for com.google.common.io CountingInputStream getCount
public long getCount()
From source file:com.palantir.atlasdb.stream.AbstractExpiringStreamStore.java
protected final StreamMetadata storeBlocksAndGetFinalMetadata(ID id, InputStream stream, long duration, TimeUnit durationUnit) {/*from w ww . j a v a 2 s. com*/ // Set up for finding hash and length MessageDigest digest = Sha256Hash.getMessageDigest(); stream = new DigestInputStream(stream, digest); CountingInputStream countingStream = new CountingInputStream(stream); // Try to store the bytes to the stream and get length try { storeBlocksFromStream(id, countingStream, duration, durationUnit); } catch (IOException e) { long length = countingStream.getCount(); StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.FAILED).setLength(length) .setHash(com.google.protobuf.ByteString.EMPTY).build(); storeMetadataAndIndex(id, metadata, duration, durationUnit); log.error("Could not store stream " + id + ". Failed after " + length + " bytes.", e); throw Throwables.rewrapAndThrowUncheckedException("Failed to store stream.", e); } // Get hash and length ByteString hashByteString = ByteString.copyFrom(digest.digest()); long length = countingStream.getCount(); // Return the final metadata. StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.STORED).setLength(length) .setHash(hashByteString).build(); return metadata; }
From source file:hudson.plugins.timestamper.io.TimestampsReader.java
/** * Read the next time-stamp from the given input stream. * //from w w w . j a v a2 s . c o m * @param inputStream * @return the next time-stamp */ private Timestamp readNext(InputStream inputStream) throws IOException { CountingInputStream countingInputStream = new CountingInputStream(inputStream); long elapsedMillisDiff = Varint.read(countingInputStream); elapsedMillis += elapsedMillisDiff; millisSinceEpoch = timeShiftsReader.getTime(entry).or(millisSinceEpoch + elapsedMillisDiff); filePointer += countingInputStream.getCount(); entry++; return new Timestamp(elapsedMillis, millisSinceEpoch); }
From source file:org.gradle.internal.resource.transfer.AccessorBackedExternalResource.java
@Nullable @Override/* w ww . j a v a2 s . c o m*/ public <T> ExternalResourceReadResult<T> withContentIfPresent( Transformer<? extends T, ? super InputStream> transformer) throws ResourceException { try { ExternalResourceReadResponse response = accessor.openResource(name.getUri(), revalidate); if (response == null) { return null; } try { CountingInputStream input = new CountingInputStream(new BufferedInputStream(response.openStream())); try { T value = transformer.transform(input); return ExternalResourceReadResult.of(input.getCount(), value); } finally { input.close(); } } finally { response.close(); } } catch (IOException e) { throw ResourceExceptions.getFailed(name.getUri(), e); } }
From source file:com.palantir.atlasdb.stream.AbstractPersistentStreamStore.java
protected final StreamMetadata storeBlocksAndGetFinalMetadata(@Nullable Transaction t, long id, InputStream stream) {// ww w .j av a 2s .c o m // Set up for finding hash and length MessageDigest digest = Sha256Hash.getMessageDigest(); stream = new DigestInputStream(stream, digest); CountingInputStream countingStream = new CountingInputStream(stream); // Try to store the bytes to the stream and get length try { storeBlocksFromStream(t, id, countingStream); } catch (IOException e) { long length = countingStream.getCount(); StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.FAILED).setLength(length) .setHash(com.google.protobuf.ByteString.EMPTY).build(); storeMetadataAndIndex(id, metadata); log.error("Could not store stream " + id + ". Failed after " + length + " bytes.", e); throw Throwables.rewrapAndThrowUncheckedException("Failed to store stream.", e); } // Get hash and length ByteString hashByteString = ByteString.copyFrom(digest.digest()); long length = countingStream.getCount(); // Return the final metadata. StreamMetadata metadata = StreamMetadata.newBuilder().setStatus(Status.STORED).setLength(length) .setHash(hashByteString).build(); return metadata; }
From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java
@Override public ExternalResourceReadResult<Void> writeTo(OutputStream output) { if (!localFile.exists()) { throw ResourceExceptions.getMissing(getURI()); }/*from w ww. ja v a2 s . c o m*/ try { CountingInputStream input = new CountingInputStream(new FileInputStream(localFile)); try { IOUtils.copyLarge(input, output); } finally { input.close(); } return ExternalResourceReadResult.of(input.getCount()); } catch (IOException e) { throw ResourceExceptions.getFailed(getURI(), e); } }
From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java
@Nullable @Override/* ww w. j a v a 2 s.co m*/ public <T> ExternalResourceReadResult<T> withContentIfPresent( Transformer<? extends T, ? super InputStream> readAction) throws ResourceException { if (!localFile.exists()) { return null; } try { CountingInputStream input = new CountingInputStream( new BufferedInputStream(new FileInputStream(localFile))); try { T resourceReadResult = readAction.transform(input); return ExternalResourceReadResult.of(input.getCount(), resourceReadResult); } finally { input.close(); } } catch (IOException e) { throw ResourceExceptions.getFailed(getURI(), e); } }
From source file:org.gradle.internal.resource.transfer.AccessorBackedExternalResource.java
@Nullable @Override/*w w w .ja va 2s . c o m*/ public <T> ExternalResourceReadResult<T> withContentIfPresent(ContentAction<? extends T> readAction) throws ResourceException { try { ExternalResourceReadResponse response = accessor.openResource(name.getUri(), revalidate); if (response == null) { return null; } try { CountingInputStream stream = new CountingInputStream( new BufferedInputStream(response.openStream())); try { T value = readAction.execute(stream, response.getMetaData()); return ExternalResourceReadResult.of(stream.getCount(), value); } finally { stream.close(); } } finally { response.close(); } } catch (IOException e) { throw ResourceExceptions.getFailed(name.getUri(), e); } }
From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java
@Nullable @Override//from ww w . ja va 2 s . c o m public <T> ExternalResourceReadResult<T> withContentIfPresent(ContentAction<? extends T> readAction) throws ResourceException { if (!localFile.exists()) { return null; } try { CountingInputStream input = new CountingInputStream( new BufferedInputStream(new FileInputStream(localFile))); try { T resourceReadResult = readAction.execute(input, getMetaData()); return ExternalResourceReadResult.of(input.getCount(), resourceReadResult); } finally { input.close(); } } catch (IOException e) { throw ResourceExceptions.getFailed(getURI(), e); } }
From source file:org.gradle.internal.resource.local.LocalFileStandInExternalResource.java
public ExternalResourceReadResult<Void> withContent(Action<? super InputStream> readAction) { if (!localFile.exists()) { throw ResourceExceptions.getMissing(getURI()); }//from ww w . j a va 2 s . c o m try { CountingInputStream input = new CountingInputStream( new BufferedInputStream(new FileInputStream(localFile))); try { readAction.execute(input); } finally { input.close(); } return ExternalResourceReadResult.of(input.getCount()); } catch (IOException e) { throw ResourceExceptions.getFailed(getURI(), e); } }
From source file:org.apache.jackrabbit.oak.plugins.tika.TextExtractor.java
private String parseStringValue(ByteSource byteSource, Metadata metadata, String path) { WriteOutContentHandler handler = new WriteOutContentHandler(maxExtractedLength); long start = System.currentTimeMillis(); long size = 0; try {//from ww w . j a va2 s .c o m CountingInputStream stream = new CountingInputStream(new LazyInputStream(byteSource)); try { tika.getParser().parse(stream, handler, metadata, new ParseContext()); } finally { size = stream.getCount(); stream.close(); } } catch (LinkageError e) { // Capture and ignore errors caused by extraction libraries // not being present. This is equivalent to disabling // selected media types in configuration, so we can simply // ignore these errors. } catch (Throwable t) { // Capture and report any other full text extraction problems. // The special STOP exception is used for normal termination. if (!handler.isWriteLimitReached(t)) { parserErrorCount.incrementAndGet(); parserError.debug("Failed to extract text from a binary property: " + path + " This is a fairly common case, and nothing to" + " worry about. The stack trace is included to" + " help improve the text extraction feature.", t); return ERROR_TEXT; } } String result = handler.toString(); timeTaken.addAndGet(System.currentTimeMillis() - start); if (size > 0) { extractedTextSize.addAndGet(result.length()); extractionCount.incrementAndGet(); totalSizeRead.addAndGet(size); return result; } return null; }