@Test public void testFailsGracefullyOnIOException() throws Exception { InputStream is = new InputStream() { @Override public int read() throws IOException { throw new IOException("exception for test"); } }; InputStreamWriter writer = new InputStreamWriter(is); File file = new File(getContext().getCacheDir(), "test_file"); boolean result = writer.write(file); Assert.assertFalse(result); }
@Override public boolean write(File file) { FileOutputStream stream = null; FileLock lock = null; try { stream = new FileOutputStream(file); lock = stream.getChannel().lock(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = in.read(buffer)) != -1) { stream.write(buffer, 0, len); } return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { if (lock != null) { try { lock.release(); } catch (IOException e) { Log.w("InputStreamWriter", "Couldn't release lock"); } } IOUtils.close(stream); } }
InputStreamWriter implements DiskCache.Writer { @Override public boolean write(File file) { FileOutputStream stream = null; FileLock lock = null; try { stream = new FileOutputStream(file); lock = stream.getChannel().lock(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = in.read(buffer)) != -1) { stream.write(buffer, 0, len); } return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { if (lock != null) { try { lock.release(); } catch (IOException e) { Log.w("InputStreamWriter", "Couldn't release lock"); } } IOUtils.close(stream); } } }
InputStreamWriter implements DiskCache.Writer { @Override public boolean write(File file) { FileOutputStream stream = null; FileLock lock = null; try { stream = new FileOutputStream(file); lock = stream.getChannel().lock(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = in.read(buffer)) != -1) { stream.write(buffer, 0, len); } return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { if (lock != null) { try { lock.release(); } catch (IOException e) { Log.w("InputStreamWriter", "Couldn't release lock"); } } IOUtils.close(stream); } } InputStreamWriter(InputStream in); }
InputStreamWriter implements DiskCache.Writer { @Override public boolean write(File file) { FileOutputStream stream = null; FileLock lock = null; try { stream = new FileOutputStream(file); lock = stream.getChannel().lock(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = in.read(buffer)) != -1) { stream.write(buffer, 0, len); } return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { if (lock != null) { try { lock.release(); } catch (IOException e) { Log.w("InputStreamWriter", "Couldn't release lock"); } } IOUtils.close(stream); } } InputStreamWriter(InputStream in); @Override boolean write(File file); }
InputStreamWriter implements DiskCache.Writer { @Override public boolean write(File file) { FileOutputStream stream = null; FileLock lock = null; try { stream = new FileOutputStream(file); lock = stream.getChannel().lock(); int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int len = 0; while ((len = in.read(buffer)) != -1) { stream.write(buffer, 0, len); } return true; } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { if (lock != null) { try { lock.release(); } catch (IOException e) { Log.w("InputStreamWriter", "Couldn't release lock"); } } IOUtils.close(stream); } } InputStreamWriter(InputStream in); @Override boolean write(File file);  }