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

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

Introduction

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

Prototype

public CountingInputStream(InputStream in) 

Source Link

Document

Wraps another input stream, counting the number of bytes read.

Usage

From source file:org.apache.jackrabbit.oak.spi.blob.stats.StatsCollectingStreams.java

public static InputStream wrap(final BlobStatsCollector collector, final String blobId, InputStream in) {
    final CountingInputStream cin = new CountingInputStream(in);
    return new FilterInputStream(cin) {
        final long startTime = System.nanoTime();

        @Override/*w ww. j  a  v a  2 s .  c  o  m*/
        public void close() throws IOException {
            super.close();
            //We rely on close to determine how much was downloaded
            //as once an InputStream is exposed its not possible to
            //determine if the stream is actually used

            //Download time might not be accurate as reading code might
            //be processing also as it moved further in stream. So that
            //overhead would add to the download time

            collector.downloaded(blobId, System.nanoTime() - startTime, TimeUnit.NANOSECONDS, cin.getCount());
            collector.downloadCompleted(blobId);
        }
    };
}

From source file:org.jabylon.log.viewer.pages.util.LogTail.java

public void nextChunk(int maxLines, Deque<String> buffer) {
    BufferedReader reader = null;
    try {/*from  w  w w  . j  a va 2 s .  c o m*/
        CountingInputStream in = new CountingInputStream(new FileInputStream(logFile));
        //buffer of 1 is slow, but at least predictable, so we can reset
        reader = new BufferedReader(new InputStreamReader(in), 1);
        reader.skip(currentChunk);
        String s = null;
        int lines = 0;
        while ((s = reader.readLine()) != null) {
            buffer.add(s);
            lines++;
            //unless it's the first chunk we stop once we reached max lines
            if (currentChunk > 0 && lines == maxLines)
                break;
        }
        currentChunk = in.getCount();
    } catch (FileNotFoundException e) {
        LOG.warn("Logfile does not seem to exist (yet)", e);
    } catch (IOException e) {
        LOG.warn("Failed to read logfile", e);
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            LOG.error("Failed to close the logfile", e);
        }
    }
}

From source file:org.archive.io.arc.UncompressedARCReader.java

public UncompressedARCReader(final String f, final InputStream is, final boolean atFirstRecord)
        throws IOException {
    // Arc file has been tested for existence by time it has come
    // to here.//www .ja  v  a  2  s  .  co  m
    setIn(new CountingInputStream(is));
    setCompressed(true);
    setAlignedOnFirstRecord(atFirstRecord);
    initialize(f);
}

From source file:org.archive.io.warc.UncompressedWARCReader.java

public UncompressedWARCReader(final String f, final InputStream is, final boolean atFirstRecord)
        throws IOException {
    // Arc file has been tested for existence by time it has come
    // to here.//from w w w  .  j av  a2s .  c o  m
    setIn(new CountingInputStream(is));
    setCompressed(true);
    initialize(f);
}

From source file:org.apache.bookkeeper.mledger.offload.jcloud.impl.DataBlockHeaderImpl.java

public static DataBlockHeader fromStream(InputStream stream) throws IOException {
    CountingInputStream countingStream = new CountingInputStream(stream);
    DataInputStream dis = new DataInputStream(countingStream);
    int magic = dis.readInt();
    if (magic != MAGIC_WORD) {
        throw new IOException(
                "Data block header magic word not match. read: " + magic + " expected: " + MAGIC_WORD);
    }//from  w ww  .  j  a  v a2s .  co m

    long headerLen = dis.readLong();
    long blockLen = dis.readLong();
    long firstEntryId = dis.readLong();
    long toSkip = headerLen - countingStream.getCount();
    if (dis.skip(toSkip) != toSkip) {
        throw new EOFException("Header was too small");
    }

    return new DataBlockHeaderImpl(headerLen, blockLen, firstEntryId);
}

From source file:com.netflix.servo.example.BaseHandler.java

public void handle(HttpExchange exchange) throws IOException {
    CountingInputStream input = new CountingInputStream(exchange.getRequestBody());
    CountingOutputStream output = new CountingOutputStream(exchange.getResponseBody());
    exchange.setStreams(input, output);//  w  ww  . j  av a  2  s.  c om
    Stopwatch stopwatch = latency.start();
    try {
        handleImpl(exchange);
    } finally {
        stopwatch.stop();
        bytesReceived.increment(input.getCount());
        bytesSent.increment(output.getCount());
    }
}

From source file:org.locationtech.geogig.spring.service.LegacySendObjectService.java

public SendObject sendObject(RepositoryProvider provider, String repoName, InputStream request) {
    final SendObject sendObject = new SendObject();
    final Repository repository = getRepository(provider, repoName);
    final BinaryPackedObjects unpacker = new BinaryPackedObjects(repository.objectDatabase());

    CountingInputStream countingStream = new CountingInputStream(request);

    Stopwatch sw = Stopwatch.createStarted();
    BinaryPackedObjects.IngestResults ingestResults = unpacker.ingest(countingStream);
    sw.stop();/*from  ww w.  j  a  va2  s.  c  om*/
    sendObject.setExisting(ingestResults.getExisting()).setInserted(ingestResults.getInserted());
    LOGGER.info(String.format(
            "SendObjectResource: Processed %,d objects.\nInserted: %,d.\nExisting: %,d.\nTime to process: %s.\nStream size: %,d bytes.\n",
            ingestResults.total(), ingestResults.getInserted(), ingestResults.getExisting(), sw,
            countingStream.getCount()));
    return sendObject;
}

From source file:com.google.appengine.tools.mapreduce.inputs.LineInputStream.java

LineInputStream(InputStream in, long lengthToRead, byte separator) {
    this.in = new CountingInputStream(in);
    this.lengthToRead = lengthToRead;
    this.separator = (separator & 0xff);
}

From source file:org.sonatype.nexus.blobstore.file.internal.MetricsInputStream.java

public MetricsInputStream(final InputStream input) {
    this(new CountingInputStream(input), createSha1());
}

From source file:garmintools.files.NavigationDataFileFactory.java

public GarminNavigationDataFile createFromGarmin(InputStream inputStream, long inputFileLength)
        throws IOException {
    CountingInputStream countingInputStream = new CountingInputStream(inputStream);
    SectionManager.GarminBuilder sectionManagerBuilder = new SectionManager.GarminBuilder();
    readSection(MetadataGarminAdapter.METADATA_TOC_ENTRY, countingInputStream, sectionManagerBuilder);
    sectionManagerBuilder.readTableOfContents(countingInputStream, Ints.checkedCast(inputFileLength));
    Collection<TableOfContentsEntry> tocEntries = ((TableOfContentsSection) sectionManagerBuilder
            .getSection(Ids.TABLE_OF_CONTENTS_SECTION)).getEntryMap().values();
    for (TableOfContentsEntry entry : tocEntries) {
        readSection(entry, countingInputStream, sectionManagerBuilder);
    }//from   w  w w .  j a  va 2s  .c  om
    return new GarminNavigationDataFile(sectionManagerBuilder.build());
}