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);
}
}
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;
}
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;
}
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);
}
}
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();
}
}
}
Aggregations