Android Open Source - qiniu-android Etag






From Project

Back to project page qiniu-android.

License

The source code is released under:

MIT License

If you think the Android project qiniu-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.qiniu.android.utils;
//from   w w w.  jav a  2 s .c  om
import com.qiniu.android.common.Config;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public final class Etag {
    public static String data(byte[] data, int offset, int length) {
        try {
            return stream(new ByteArrayInputStream(data, offset, length), length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // never reach
        return null;
    }

    public static String data(byte[] data) {
        return data(data, 0, data.length);
    }

    public static String file(File file) throws IOException {
        FileInputStream fi = new FileInputStream(file);
        return stream(fi, file.length());
    }

    public static String file(String filePath) throws IOException {
        File f = new File(filePath);
        return file(f);
    }

    public static String stream(InputStream in, long len) throws IOException {
        if (len == 0) {
            return "Fto5o-5ea0sNMlW_75VgGJCv2AcJ";
        }
        byte[] buffer = new byte[64 * 1024];
        byte[][] blocks = new byte[(int) (len + Config.BLOCK_SIZE - 1) / Config.BLOCK_SIZE][];
        for (int i = 0; i < blocks.length; i++) {
            long left = len - (long) Config.BLOCK_SIZE * i;
            long read = left > Config.BLOCK_SIZE ? Config.BLOCK_SIZE : left;
            blocks[i] = oneBlock(buffer, in, (int) read);
        }
        return resultEncode(blocks);
    }

    private static byte[] oneBlock(byte[] buffer, InputStream in, int len) throws IOException {
        MessageDigest sha1 = null;
        try {
            sha1 = MessageDigest.getInstance("sha-1");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            //never reach
            return null;
        }
        int buffSize = buffer.length;
        while (len != 0) {
            int next = buffSize > len ? len : buffSize;
            //noinspection ResultOfMethodCallIgnored
            in.read(buffer, 0, next);
            sha1.update(buffer, 0, next);
            len -= next;
        }

        return sha1.digest();
    }

    private static String resultEncode(byte[][] sha1s) {
        byte head = 0x16;
        byte[] finalHash = sha1s[0];
        int len = finalHash.length;
        byte[] ret = new byte[len + 1];
        if (sha1s.length != 1) {
            head = (byte) 0x96;
            MessageDigest sha1 = null;
            try {
                sha1 = MessageDigest.getInstance("sha-1");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                // never reach
                return null;
            }
            for (byte[] s : sha1s) {
                sha1.update(s);
            }
            finalHash = sha1.digest();
        }
        ret[0] = head;
        System.arraycopy(finalHash, 0, ret, 1, len);
        return UrlSafeBase64.encodeToString(ret);
    }
}




Java Source Code List

com.qiniu.android.Base64Test.java
com.qiniu.android.CrcTest.java
com.qiniu.android.EtagTest.java
com.qiniu.android.FormUploadTest.java
com.qiniu.android.HttpTest.java
com.qiniu.android.ResumeUploadTest.java
com.qiniu.android.TempFile.java
com.qiniu.android.TestConfig.java
com.qiniu.android.TestFileRecorder.java
com.qiniu.android.common.Config.java
com.qiniu.android.http.ByteArrayEntity.java
com.qiniu.android.http.CompletionHandler.java
com.qiniu.android.http.HttpManager.java
com.qiniu.android.http.PostArgs.java
com.qiniu.android.http.ProgressHandler.java
com.qiniu.android.http.ResponseHandler.java
com.qiniu.android.http.ResponseInfo.java
com.qiniu.android.storage.FormUploader.java
com.qiniu.android.storage.KeyGenerator.java
com.qiniu.android.storage.Recorder.java
com.qiniu.android.storage.ResumeUploader.java
com.qiniu.android.storage.UpCancellationSignal.java
com.qiniu.android.storage.UpCompletionHandler.java
com.qiniu.android.storage.UpProgressHandler.java
com.qiniu.android.storage.UploadManager.java
com.qiniu.android.storage.UploadOptions.java
com.qiniu.android.storage.persistent.FileRecorder.java
com.qiniu.android.utils.AsyncRun.java
com.qiniu.android.utils.Crc32.java
com.qiniu.android.utils.Etag.java
com.qiniu.android.utils.StringUtils.java
com.qiniu.android.utils.UriToFile.java
com.qiniu.android.utils.UrlSafeBase64.java