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:io.airlift.slice.InputStreamSliceInput.java

@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public InputStreamSliceInput(InputStream inputStream) {
    checkNotNull(inputStream, "inputStream is null");
    pushbackInputStream = new PushbackInputStream(inputStream);
    countingInputStream = new CountingInputStream(pushbackInputStream);
    dataInputStream = new LittleEndianDataInputStream(countingInputStream);
}

From source file:org.gradle.cache.internal.btree.ByteInput.java

/**
 * Starts reading from the given offset.
 *///from  w  w  w . ja v  a  2s.  c o  m
public DataInputStream start(long offset) throws IOException {
    file.seek(offset);
    bufferedInputStream.clear();
    countingInputStream = new CountingInputStream(bufferedInputStream);
    return new DataInputStream(countingInputStream);
}

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 .  j ava 2s .  co 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.hadoop.hive.ql.io.slice.InputStreamSliceInput.java

@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public InputStreamSliceInput(InputStream inputStream) {
    pushbackInputStream = new PushbackInputStream(inputStream);
    countingInputStream = new CountingInputStream(pushbackInputStream);
    dataInputStream = new LittleEndianDataInputStream(countingInputStream);
}

From source file:org.jpmml.sklearn.CompressedInputStreamStorage.java

static private InputStream initCompat(PushbackInputStream is) throws IOException {
    byte[] headerBytes = new byte[2 + 19];

    ByteStreams.readFully(is, headerBytes);

    String header = new String(headerBytes);

    if (!header.startsWith("ZF0x")) {
        throw new IOException();
    }//from w  w  w  .j  a v a 2 s  . c  o  m

    // Remove trailing whitespace
    header = header.trim();

    final long expectedSize = Long.parseLong(header.substring(4), 16);

    // Consume the first byte
    int firstByte = is.read();
    if (firstByte < 0) {
        return is;
    } // End if

    // If the first byte is not a space character, then make it available for reading again
    if (firstByte != '\u0020') {
        is.unread(firstByte);
    }

    InflaterInputStream zlibIs = new InflaterInputStream(is);

    InputStream result = new FilterInputStream(new CountingInputStream(zlibIs)) {

        private boolean closed = false;

        @Override
        public void close() throws IOException {

            if (this.closed) {
                return;
            }

            this.closed = true;

            long size = ((CountingInputStream) super.in).getCount();

            super.close();

            if (size != expectedSize) {
                throw new IOException(
                        "Expected " + expectedSize + " bytes of uncompressed data, got " + size + " bytes");
            }
        }
    };

    return result;
}

From source file:org.phoenicis.tools.archive.Zip.java

List<File> uncompressZipFile(File inputFile, File outputDir, Consumer<ProgressEntity> stateCallback) {
    try (CountingInputStream inputStream = new CountingInputStream(new FileInputStream(inputFile))) {
        final long finalSize = FileUtils.sizeOf(inputFile);
        List<File> files = uncompress(inputStream, inputStream, outputDir, finalSize, stateCallback);

        return files;
    } catch (IOException e) {
        throw new ArchiveException(ZIP_ERROR_MESSAGE, e);
    }/*from  ww w  .  j a v  a  2s. c  o m*/
}

From source file:org.locationtech.geogig.rest.repository.SendObjectResource.java

@Override
public void post(Representation entity) {
    InputStream input = null;/*  w  w  w.  j a v a  2 s .  com*/

    Request request = getRequest();
    try {
        LOGGER.info("Receiving objects from {}", request.getClientInfo().getAddress());
        Representation representation = request.getEntity();
        input = representation.getStream();
        final GeoGIG ggit = getGeogig(request).get();
        final BinaryPackedObjects unpacker = new BinaryPackedObjects(ggit.getRepository().objectDatabase());

        CountingInputStream countingStream = new CountingInputStream(input);

        Stopwatch sw = Stopwatch.createStarted();
        IngestResults ingestResults = unpacker.ingest(countingStream);
        sw.stop();

        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()));

    } catch (IOException e) {
        LOGGER.warn("Error processing incoming objects from {}", request.getClientInfo().getAddress(), e);
        throw new RestletException(e.getMessage(), Status.SERVER_ERROR_INTERNAL, e);
    } finally {
        if (input != null)
            Closeables.closeQuietly(input);
    }
}

From source file:de.dentrassi.rpm.RpmInputStream.java

public RpmInputStream(final InputStream in) {
    this.count = new CountingInputStream(in);
    this.in = new DataInputStream(this.count);
}

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

/**
 * Deserializes and validates signed Signables.
 *
 * @param content Serialized Signable including Signature.
 * @param signableDeserializingFactory Factory to deserialize Signable with.
 * @param publicKeyProvider to return matching public key for given signable.
 * @param <T> Type of your Signable. Also Certificate uses this.
 * @return Deserialized and validated Signable.
 *
 * @throws SignatureValidationFailedException If the signature cannot be read or no key is provided to check.
 * @throws IllegalArgumentException when Signable cannot be deserialized from content using the provided factory or
 * no Signature can be extracted from provided content.
 *///from ww w. ja v a2 s. c  o  m
@Nonnull
public <T extends Signable> T deserializeAndValidate(final byte[] content,
        final DeserializingFactory<T> signableDeserializingFactory,
        final Function<T, PublicKey> publicKeyProvider) {
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(content);
    try {
        final CountingInputStream bufferedInputStream = new CountingInputStream(inputStream);
        try {
            bufferedInputStream.mark(0);
            final T signable = signableDeserializingFactory.consume(bufferedInputStream);

            final byte[] signableBytes = readSignableBytesAgainForLaterValidation(bufferedInputStream);

            final PublicKey publicKey = publicKeyProvider.apply(signable);
            if (publicKey == null) {
                throw new SignatureValidationFailedException("no public key provided");
            }
            final int signatureLength = InputStreamUtils.readInt(bufferedInputStream);
            final Signature signature = new Signature(
                    InputStreamUtils.readBytes(bufferedInputStream, signatureLength));
            if (signature.isValidFor(signableBytes, publicKey)) {
                return signable;
            }
            throw new SignatureValidationFailedException("signature is invalid for provided public key");
        } finally {
            IOUtils.closeQuietly(bufferedInputStream);
        }
    } catch (final IOException e) {
        throw new IllegalArgumentException("Signable cannot be deserialized using "
                + signableDeserializingFactory.getClass() + " or content is wrong / contains no signature.", e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:io.druid.data.input.impl.prefetch.RetryingInputStream.java

RetryingInputStream(T object, ObjectOpenFunction<T> objectOpenFunction, Predicate<Throwable> retryCondition,
        int maxRetry) throws IOException {
    this.object = object;
    this.objectOpenFunction = objectOpenFunction;
    this.retryCondition = retryCondition;
    this.maxRetry = maxRetry;
    this.delegate = new CountingInputStream(objectOpenFunction.open(object));
}