use of org.xutils.ex.FileLockedException in project xUtils3 by wyouflf.
the class LruDiskCache method createDiskCacheFile.
public DiskCacheFile createDiskCacheFile(DiskCacheEntity entity) throws IOException {
if (!available || entity == null) {
return null;
}
DiskCacheFile result = null;
entity.setPath(new File(this.cacheDir, MD5.md5(entity.getKey())).getAbsolutePath());
String tempFilePath = entity.getPath() + TEMP_FILE_SUFFIX;
ProcessLock processLock = ProcessLock.tryLock(tempFilePath, true);
if (processLock != null && processLock.isValid()) {
result = new DiskCacheFile(entity, tempFilePath, processLock);
if (!result.getParentFile().exists()) {
result.mkdirs();
}
} else {
throw new FileLockedException(entity.getPath());
}
return result;
}
use of org.xutils.ex.FileLockedException in project xUtils3 by wyouflf.
the class LruDiskCache method commitDiskCacheFile.
/**
* 添加缓存文件
*
* @param cacheFile
*/
/*package*/
DiskCacheFile commitDiskCacheFile(DiskCacheFile cacheFile) throws IOException {
if (cacheFile != null && cacheFile.length() < 1L) {
IOUtil.closeQuietly(cacheFile);
return null;
}
if (!available || cacheFile == null) {
return null;
}
DiskCacheFile result = null;
DiskCacheEntity cacheEntity = cacheFile.cacheEntity;
if (cacheFile.getName().endsWith(TEMP_FILE_SUFFIX)) {
// is temp file
ProcessLock processLock = null;
DiskCacheFile destFile = null;
try {
String destPath = cacheEntity.getPath();
processLock = ProcessLock.tryLock(destPath, true, LOCK_WAIT);
if (processLock != null && processLock.isValid()) {
// lock
destFile = new DiskCacheFile(cacheEntity, destPath, processLock);
if (cacheFile.renameTo(destFile)) {
try {
result = destFile;
cacheDb.replace(cacheEntity);
} catch (DbException ex) {
LogUtil.e(ex.getMessage(), ex);
}
trimSize();
} else {
throw new IOException("rename:" + cacheFile.getAbsolutePath());
}
} else {
throw new FileLockedException(destPath);
}
} catch (InterruptedException ex) {
result = cacheFile;
LogUtil.e(ex.getMessage(), ex);
} finally {
if (result == null) {
result = cacheFile;
IOUtil.closeQuietly(destFile);
IOUtil.closeQuietly(processLock);
IOUtil.deleteFileOrDir(destFile);
} else {
IOUtil.closeQuietly(cacheFile);
IOUtil.deleteFileOrDir(cacheFile);
}
}
} else {
result = cacheFile;
}
return result;
}
use of org.xutils.ex.FileLockedException in project xUtils3 by wyouflf.
the class FileLoader method load.
@Override
public File load(final UriRequest request) throws Throwable {
ProcessLock processLock = null;
File result = null;
try {
// 处理[下载逻辑1](见文件头doc)
saveFilePath = params.getSaveFilePath();
diskCacheFile = null;
if (TextUtils.isEmpty(saveFilePath)) {
if (progressHandler != null && !progressHandler.updateProgress(0, 0, false)) {
throw new Callback.CancelledException("download stopped!");
}
// 保存路径为空, 存入磁盘缓存.
initDiskCacheFile(request);
} else {
tempSaveFilePath = saveFilePath + ".tmp";
}
if (progressHandler != null && !progressHandler.updateProgress(0, 0, false)) {
throw new Callback.CancelledException("download stopped!");
}
// 等待, 若不能下载则取消此次下载.
processLock = ProcessLock.tryLock(saveFilePath + "_lock", true);
if (processLock == null || !processLock.isValid()) {
throw new FileLockedException("download exists: " + saveFilePath);
}
params = request.getParams();
{
// 处理[断点逻辑1](见文件头doc)
long range = 0;
if (isAutoResume) {
File tempFile = new File(tempSaveFilePath);
long fileLen = tempFile.length();
if (fileLen <= CHECK_SIZE) {
IOUtil.deleteFileOrDir(tempFile);
range = 0;
} else {
range = fileLen - CHECK_SIZE;
}
}
// retry 时需要覆盖RANGE参数
params.setHeader("RANGE", "bytes=" + range + "-");
}
if (progressHandler != null && !progressHandler.updateProgress(0, 0, false)) {
throw new Callback.CancelledException("download stopped!");
}
// may be throw an HttpException
request.sendRequest();
contentLength = request.getContentLength();
if (isAutoRename) {
responseFileName = getResponseFileName(request);
}
if (isAutoResume) {
isAutoResume = isSupportRange(request);
}
if (progressHandler != null && !progressHandler.updateProgress(0, 0, false)) {
throw new Callback.CancelledException("download stopped!");
}
if (diskCacheFile != null) {
DiskCacheEntity entity = diskCacheFile.getCacheEntity();
entity.setLastAccess(System.currentTimeMillis());
entity.setEtag(request.getETag());
entity.setExpires(request.getExpiration());
entity.setLastModify(new Date(request.getLastModified()));
}
result = this.load(request.getInputStream());
} catch (HttpException httpException) {
if (httpException.getCode() == 416) {
if (diskCacheFile != null) {
result = diskCacheFile.commit();
} else {
result = new File(tempSaveFilePath);
}
// 从缓存获取文件, 不rename和断点, 直接退出.
if (result != null && result.exists()) {
if (isAutoRename) {
responseFileName = getResponseFileName(request);
}
result = autoRename(result);
} else {
IOUtil.deleteFileOrDir(result);
throw new IllegalStateException("cache file not found" + request.getCacheKey());
}
} else {
throw httpException;
}
} finally {
IOUtil.closeQuietly(processLock);
IOUtil.closeQuietly(diskCacheFile);
}
return result;
}
Aggregations