Search in sources :

Example 1 with DownloadStatus

use of zlc.season.rxdownload2.entity.DownloadStatus in project RxDownload by ssseasonnn.

the class FileHelper method saveFile.

public void saveFile(FlowableEmitter<DownloadStatus> emitter, int i, File tempFile, File saveFile, ResponseBody response) {
    RandomAccessFile record = null;
    FileChannel recordChannel = null;
    RandomAccessFile save = null;
    FileChannel saveChannel = null;
    InputStream inStream = null;
    try {
        try {
            int readLen;
            byte[] buffer = new byte[2048];
            DownloadStatus status = new DownloadStatus();
            record = new RandomAccessFile(tempFile, ACCESS);
            recordChannel = record.getChannel();
            MappedByteBuffer recordBuffer = recordChannel.map(READ_WRITE, 0, RECORD_FILE_TOTAL_SIZE);
            int startIndex = i * EACH_RECORD_SIZE;
            long start = recordBuffer.getLong(startIndex);
            //                long end = recordBuffer.getLong(startIndex + 8);
            long totalSize = recordBuffer.getLong(RECORD_FILE_TOTAL_SIZE - 8) + 1;
            status.setTotalSize(totalSize);
            save = new RandomAccessFile(saveFile, ACCESS);
            saveChannel = save.getChannel();
            inStream = response.byteStream();
            while ((readLen = inStream.read(buffer)) != -1 && !emitter.isCancelled()) {
                MappedByteBuffer saveBuffer = saveChannel.map(READ_WRITE, start, readLen);
                start += readLen;
                saveBuffer.put(buffer, 0, readLen);
                recordBuffer.putLong(startIndex, start);
                status.setDownloadSize(totalSize - getResidue(recordBuffer));
                emitter.onNext(status);
            }
            emitter.onComplete();
        } finally {
            closeQuietly(record);
            closeQuietly(recordChannel);
            closeQuietly(save);
            closeQuietly(saveChannel);
            closeQuietly(inStream);
            closeQuietly(response);
        }
    } catch (IOException e) {
        emitter.onError(e);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) InputStream(java.io.InputStream) DownloadStatus(zlc.season.rxdownload2.entity.DownloadStatus) IOException(java.io.IOException)

Example 2 with DownloadStatus

use of zlc.season.rxdownload2.entity.DownloadStatus in project RxDownload by ssseasonnn.

the class DownloadEventFactory method createEvent.

private static DownloadEvent createEvent(int flag, DownloadStatus status, Throwable throwable) {
    DownloadEvent event = createEvent(flag, status);
    event.setError(throwable);
    return event;
}
Also used : DownloadEvent(zlc.season.rxdownload2.entity.DownloadEvent)

Example 3 with DownloadStatus

use of zlc.season.rxdownload2.entity.DownloadStatus in project RxDownload by ssseasonnn.

the class DownloadEventFactory method createEvent.

public static DownloadEvent createEvent(int flag, DownloadStatus status) {
    DownloadEvent event = new DownloadEvent();
    event.setDownloadStatus(status == null ? new DownloadStatus() : status);
    event.setFlag(flag);
    return event;
}
Also used : DownloadEvent(zlc.season.rxdownload2.entity.DownloadEvent) DownloadStatus(zlc.season.rxdownload2.entity.DownloadStatus)

Example 4 with DownloadStatus

use of zlc.season.rxdownload2.entity.DownloadStatus in project RxDownload by ssseasonnn.

the class FileHelper method saveFile.

public void saveFile(FlowableEmitter<DownloadStatus> emitter, File saveFile, Response<ResponseBody> resp) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {
        try {
            int readLen;
            int downloadSize = 0;
            byte[] buffer = new byte[8192];
            DownloadStatus status = new DownloadStatus();
            inputStream = resp.body().byteStream();
            outputStream = new FileOutputStream(saveFile);
            long contentLength = resp.body().contentLength();
            boolean isChunked = isChunked(resp);
            if (isChunked || contentLength == -1) {
                status.isChunked = true;
            }
            status.setTotalSize(contentLength);
            while ((readLen = inputStream.read(buffer)) != -1 && !emitter.isCancelled()) {
                outputStream.write(buffer, 0, readLen);
                downloadSize += readLen;
                status.setDownloadSize(downloadSize);
                emitter.onNext(status);
            }
            // This is important!!!
            outputStream.flush();
            emitter.onComplete();
        } finally {
            closeQuietly(inputStream);
            closeQuietly(outputStream);
            closeQuietly(resp.body());
        }
    } catch (IOException e) {
        emitter.onError(e);
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DownloadStatus(zlc.season.rxdownload2.entity.DownloadStatus) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException)

Example 5 with DownloadStatus

use of zlc.season.rxdownload2.entity.DownloadStatus in project RxDownload by ssseasonnn.

the class DataBaseHelper method readStatus.

/**
     * Read the url's download status.
     *
     * @param url url
     * @return download status
     */
public DownloadStatus readStatus(String url) {
    Cursor cursor = null;
    try {
        cursor = getReadableDatabase().query(TABLE_NAME, new String[] { COLUMN_DOWNLOAD_SIZE, COLUMN_TOTAL_SIZE, COLUMN_IS_CHUNKED }, COLUMN_URL + "=?", new String[] { url }, null, null, null);
        cursor.moveToFirst();
        if (cursor.getCount() == 0) {
            return new DownloadStatus();
        } else {
            return Db.RecordTable.readStatus(cursor);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
Also used : DownloadStatus(zlc.season.rxdownload2.entity.DownloadStatus) Cursor(android.database.Cursor)

Aggregations

DownloadStatus (zlc.season.rxdownload2.entity.DownloadStatus)6 DownloadEvent (zlc.season.rxdownload2.entity.DownloadEvent)4 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Cursor (android.database.Cursor)1 Consumer (io.reactivex.functions.Consumer)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 MappedByteBuffer (java.nio.MappedByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 DownloadMission (zlc.season.rxdownload2.entity.DownloadMission)1 DownloadRecord (zlc.season.rxdownload2.entity.DownloadRecord)1