翰林优商.apk(点击下载) / FileAsyncHttpResponseHandler.java


package com.baidu.speech.utils.auth;

import android.content.Context;
import com.baidu.speech.utils.LogUtil;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public abstract class FileAsyncHttpResponseHandler extends ResponseHandler {
    public static final int BUFFER_SIZE = 4096;
    private static final String LOG_TAG = "FileAsyncHttpRH";
    public final boolean append;
    public final File file;
    public File frontendFile;
    public final boolean renameIfExists;

    public FileAsyncHttpResponseHandler(Context context) {
        this.file = getTemporaryFile(context);
        this.append = false;
        this.renameIfExists = false;
    }

    public FileAsyncHttpResponseHandler(File file2) {
        this(file2, false);
    }

    public FileAsyncHttpResponseHandler(File file2, boolean z) {
        this(file2, z, false);
    }

    public FileAsyncHttpResponseHandler(File file2, boolean z, boolean z2) {
        CommonUtility.asserts(file2 != null, "File passed into FileAsyncHttpResponseHandler constructor must not be null");
        if (!file2.isDirectory() && !file2.getParentFile().isDirectory()) {
            CommonUtility.asserts(file2.getParentFile().mkdirs(), "Cannot create parent directories for requested File location");
        }
        if (file2.isDirectory() && !file2.mkdirs()) {
            LogUtil.d(LOG_TAG, "Cannot create directories for requested Directory location, might not be a problem");
        }
        this.file = file2;
        this.append = z;
        this.renameIfExists = z2;
    }

    public boolean deleteTargetFile() {
        return getTargetFile() != null && getTargetFile().delete();
    }

    public File getOriginalFile() {
        CommonUtility.asserts(this.file != null, "Target file is null, fatal!");
        return this.file;
    }

    public File getTargetFile() {
        if (this.frontendFile == null) {
            this.frontendFile = getOriginalFile().isDirectory() ? getTargetFileByParsingURL() : getOriginalFile();
        }
        return this.frontendFile;
    }

    public File getTargetFileByParsingURL() {
        StringBuilder sb;
        CommonUtility.asserts(getOriginalFile().isDirectory(), "Target file is not a directory, cannot proceed");
        CommonUtility.asserts(getRequestURI() != null, "RequestURI is null, cannot proceed");
        String uri = getRequestURI().toString();
        String substring = uri.substring(uri.lastIndexOf(47) + 1, uri.length());
        File file2 = new File(getOriginalFile(), substring);
        if (!file2.exists() || !this.renameIfExists) {
            return file2;
        }
        if (!substring.contains(".")) {
            sb = new StringBuilder();
            sb.append(substring);
            sb.append(" (%d)");
        } else {
            sb = new StringBuilder();
            sb.append(substring.substring(0, substring.lastIndexOf(46)));
            sb.append(" (%d)");
            sb.append(substring.substring(substring.lastIndexOf(46), substring.length()));
        }
        String sb2 = sb.toString();
        int i = 0;
        while (true) {
            File file3 = new File(getOriginalFile(), String.format(sb2, Integer.valueOf(i)));
            if (!file3.exists()) {
                return file3;
            }
            i++;
        }
    }

    public File getTemporaryFile(Context context) {
        CommonUtility.asserts(context != null, "Tried creating temporary file without having Context");
        try {
            return File.createTempFile("temp_", "_handled", context.getCacheDir());
        } catch (IOException unused) {
            LogUtil.e(LOG_TAG, "Cannot create temporary file");
            return null;
        }
    }

    @Override // com.baidu.speech.utils.auth.ResponseHandler
    public void onFailure(int i, Map<String, List<String>> map, String str, byte[] bArr, Throwable th) {
        onFailure(i, map, th, getTargetFile());
    }

    public abstract void onFailure(int i, Map<String, List<String>> map, Throwable th, File file2);

    public void onProgress(long j, long j2) {
    }

    public abstract void onSuccess(int i, Map<String, List<String>> map, File file2);

    @Override // com.baidu.speech.utils.auth.ResponseHandler
    public void onSuccess(int i, Map<String, List<String>> map, String str, byte[] bArr) {
        onSuccess(i, map, getTargetFile());
    }
}