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

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

Introduction

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

Prototype

long mark

To view the source code for com.google.common.io CountingInputStream mark.

Click Source Link

Usage

From source file:org.archive.io.GZIPMembersInputStream.java

/**
 * A CountingInputStream is inserted to read compressed-offsets. 
 * //  ww  w. j  a va2 s . c om
 * @param in stream to wrap
 * @param lookback tolerance of initial mark
 * @return original stream wrapped in CountingInputStream
 * @throws IOException
 */
protected static InputStream countingStream(InputStream in, int lookback) throws IOException {
    CountingInputStream cin = new CountingInputStream(in);
    cin.mark(lookback);
    return cin;
}

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  w ww  .j a va2s  . c  om
@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);
    }
}