Search in sources :

Example 1 with QANoFoundException

use of cn.jeesoft.qa.error.QANoFoundException in project QuickAndroid by ImKarl.

the class QAFileHttpHandler method handlerResponse.

@Override
public File handlerResponse(Response response) throws Exception {
    if (targetFile == null) {
        throw new QANoFoundException(QAException.CODE_IO_FILE, "'targetFile'不能为空");
    }
    File parentFile = targetFile.getParentFile();
    if (parentFile != null && !parentFile.exists()) {
        parentFile.mkdirs();
    }
    Exception exception = null;
    InputStream stream = null;
    byte[] buf = new byte[2048];
    int len = 0;
    FileOutputStream fos = null;
    try {
        ResponseBody responseBody = response.body();
        stream = responseBody.byteStream();
        fos = new FileOutputStream(targetFile);
        while ((len = stream.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        fos.flush();
    } catch (Exception e) {
        exception = e;
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException e) {
        }
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
        }
    }
    if (exception == null) {
        return targetFile;
    }
    throw new QAIOException(QAException.CODE_IO_FILE, exception);
}
Also used : QANoFoundException(cn.jeesoft.qa.error.QANoFoundException) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) QAIOException(cn.jeesoft.qa.error.QAIOException) IOException(java.io.IOException) QAIOException(cn.jeesoft.qa.error.QAIOException) File(java.io.File) QAIOException(cn.jeesoft.qa.error.QAIOException) QANoFoundException(cn.jeesoft.qa.error.QANoFoundException) IOException(java.io.IOException) QAException(cn.jeesoft.qa.error.QAException) ResponseBody(com.squareup.okhttp.ResponseBody)

Aggregations

QAException (cn.jeesoft.qa.error.QAException)1 QAIOException (cn.jeesoft.qa.error.QAIOException)1 QANoFoundException (cn.jeesoft.qa.error.QANoFoundException)1 ResponseBody (com.squareup.okhttp.ResponseBody)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1