Java Code Examples for com.hippo.yorozuya.IOUtils#copy()

The following examples show how to use com.hippo.yorozuya.IOUtils#copy() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadsScene.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(InputStream is, long length, String mediaType, ProgressNotifier notify) {
    ensureFile();
    if (mFile == null) {
        return false;
    }

    OutputStream os = null;
    try {
        os = mFile.openOutputStream();
        IOUtils.copy(is, os);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        IOUtils.closeQuietly(os);
    }
}
 
Example 2
Source File: DirGalleryProvider.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(int index, @NonNull UniFile file) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return false;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = fileList[index].openInputStream();
        os = file.openOutputStream();
        IOUtils.copy(is, os);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
 
Example 3
Source File: EhTagDatabase.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private static boolean save(OkHttpClient client, String url, File file) {
  Request request = new Request.Builder().url(url).build();
  Call call = client.newCall(request);
  try (Response response = call.execute()) {
    if (!response.isSuccessful()) {
      return false;
    }
    ResponseBody body = response.body();
    if (body == null) {
      return false;
    }

    try (InputStream is = body.byteStream(); OutputStream os = new FileOutputStream(file)) {
      IOUtils.copy(is, os);
    }

    return true;
  } catch (Throwable t) {
    ExceptionUtils.throwIfFatal(t);
    return false;
  }
}
 
Example 4
Source File: EhTagDatabase.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private static boolean save(OkHttpClient client, String url, File file) {
  Request request = new Request.Builder().url(url).build();
  Call call = client.newCall(request);
  try (Response response = call.execute()) {
    if (!response.isSuccessful()) {
      return false;
    }
    ResponseBody body = response.body();
    if (body == null) {
      return false;
    }

    try (InputStream is = body.byteStream(); OutputStream os = new FileOutputStream(file)) {
      IOUtils.copy(is, os);
    }

    return true;
  } catch (Throwable t) {
    ExceptionUtils.throwIfFatal(t);
    return false;
  }
}
 
Example 5
Source File: DownloadsScene.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(InputStream is, long length, String mediaType, ProgressNotifier notify) {
    ensureFile();
    if (mFile == null) {
        return false;
    }

    OutputStream os = null;
    try {
        os = mFile.openOutputStream();
        IOUtils.copy(is, os);
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        IOUtils.closeQuietly(os);
    }
}
 
Example 6
Source File: DirGalleryProvider.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean save(int index, @NonNull UniFile file) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return false;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = fileList[index].openInputStream();
        os = file.openOutputStream();
        IOUtils.copy(is, os);
        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
 
Example 7
Source File: SpiderQueen.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 8
Source File: SpiderQueen.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 9
Source File: HeaderImageView.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
@Override
public boolean save(InputStream is, long length, String mediaType, ProgressNotify notify) {
    OutputStream os = null;
    try {
        boolean autoSave = Settings.getSaveImageAuto() && mName != null;
        if (autoSave) {
            String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mediaType);
            if (TextUtils.isEmpty(extension)) {
                extension = "jpg";
            }
            String filename = mName + '.' + extension;
            UniFile dir = Settings.getImageSaveLocation();
            if (dir != null) {
                mTempFile = dir.createFile(filename);
            } else {
                mTempFile = UniFile.fromFile(NMBAppConfig.createTempFileWithFilename(filename));
            }
        } else {
            mTempFile = UniFile.fromFile(NMBAppConfig.createTempFile());
        }

        if (mTempFile == null) {
            return false;
        }
        os = mTempFile.openOutputStream();
        IOUtils.copy(is, os);

        // Notify media scanner
        if (autoSave) {
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mTempFile.getUri()));
        }

        return true;
    } catch (IOException e) {
        return false;
    } finally {
        IOUtils.closeQuietly(os);
    }
}
 
Example 10
Source File: LogCat.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static boolean save(File file) {
    if (!FileUtils.ensureFile(file)) {
        return false;
    }

    try {
        Process p = Runtime.getRuntime().exec("logcat -d");
        IOUtils.copy(p.getInputStream(), new FileOutputStream(file));
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 11
Source File: DirGalleryProvider.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return null;
    }

    UniFile src = fileList[index];
    String extension = FileUtils.getExtensionFromFilename(src.getName());
    UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
    if (null == dst) {
        return null;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.openInputStream();
        os = dst.openOutputStream();
        IOUtils.copy(is, os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
 
Example 12
Source File: SpiderQueen.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 13
Source File: LogCat.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
public static boolean save(File file) {
    if (!FileUtils.ensureFile(file)) {
        return false;
    }

    try {
        Process p = Runtime.getRuntime().exec("logcat -d");
        IOUtils.copy(p.getInputStream(), new FileOutputStream(file));
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 14
Source File: LogCat.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static boolean save(File file) {
    if (!FileUtils.ensureFile(file)) {
        return false;
    }

    try {
        Process p = Runtime.getRuntime().exec("logcat -d");
        IOUtils.copy(p.getInputStream(), new FileOutputStream(file));
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}
 
Example 15
Source File: DirGalleryProvider.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return null;
    }

    UniFile src = fileList[index];
    String extension = FileUtils.getExtensionFromFilename(src.getName());
    UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
    if (null == dst) {
        return null;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.openInputStream();
        os = dst.openOutputStream();
        IOUtils.copy(is, os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
 
Example 16
Source File: SpiderQueen.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 17
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
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();
    }
}
 
Example 18
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
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();
    }
}