Search in sources :

Example 1 with InputStreamPipe

use of com.hippo.streampipe.InputStreamPipe in project EhViewer by seven332.

the class SpiderQueen method save.

@Nullable
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    int state = getPageState(index);
    if (STATE_FINISHED != state) {
        return null;
    }
    InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
    if (null == pipe) {
        return null;
    }
    OutputStream os = null;
    try {
        pipe.obtain();
        // Get dst file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(pipe.open(), null, options);
        pipe.close();
        String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
        UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
        if (null == dst) {
            return null;
        }
        // Copy
        os = dst.openOutputStream();
        IOUtils.copy(pipe.open(), os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        pipe.close();
        pipe.release();
        IOUtils.closeQuietly(os);
    }
}
Also used : InputStreamPipe(com.hippo.streampipe.InputStreamPipe) UniFile(com.hippo.unifile.UniFile) OutputStream(java.io.OutputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) Nullable(android.support.annotation.Nullable)

Example 2 with InputStreamPipe

use of com.hippo.streampipe.InputStreamPipe in project EhViewer by seven332.

the class SpiderQueen method readSpiderInfoFromLocal.

private synchronized SpiderInfo readSpiderInfoFromLocal() {
    SpiderInfo spiderInfo = mSpiderInfo.get();
    if (spiderInfo != null) {
        return spiderInfo;
    }
    // Read from download dir
    UniFile downloadDir = mSpiderDen.getDownloadDir();
    if (downloadDir != null) {
        UniFile file = downloadDir.findFile(SPIDER_INFO_FILENAME);
        spiderInfo = SpiderInfo.read(file);
        if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid && spiderInfo.token.equals(mGalleryInfo.token)) {
            return spiderInfo;
        }
    }
    // Read from cache
    InputStreamPipe pipe = mSpiderInfoCache.getInputStreamPipe(Long.toString(mGalleryInfo.gid));
    if (null != pipe) {
        try {
            pipe.obtain();
            spiderInfo = SpiderInfo.read(pipe.open());
            if (spiderInfo != null && spiderInfo.gid == mGalleryInfo.gid && spiderInfo.token.equals(mGalleryInfo.token)) {
                return spiderInfo;
            }
        } catch (IOException e) {
        // Ignore
        } finally {
            pipe.close();
            pipe.release();
        }
    }
    return null;
}
Also used : UniFile(com.hippo.unifile.UniFile) InputStreamPipe(com.hippo.streampipe.InputStreamPipe) IOException(java.io.IOException)

Example 3 with InputStreamPipe

use of com.hippo.streampipe.InputStreamPipe in project EhViewer by seven332.

the class SpiderDen method copyFromCacheToDownloadDir.

private boolean copyFromCacheToDownloadDir(int index) {
    if (sCache == null) {
        return false;
    }
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return false;
    }
    // Find image file in cache
    String key = EhCacheKeyFactory.getImageKey(mGid, index);
    InputStreamPipe pipe = sCache.getInputStreamPipe(key);
    if (pipe == null) {
        return false;
    }
    OutputStream os = null;
    try {
        // Get extension
        String extension;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        pipe.obtain();
        BitmapFactory.decodeStream(pipe.open(), null, options);
        pipe.close();
        extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
        if (extension != null) {
            extension = '.' + extension;
        } else {
            return false;
        }
        // Fix extension
        extension = fixExtension(extension);
        // Copy from cache to download dir
        UniFile file = dir.createFile(generateImageFilename(index, extension));
        if (file == null) {
            return false;
        }
        os = file.openOutputStream();
        IOUtils.copy(pipe.open(), os);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(os);
        pipe.close();
        pipe.release();
    }
}
Also used : UniFile(com.hippo.unifile.UniFile) UniFileInputStreamPipe(com.hippo.io.UniFileInputStreamPipe) InputStreamPipe(com.hippo.streampipe.InputStreamPipe) OutputStream(java.io.OutputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException)

Example 4 with InputStreamPipe

use of com.hippo.streampipe.InputStreamPipe in project EhViewer by seven332.

the class SpiderQueen method save.

public boolean save(int index, @NonNull UniFile file) {
    int state = getPageState(index);
    if (STATE_FINISHED != state) {
        return false;
    }
    InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
    if (null == pipe) {
        return false;
    }
    OutputStream os = null;
    try {
        os = file.openOutputStream();
        pipe.obtain();
        IOUtils.copy(pipe.open(), os);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        pipe.close();
        pipe.release();
        IOUtils.closeQuietly(os);
    }
}
Also used : InputStreamPipe(com.hippo.streampipe.InputStreamPipe) OutputStream(java.io.OutputStream) IOException(java.io.IOException)

Aggregations

InputStreamPipe (com.hippo.streampipe.InputStreamPipe)4 IOException (java.io.IOException)4 UniFile (com.hippo.unifile.UniFile)3 OutputStream (java.io.OutputStream)3 BitmapFactory (android.graphics.BitmapFactory)2 Nullable (android.support.annotation.Nullable)1 UniFileInputStreamPipe (com.hippo.io.UniFileInputStreamPipe)1