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

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

Introduction

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

Prototype

public long getCount() 

Source Link

Document

Returns the number of bytes read.

Usage

From source file:hudson.plugins.timestamper.io.DumpTimestamps.java

private static void dump(File parent, String filename, int columns) throws IOException {
    System.out.println(filename);
    File file = new File(parent, filename);
    if (!file.isFile()) {
        System.out.println("(none)");
        return;/*from ww  w  .java 2  s.  c  o  m*/
    }
    byte[] fileContents = Files.toByteArray(file);
    CountingInputStream inputStream = new CountingInputStream(new ByteArrayInputStream(fileContents));
    List<Long> values = new ArrayList<Long>();
    while (inputStream.getCount() < fileContents.length) {
        values.add(Varint.read(inputStream));
        if (values.size() == columns) {
            System.out.println(Joiner.on('\t').join(values));
            values.clear();
        }
    }
    if (!values.isEmpty()) {
        System.out.println(Joiner.on('\t').join(values));
    }
}

From source file:org.apache.beam.sdk.testing.CoderProperties.java

@VisibleForTesting
static <T> T decode(Coder<T> coder, Coder.Context context, byte[] bytes) throws CoderException, IOException {
    @SuppressWarnings("unchecked")
    Coder<T> deserializedCoder = SerializableUtils.clone(coder);

    byte[] buffer;
    if (context == Coder.Context.NESTED) {
        buffer = new byte[bytes.length + 1];
        System.arraycopy(bytes, 0, buffer, 0, bytes.length);
        buffer[bytes.length] = 1;/*from w  w  w  .  j av a  2  s . co m*/
    } else {
        buffer = bytes;
    }

    CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(buffer));
    T value = deserializedCoder.decode(new UnownedInputStream(cis), context);
    assertThat("consumed bytes equal to encoded bytes", cis.getCount(), equalTo((long) bytes.length));
    return value;
}

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  a2 s.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: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/*from w  w  w.jav a2 s .co  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.pgptool.gui.encryption.implpgp.EncryptionServicePgpImpl.java

/**
 * @param countingStream/*from w  ww  .j a v  a 2  s .c o m*/
 *            this stream is passed for progress reporting only. Optional, if
 *            not provided then return from pIn method will be used
 */
private static void pipeStream(InputStream pIn, OutputStream pOut, int bufSize, Updater progress,
        CountingInputStream countingStream) throws IOException, UserRequestedCancellationException {
    byte[] buf = new byte[bufSize];
    long totalRead = 0;

    int len;
    while ((len = pIn.read(buf)) > 0) {
        pOut.write(buf, 0, len);

        if (countingStream == null) {
            totalRead += len;
            updateProgress(progress, totalRead);
        } else {
            updateProgress(progress, countingStream.getCount());
        }
    }
}

From source file:org.echocat.marquardt.common.Validator.java

private byte[] readSignableBytesAgainForLaterValidation(final CountingInputStream bufferedInputStream)
        throws IOException {
    final int position = (int) bufferedInputStream.getCount();
    bufferedInputStream.reset();//  w  w w.  j  ava  2 s  .c  o m
    final byte[] bytes = new byte[position];
    IOUtils.read(bufferedInputStream, bytes, 0, position);
    return bytes;
}

From source file:garmintools.files.NavigationDataFileFactory.java

private void readSection(TableOfContentsEntry entry, CountingInputStream countingInputStream,
        SectionManager.GarminBuilder sectionManagerBuilder) throws IOException {
    Preconditions.checkState(countingInputStream.getCount() == entry.fileOffset);
    InputStream sectionInputStream = ByteStreams.limit(countingInputStream, entry.actualLength);
    ByteBuffer byteBuffer = ByteBuffer.wrap(ByteStreams.toByteArray(sectionInputStream))
            .order(ByteOrder.LITTLE_ENDIAN);
    logger.info(String.format("Reading section %d", entry.sectionNumber));
    sectionManagerBuilder.addSection(entry, byteBuffer);
    Preconditions.checkState(!byteBuffer.hasRemaining(),
            String.format("Trailing input (%d of %d bytes)", byteBuffer.remaining(), entry.actualLength));
}

From source file:hudson.plugins.timestamper.io.TimeShiftsReader.java

private Map<Long, Long> readTimeShifts() throws IOException {
    if (!timeShiftsFile.isFile()) {
        return Collections.emptyMap();
    }// w  w w  .  j a  v  a2 s  . co m
    Map<Long, Long> timeShifts = new HashMap<Long, Long>();
    CountingInputStream inputStream = new CountingInputStream(
            new BufferedInputStream(new FileInputStream(timeShiftsFile)));
    boolean threw = true;
    try {
        while (inputStream.getCount() < timeShiftsFile.length()) {
            long entry = Varint.read(inputStream);
            long shift = Varint.read(inputStream);
            timeShifts.put(entry, shift);
        }
        threw = false;
    } finally {
        Closeables.close(inputStream, threw);
    }
    return timeShifts;
}

From source file:org.gradle.internal.resource.transfer.AccessorBackedExternalResource.java

@Override
public ExternalResourceReadResult<Void> withContent(Action<? super InputStream> readAction)
        throws ResourceException {
    try {/*from ww  w . j a va  2  s . c om*/
        ExternalResourceReadResponse response = accessor.openResource(name.getUri(), revalidate);
        if (response == null) {
            throw ResourceExceptions.getMissing(getURI());
        }
        try {
            CountingInputStream inputStream = new CountingInputStream(response.openStream());
            readAction.execute(inputStream);
            return ExternalResourceReadResult.of(inputStream.getCount());
        } finally {
            response.close();
        }
    } catch (IOException e) {
        throw ResourceExceptions.getFailed(name.getUri(), e);
    }
}

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);/*from  w  w w.  j a v a  2s. co  m*/
    Stopwatch stopwatch = latency.start();
    try {
        handleImpl(exchange);
    } finally {
        stopwatch.stop();
        bytesReceived.increment(input.getCount());
        bytesSent.increment(output.getCount());
    }
}