All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ksyun.ks3.signer.internal.SdkDigestInputStream Maven / Gradle / Ivy


package com.ksyun.ks3.signer.internal;


import java.io.IOException;
import java.io.InputStream;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class SdkDigestInputStream extends DigestInputStream implements
        MetricAware, Releasable {
    private static final int SKIP_BUF_SIZE = 2*1024;
    public SdkDigestInputStream(InputStream stream, MessageDigest digest) {
        super(stream, digest);
    }

    @Override
    public final boolean isMetricActivated() {
        if (in instanceof MetricAware) {
            MetricAware metricAware = (MetricAware)in;
            return metricAware.isMetricActivated();
        }
        return false;
    }

    /**
     * Skips over and discards n bytes of data from this input
     * stream, while taking the skipped bytes into account for digest
     * calculation. The skip method may, for a variety of reasons,
     * end up skipping over some smaller number of bytes, possibly
     * 0. This may result from any of a number of conditions;
     * reaching end of file before n bytes have been skipped is
     * only one possibility. The actual number of bytes skipped is returned. If
     * n is negative, no bytes are skipped.
     *
     * 

* The skip method of this class creates a byte array and then * repeatedly reads into it until n bytes have been read or the * end of the stream has been reached. Subclasses are encouraged to provide * a more efficient implementation of this method. For instance, the * implementation may depend on the ability to seek. * * @param n * the number of bytes to be skipped. * @return the actual number of bytes skipped. * @exception IOException * if the stream does not support seek, or if some other I/O * error occurs. */ @Override public final long skip(final long n) throws IOException { if (n <= 0) return n; byte[] b = new byte[(int)Math.min(SKIP_BUF_SIZE, n)]; long m = n; // remaining number of bytes to read while (m > 0) { int len = read(b, 0, (int)Math.min(m, b.length)); if (len == -1) return n - m; m -= len; } assert (m == 0); return n; } @Override public final void release() { // Don't call IOUtils.release(in, null) or else could lead to infinite loop //SdkIOUtils.closeQuietly(this); by chen if (in instanceof Releasable) { // This allows any underlying stream that has the close operation // disabled to be truly released Releasable r = (Releasable)in; r.release(); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy