Search in sources :

Example 1 with QAIOException

use of cn.jeesoft.qa.error.QAIOException 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)

Example 2 with QAIOException

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

the class QABimtapHttpHandler method handlerResponse.

@Override
public Bitmap handlerResponse(Response response) throws Exception {
    Exception exception = null;
    Bitmap bitmap = null;
    InputStream stream = null;
    try {
        stream = response.body().byteStream();
        bitmap = BitmapFactory.decodeStream(stream);
    } catch (Exception e) {
        exception = e;
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }
    if (exception == null) {
        return bitmap;
    }
    throw new QAIOException(QAException.CODE_IO_FILE, exception);
}
Also used : Bitmap(android.graphics.Bitmap) InputStream(java.io.InputStream) QAIOException(cn.jeesoft.qa.error.QAIOException) IOException(java.io.IOException) QAIOException(cn.jeesoft.qa.error.QAIOException) QAIOException(cn.jeesoft.qa.error.QAIOException) IOException(java.io.IOException) QAException(cn.jeesoft.qa.error.QAException)

Aggregations

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