Search in sources :

Example 1 with DiskCacheEntity

use of org.xutils.cache.DiskCacheEntity in project xUtils3 by wyouflf.

the class FileLoader method initDiskCacheFile.

// 保存路径为空, 存入磁盘缓存.
private void initDiskCacheFile(final UriRequest request) throws Throwable {
    DiskCacheEntity entity = new DiskCacheEntity();
    entity.setKey(request.getCacheKey());
    diskCacheFile = LruDiskCache.getDiskCache(params.getCacheDirName()).createDiskCacheFile(entity);
    if (diskCacheFile != null) {
        saveFilePath = diskCacheFile.getAbsolutePath();
        // diskCacheFile is a temp path, diskCacheFile.commit() return the dest file.
        tempSaveFilePath = saveFilePath;
        isAutoRename = false;
    } else {
        throw new IOException("create cache file error:" + request.getCacheKey());
    }
}
Also used : DiskCacheEntity(org.xutils.cache.DiskCacheEntity) IOException(java.io.IOException)

Example 2 with DiskCacheEntity

use of org.xutils.cache.DiskCacheEntity in project xUtils3 by wyouflf.

the class HttpRequest method loadResultFromCache.

/**
     * 尝试从缓存获取结果, 并为请求头加入缓存控制参数.
     *
     * @return
     * @throws Throwable
     */
@Override
public Object loadResultFromCache() throws Throwable {
    isLoading = true;
    DiskCacheEntity cacheEntity = LruDiskCache.getDiskCache(params.getCacheDirName()).setMaxSize(params.getCacheSize()).get(this.getCacheKey());
    if (cacheEntity != null) {
        if (HttpMethod.permitsCache(params.getMethod())) {
            Date lastModified = cacheEntity.getLastModify();
            if (lastModified.getTime() > 0) {
                params.setHeader("If-Modified-Since", toGMTString(lastModified));
            }
            String eTag = cacheEntity.getEtag();
            if (!TextUtils.isEmpty(eTag)) {
                params.setHeader("If-None-Match", eTag);
            }
        }
        return loader.loadFromCache(cacheEntity);
    } else {
        return null;
    }
}
Also used : DiskCacheEntity(org.xutils.cache.DiskCacheEntity) Date(java.util.Date)

Example 3 with DiskCacheEntity

use of org.xutils.cache.DiskCacheEntity in project xUtils3 by wyouflf.

the class ImageDecoder method saveThumbCache.

/**
     * 根据文件的修改时间和图片的属性保存缩略图
     *
     * @param file
     * @param options
     * @param thumbBitmap
     */
private static void saveThumbCache(File file, ImageOptions options, Bitmap thumbBitmap) {
    if (!WebPFactory.available())
        return;
    DiskCacheEntity entity = new DiskCacheEntity();
    entity.setKey(file.getAbsolutePath() + "@" + file.lastModified() + options.toString());
    DiskCacheFile cacheFile = null;
    OutputStream out = null;
    try {
        cacheFile = THUMB_CACHE.createDiskCacheFile(entity);
        if (cacheFile != null) {
            out = new FileOutputStream(cacheFile);
            byte[] encoded = WebPFactory.encodeBitmap(thumbBitmap, 80);
            out.write(encoded);
            out.flush();
            cacheFile = cacheFile.commit();
        }
    } catch (Throwable ex) {
        IOUtil.deleteFileOrDir(cacheFile);
        LogUtil.w(ex.getMessage(), ex);
    } finally {
        IOUtil.closeQuietly(cacheFile);
        IOUtil.closeQuietly(out);
    }
}
Also used : DiskCacheFile(org.xutils.cache.DiskCacheFile) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) DiskCacheEntity(org.xutils.cache.DiskCacheEntity)

Example 4 with DiskCacheEntity

use of org.xutils.cache.DiskCacheEntity 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;
}
Also used : FileLockedException(org.xutils.ex.FileLockedException) ProcessLock(org.xutils.common.util.ProcessLock) HttpException(org.xutils.ex.HttpException) DiskCacheEntity(org.xutils.cache.DiskCacheEntity) File(java.io.File) DiskCacheFile(org.xutils.cache.DiskCacheFile) Date(java.util.Date)

Example 5 with DiskCacheEntity

use of org.xutils.cache.DiskCacheEntity in project xUtils3 by wyouflf.

the class Loader method saveStringCache.

protected void saveStringCache(UriRequest request, String resultStr) {
    if (!TextUtils.isEmpty(resultStr)) {
        DiskCacheEntity entity = new DiskCacheEntity();
        entity.setKey(request.getCacheKey());
        entity.setLastAccess(System.currentTimeMillis());
        entity.setEtag(request.getETag());
        entity.setExpires(request.getExpiration());
        entity.setLastModify(new Date(request.getLastModified()));
        entity.setTextContent(resultStr);
        LruDiskCache.getDiskCache(request.getParams().getCacheDirName()).put(entity);
    }
}
Also used : DiskCacheEntity(org.xutils.cache.DiskCacheEntity) Date(java.util.Date)

Aggregations

DiskCacheEntity (org.xutils.cache.DiskCacheEntity)5 Date (java.util.Date)3 DiskCacheFile (org.xutils.cache.DiskCacheFile)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ProcessLock (org.xutils.common.util.ProcessLock)1 FileLockedException (org.xutils.ex.FileLockedException)1 HttpException (org.xutils.ex.HttpException)1