高佣联盟 v6.22.57版本的 MD5 值为:0c736d87a62b04809b5a48aa82055f6a

以下内容为反编译后的 BlockUploader.java 源代码,内容仅作参考


package cn.bmob.v3.datatype.up;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.helper.ErrorCode;
import cn.bmob.v3.util.BLog;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class BlockUploader implements Runnable {
    private String apiKey;
    private int[] blockIndex;
    private String bucket;
    private UploadClient client;
    private UpCompleteListener completeListener;
    private long expiration;
    private File file;
    private Map<String, Object> params;
    private PostData postData;
    private UpProgressListener progressListener;
    private int retryTime;
    private String saveToken;
    private SignatureListener signatureListener;
    private String tokenSecret;
    private int totalBlockNum;
    private String url;
    private String userPolicy;
    private String userSignature;
    private RandomAccessFile randomAccessFile = null;
    private int blockSize = Bmob.getFileBlockSize();

    public BlockUploader(UploadClient uploadClient, File file, Map<String, Object> map, String str, SignatureListener signatureListener, UpCompleteListener upCompleteListener, UpProgressListener upProgressListener) {
        this.client = uploadClient;
        this.file = file;
        this.params = map;
        this.progressListener = upProgressListener;
        this.completeListener = upCompleteListener;
        this.apiKey = str;
        this.signatureListener = signatureListener;
    }

    private void blockUpload(int i) {
        int i2;
        while (true) {
            if (this.postData == null) {
                this.postData = new PostData();
            }
            try {
                this.postData.data = readBlockByIndex(i);
            } catch (BmobException e) {
                this.completeListener.onComplete(false, e.toString());
            }
            HashMap hashMap = new HashMap();
            hashMap.put(Params.SAVE_TOKEN, this.saveToken);
            hashMap.put(Params.EXPIRATION, Long.valueOf(this.expiration));
            hashMap.put(Params.BLOCK_INDEX, Integer.valueOf(this.blockIndex[i]));
            hashMap.put(Params.BLOCK_MD5, UpYunUtils.md5(this.postData.data));
            String policy = UpYunUtils.getPolicy(hashMap);
            String signature = UpYunUtils.getSignature(hashMap, this.tokenSecret);
            HashMap hashMap2 = new HashMap();
            hashMap2.put(Params.POLICY, policy);
            hashMap2.put("signature", signature);
            this.postData.fileName = this.file.getName();
            PostData postData = this.postData;
            postData.params = hashMap2;
            boolean z = true;
            try {
                try {
                    this.client.blockMultipartPost(this.url, postData);
                    UpProgressListener upProgressListener = this.progressListener;
                    if (upProgressListener != null) {
                        upProgressListener.onRequestProgress(i, this.blockIndex.length);
                    }
                    i2 = i + 1;
                } finally {
                    this.postData = null;
                }
            } catch (BmobException e2) {
                e = e2;
            } catch (IOException e3) {
                e = e3;
            }
            try {
            } catch (BmobException | IOException e4) {
                e = e4;
                i = i2;
                int i3 = this.retryTime + 1;
                this.retryTime = i3;
                boolean z2 = i3 > 2;
                z = (!(e instanceof BmobException) || ((BmobException) e).getErrorCode() / 100 == 5) ? false : false;
                if (!z2 && !z) {
                    this.postData = null;
                } else {
                    this.completeListener.onComplete(false, e.getMessage());
                }
            }
            if (i == this.blockIndex.length - 1) {
                mergeRequest();
                break;
            } else {
                this.postData = null;
                i = i2;
            }
        }
    }

    private int[] getBlockIndex(JSONArray jSONArray) throws JSONException {
        int i = 0;
        for (int i2 = 0; i2 < jSONArray.length(); i2++) {
            if (jSONArray.getInt(i2) == 0) {
                i++;
            }
        }
        int[] iArr = new int[i];
        int i3 = 0;
        for (int i4 = 0; i4 < jSONArray.length(); i4++) {
            if (jSONArray.getInt(i4) == 0) {
                iArr[i3] = i4;
                i3++;
            }
        }
        return iArr;
    }

    private String getParamsString(Map<String, Object> map) {
        Object[] array = map.keySet().toArray();
        Arrays.sort(array);
        StringBuffer stringBuffer = new StringBuffer();
        for (Object obj : array) {
            stringBuffer.append(obj);
            stringBuffer.append(map.get(obj));
        }
        return stringBuffer.toString();
    }

    private void initRequest() {
        boolean z;
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(Params.POLICY, this.userPolicy);
        linkedHashMap.put("signature", this.userSignature);
        try {
            JSONObject jSONObject = new JSONObject(this.client.post(this.url, linkedHashMap));
            this.saveToken = jSONObject.optString(Params.SAVE_TOKEN);
            this.tokenSecret = jSONObject.optString(Params.TOKEN_SECRET);
            int[] blockIndex = getBlockIndex(jSONObject.getJSONArray("status"));
            this.blockIndex = blockIndex;
            if (blockIndex.length == 0) {
                mergeRequest();
            } else {
                blockUpload(0);
            }
        } catch (BmobException e) {
            e = e;
            boolean z2 = true;
            int i = this.retryTime + 1;
            this.retryTime = i;
            z = i <= 2;
            z2 = ((e instanceof BmobException) || ((BmobException) e).getErrorCode() / 100 == 5) ? false : false;
            if (z && !z2) {
                initRequest();
            } else {
                this.completeListener.onComplete(false, e.getMessage());
            }
        } catch (IOException e2) {
            e = e2;
            boolean z22 = true;
            int i2 = this.retryTime + 1;
            this.retryTime = i2;
            if (i2 <= 2) {
            }
            if (e instanceof BmobException) {
            }
            if (z) {
            }
            this.completeListener.onComplete(false, e.getMessage());
        } catch (JSONException e3) {
            this.completeListener.onComplete(false, e3.getMessage());
        }
    }

    private void mergeRequest() {
        HashMap hashMap = new HashMap();
        hashMap.put(Params.EXPIRATION, Long.valueOf(this.expiration));
        hashMap.put(Params.SAVE_TOKEN, this.saveToken);
        String policy = UpYunUtils.getPolicy(hashMap);
        String signature = UpYunUtils.getSignature(hashMap, this.tokenSecret);
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put(Params.POLICY, policy);
        linkedHashMap.put("signature", signature);
        boolean z = true;
        try {
            String post = this.client.post(this.url, linkedHashMap);
            UpProgressListener upProgressListener = this.progressListener;
            int[] iArr = this.blockIndex;
            upProgressListener.onRequestProgress(iArr.length, iArr.length);
            this.randomAccessFile.close();
            BLog.e(post);
            this.completeListener.onComplete(true, post);
        } catch (BmobException | IOException e) {
            int i = this.retryTime + 1;
            this.retryTime = i;
            boolean z2 = i > 2;
            z = (!(e instanceof BmobException) || ((BmobException) e).getErrorCode() / 100 == 5) ? false : false;
            if (!z2 && !z) {
                mergeRequest();
            } else {
                this.completeListener.onComplete(false, e.getMessage());
            }
        }
    }

    private byte[] readBlockByIndex(int i) throws BmobException {
        if (i <= this.totalBlockNum) {
            int i2 = this.blockSize;
            byte[] bArr = new byte[i2];
            try {
                this.randomAccessFile.seek(this.blockIndex[i] * i2);
                int read = this.randomAccessFile.read(bArr, 0, this.blockSize);
                if (read < this.blockSize) {
                    byte[] bArr2 = new byte[read];
                    System.arraycopy(bArr, 0, bArr2, 0, read);
                    return bArr2;
                }
                return bArr;
            } catch (IOException e) {
                throw new BmobException((int) ErrorCode.E9015, "readBlockByIndex failed:" + e.getMessage());
            }
        }
        BLog.e("Block index error", "the index is bigger than totalBlockNum.");
        throw new BmobException((int) ErrorCode.E9015, "readBlockByIndex: the index is bigger than totalBlockNum.");
    }

    @Override
    public void run() {
        try {
            this.bucket = (String) this.params.remove(Params.BUCKET);
            this.url = "http://m0.api.upyun.com/" + this.bucket;
            this.expiration = ((Long) this.params.get(Params.EXPIRATION)).longValue();
            this.params.put(Params.BLOCK_NUM, Integer.valueOf(UpYunUtils.getBlockNum(this.file, this.blockSize)));
            this.params.put(Params.FILE_SIZE, Long.valueOf(this.file.length()));
            this.params.put(Params.FILE_MD5, UpYunUtils.md5Hex(this.file));
            String str = (String) this.params.remove(Params.SAVE_KEY);
            String str2 = (String) this.params.get("path");
            if (str != null && str2 == null) {
                this.params.put("path", str);
            }
            this.userPolicy = UpYunUtils.getPolicy(this.params);
            String str3 = this.apiKey;
            if (str3 != null) {
                this.userSignature = UpYunUtils.getSignature(this.params, str3);
            } else {
                SignatureListener signatureListener = this.signatureListener;
                if (signatureListener != null) {
                    this.userSignature = signatureListener.getSignature(getParamsString(this.params));
                } else {
                    throw new RuntimeException("apikey 和 signatureListener 不可都为null");
                }
            }
            this.randomAccessFile = new RandomAccessFile(this.file, "r");
            this.totalBlockNum = UpYunUtils.getBlockNum(this.file, this.blockSize);
            initRequest();
        } catch (FileNotFoundException e) {
            throw new RuntimeException("文件不存在", e);
        }
    }
}