Search in sources :

Example 1 with DownloadRequest

use of com.lzy.okserver.download.db.DownloadRequest in project okhttp-OkGo by jeasonlzy.

the class DownloadInfo method buildContentValues.

public static ContentValues buildContentValues(DownloadInfo downloadInfo) {
    ContentValues values = new ContentValues();
    values.put(TASK_KEY, downloadInfo.getTaskKey());
    values.put(URL, downloadInfo.getUrl());
    values.put(TARGET_FOLDER, downloadInfo.getTargetFolder());
    values.put(TARGET_PATH, downloadInfo.getTargetPath());
    values.put(FILE_NAME, downloadInfo.getFileName());
    values.put(PROGRESS, downloadInfo.getProgress());
    values.put(TOTAL_LENGTH, downloadInfo.getTotalLength());
    values.put(DOWNLOAD_LENGTH, downloadInfo.getDownloadLength());
    values.put(NETWORK_SPEED, downloadInfo.getNetworkSpeed());
    values.put(STATE, downloadInfo.getState());
    BaseRequest request = downloadInfo.getRequest();
    DownloadRequest downloadRequest = downloadInfo.getDownloadRequest();
    downloadRequest.cacheKey = request.getCacheKey();
    downloadRequest.cacheTime = request.getCacheTime();
    downloadRequest.cacheMode = request.getCacheMode();
    downloadRequest.url = request.getBaseUrl();
    downloadRequest.params = request.getParams();
    downloadRequest.headers = request.getHeaders();
    downloadRequest.method = DownloadRequest.getMethod(request);
    ByteArrayOutputStream baos = null;
    ObjectOutputStream oos = null;
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(downloadRequest);
        oos.flush();
        byte[] requestData = baos.toByteArray();
        values.put(DownloadInfo.DOWNLOAD_REQUEST, requestData);
    } catch (IOException e) {
        OkLogger.e(e);
    } finally {
        try {
            if (oos != null)
                oos.close();
            if (baos != null)
                baos.close();
        } catch (IOException e) {
            OkLogger.e(e);
        }
    }
    try {
        baos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(baos);
        oos.writeObject(downloadInfo.getData());
        oos.flush();
        byte[] data = baos.toByteArray();
        values.put(DownloadInfo.DATA, data);
    } catch (IOException e) {
        OkLogger.e(e);
    } finally {
        try {
            if (oos != null)
                oos.close();
            if (baos != null)
                baos.close();
        } catch (IOException e) {
            OkLogger.e(e);
        }
    }
    return values;
}
Also used : ContentValues(android.content.ContentValues) BaseRequest(com.lzy.okgo.request.BaseRequest) DownloadRequest(com.lzy.okserver.download.db.DownloadRequest) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

Example 2 with DownloadRequest

use of com.lzy.okserver.download.db.DownloadRequest in project okhttp-OkGo by jeasonlzy.

the class DownloadInfo method parseCursorToBean.

public static DownloadInfo parseCursorToBean(Cursor cursor) {
    DownloadInfo info = new DownloadInfo();
    info.setId(cursor.getInt(cursor.getColumnIndex(DownloadInfo.ID)));
    info.setTaskKey(cursor.getString(cursor.getColumnIndex(DownloadInfo.TASK_KEY)));
    info.setUrl(cursor.getString(cursor.getColumnIndex(DownloadInfo.URL)));
    info.setTargetFolder(cursor.getString(cursor.getColumnIndex(DownloadInfo.TARGET_FOLDER)));
    info.setTargetPath(cursor.getString(cursor.getColumnIndex(DownloadInfo.TARGET_PATH)));
    info.setFileName(cursor.getString(cursor.getColumnIndex(DownloadInfo.FILE_NAME)));
    info.setProgress(cursor.getFloat(cursor.getColumnIndex(DownloadInfo.PROGRESS)));
    info.setTotalLength(cursor.getLong(cursor.getColumnIndex(DownloadInfo.TOTAL_LENGTH)));
    info.setDownloadLength(cursor.getLong(cursor.getColumnIndex(DownloadInfo.DOWNLOAD_LENGTH)));
    info.setNetworkSpeed(cursor.getLong(cursor.getColumnIndex(DownloadInfo.NETWORK_SPEED)));
    info.setState(cursor.getInt(cursor.getColumnIndex(DownloadInfo.STATE)));
    ByteArrayInputStream bais = null;
    ObjectInputStream ois = null;
    byte[] requestData = cursor.getBlob(cursor.getColumnIndex(DownloadInfo.DOWNLOAD_REQUEST));
    try {
        if (requestData != null) {
            bais = new ByteArrayInputStream(requestData);
            ois = new ObjectInputStream(bais);
            DownloadRequest downloadRequest = (DownloadRequest) ois.readObject();
            info.setDownloadRequest(downloadRequest);
            BaseRequest request = DownloadRequest.createRequest(downloadRequest.url, downloadRequest.method);
            if (request != null) {
                request.cacheMode(downloadRequest.cacheMode);
                request.cacheTime(downloadRequest.cacheTime);
                request.cacheKey(downloadRequest.cacheKey);
                request.params(downloadRequest.params);
                request.headers(downloadRequest.headers);
                info.setRequest(request);
            }
        }
    } catch (Exception e) {
        OkLogger.e(e);
    } finally {
        try {
            if (ois != null)
                ois.close();
            if (bais != null)
                bais.close();
        } catch (IOException e) {
            OkLogger.e(e);
        }
    }
    byte[] data = cursor.getBlob(cursor.getColumnIndex(DownloadInfo.DATA));
    try {
        if (data != null) {
            bais = new ByteArrayInputStream(data);
            ois = new ObjectInputStream(bais);
            Serializable serializableData = (Serializable) ois.readObject();
            info.setData(serializableData);
        }
    } catch (Exception e) {
        OkLogger.e(e);
    } finally {
        try {
            if (ois != null)
                ois.close();
            if (bais != null)
                bais.close();
        } catch (IOException e) {
            OkLogger.e(e);
        }
    }
    return info;
}
Also used : Serializable(java.io.Serializable) ByteArrayInputStream(java.io.ByteArrayInputStream) BaseRequest(com.lzy.okgo.request.BaseRequest) DownloadRequest(com.lzy.okserver.download.db.DownloadRequest) IOException(java.io.IOException) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

BaseRequest (com.lzy.okgo.request.BaseRequest)2 DownloadRequest (com.lzy.okserver.download.db.DownloadRequest)2 IOException (java.io.IOException)2 ContentValues (android.content.ContentValues)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Serializable (java.io.Serializable)1