com.hippo.yorozuya.IOUtils Java Examples

The following examples show how to use com.hippo.yorozuya.IOUtils. 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: 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 #2
Source File: CommonOperations.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public static void ensureNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.createFile(".nomedia");
    if (null == noMedia) {
        return;
    }

    InputStream is = null;
    try {
        is = noMedia.openInputStream();
    } catch (IOException e) {
        // Ignore
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #3
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 #4
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 #5
Source File: Crash.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
public static void saveCrashLog(Context context, Throwable t) {
  File dir = AppConfig.getExternalCrashDir();
  if (dir == null) {
    return;
  }

  String nowString = ReadableTime.getFilenamableTime(System.currentTimeMillis());
  String fileName = "crash-" + nowString + ".log";
  File file = new File(dir, fileName);

  FileWriter fw = null;
  try {
    fw = new FileWriter(file);
    fw.write("TIME=");fw.write(nowString);fw.write("\r\n");
    fw.write("\r\n");
    collectInfo(context, fw);
    fw.write("======== CrashInfo ========\r\n");
    getThrowableInfo(t, fw);
    fw.write("\r\n");
    fw.flush();
  } catch (Exception e) {
    file.delete();
  } finally {
    IOUtils.closeQuietly(fw);
  }
}
 
Example #6
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 #7
Source File: CommonOperations.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
public static void ensureNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.createFile(".nomedia");
    if (null == noMedia) {
        return;
    }

    InputStream is = null;
    try {
        is = noMedia.openInputStream();
    } catch (IOException e) {
        // Ignore
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #8
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 #9
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 #10
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 #11
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 #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: SpiderInfo.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static SpiderInfo read(@Nullable UniFile file) {
    if (file == null) {
        return null;
    }

    InputStream is = null;
    try {
        is = file.openInputStream();
        return read(is);
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #14
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 #15
Source File: UriInputStreamPipe.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
@Override
public void close() {
    if (mIs != null) {
        IOUtils.closeQuietly(mIs);
        mIs = null;
    }
}
 
Example #16
Source File: SpiderInfo.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public void write(@NonNull OutputStream os) {
    OutputStreamWriter writer = null;
    try {
        writer = new OutputStreamWriter(os);
        writer.write(VERSION_STR);
        writer.write(Integer.toString(VERSION));
        writer.write("\n");
        writer.write(String.format("%08x", startPage >= 0 ? startPage : 0)); // Avoid negative
        writer.write("\n");
        writer.write(Long.toString(gid));
        writer.write("\n");
        writer.write(token);
        writer.write("\n");
        writer.write("1");
        writer.write("\n");
        writer.write(Integer.toString(previewPages));
        writer.write("\n");
        writer.write(Integer.toString(previewPerPage));
        writer.write("\n");
        writer.write(Integer.toString(pages));
        writer.write("\n");
        for (int i = 0; i < pTokenMap.size(); i++) {
            Integer key = pTokenMap.keyAt(i);
            String value = pTokenMap.valueAt(i);
            if (TOKEN_FAILED.equals(value) || TextUtils.isEmpty(value)) {
                continue;
            }
            writer.write(Integer.toString(key));
            writer.write(" ");
            writer.write(value);
            writer.write("\n");
        }
        writer.flush();
    } catch (IOException e) {
        // Ignore
    } finally {
        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(os);
    }
}
 
Example #17
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 #18
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 #19
Source File: Crash.java    From Nimingban with Apache License 2.0 5 votes vote down vote up
public static void saveCrashInfo2File(Context context, Throwable ex) {
    File dir = NMBAppConfig.getCrashDir();
    if (dir == null) {
        return;
    }

    String fileName = "crash-" + ReadableTime.getFilenamableTime(System.currentTimeMillis()) + ".log";
    File file = new File(dir, fileName);

    FileWriter fw = null;
    try {
        fw = new FileWriter(file);
        fw.write("TIME=");fw.write(ReadableTime.getFilenamableTime(System.currentTimeMillis()));fw.write("\r\n");
        fw.write("\r\n");
        collectInfo(context, fw);
        fw.write("======== CrashInfo ========\r\n");
        getThrowableInfo(ex, fw);
        fw.write("\r\n");

        fw.flush();

        Settings.putCrashFilename(fileName);
    } catch (Exception e) {
        file.delete();
    } finally {
        IOUtils.closeQuietly(fw);
    }
}
 
Example #20
Source File: RestoreDownloadPreference.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
private RestoreItem getRestoreItem(UniFile file) {
    if (null == file || !file.isDirectory()) {
        return null;
    }
    UniFile siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME);
    if (null == siFile) {
        return null;
    }

    InputStream is = null;
    try {
        is = siFile.openInputStream();
        SpiderInfo spiderInfo = SpiderInfo.read(is);
        if (spiderInfo == null) {
            return null;
        }
        long gid = spiderInfo.gid;
        if (mManager.containDownloadInfo(gid)) {
            return null;
        }
        String token = spiderInfo.token;
        RestoreItem restoreItem = new RestoreItem();
        restoreItem.gid = gid;
        restoreItem.token = token;
        restoreItem.dirname = file.getName();
        return restoreItem;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #21
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 #22
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 #23
Source File: RestoreDownloadPreference.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
private RestoreItem getRestoreItem(UniFile file) {
    if (null == file || !file.isDirectory()) {
        return null;
    }
    UniFile siFile = file.findFile(SpiderQueen.SPIDER_INFO_FILENAME);
    if (null == siFile) {
        return null;
    }

    InputStream is = null;
    try {
        is = siFile.openInputStream();
        SpiderInfo spiderInfo = SpiderInfo.read(is);
        if (spiderInfo == null) {
            return null;
        }
        String gid = spiderInfo.gid;
        if (mManager.containDownloadInfo(gid)) {
            return null;
        }
        String token = spiderInfo.token;
        RestoreItem restoreItem = new RestoreItem();
        restoreItem.gid = gid;
        restoreItem.token = token;
        restoreItem.dirname = file.getName();
        return restoreItem;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #24
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 #25
Source File: SpiderInfo.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static SpiderInfo read(@Nullable UniFile file) {
    if (file == null) {
        return null;
    }

    InputStream is = null;
    try {
        is = file.openInputStream();
        return read(is);
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}
 
Example #26
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 #27
Source File: FileOutputStreamPipe.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mOs);
    mOs = null;
}
 
Example #28
Source File: UniFileInputStreamPipe.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mIs);
    mIs = null;
}
 
Example #29
Source File: FileInputStreamPipe.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mIs);
    mIs = null;
}
 
Example #30
Source File: UniFileOutputStreamPipe.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mOs);
    mOs = null;
}