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

The following examples show how to use com.hippo.yorozuya.IOUtils#closeQuietly() . 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: 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 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: 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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
Source File: UniFileInputStreamPipe.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mIs);
    mIs = null;
}
 
Example 14
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 15
Source File: FileOutputStreamPipe.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mOs);
    mOs = null;
}
 
Example 16
Source File: SettingsActivity.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
@Override
protected String doInBackground(Void... params) {
    File dir = NMBAppConfig.getCookiesDir();
    if (dir == null) {
        return null;
    }

    SimpleCookieStore cookieStore = NMBApplication.getSimpleCookieStore(mContext);
    List<TransportableHttpCookie> list = cookieStore.getTransportableCookies();

    boolean ok;
    File file = new File(dir, ReadableTime.getFilenamableTime(System.currentTimeMillis()));
    FileWriter fileWriter = null;
    try {
        boolean first = true;
        fileWriter = new FileWriter(file);
        fileWriter.append('[');
        for (TransportableHttpCookie thc : list) {
            if (first) {
                first = false;
            } else {
                fileWriter.append(',');
            }
            fileWriter.append(JSON.toJSONString(thc));
        }
        fileWriter.append(']');
        fileWriter.flush();
        ok = true;
    } catch (Exception e) {
        ok = false;
    } finally {
        IOUtils.closeQuietly(fileWriter);
    }

    if (!ok) {
        file.delete();
        return null;
    } else {
        return file.getPath();
    }
}
 
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();
    }
}
 
Example 19
Source File: UniFileInputStreamPipe.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mIs);
    mIs = null;
}
 
Example 20
Source File: FileInputStreamPipe.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IOUtils.closeQuietly(mIs);
    mIs = null;
}