List of usage examples for android.os CancellationSignal throwIfCanceled
public void throwIfCanceled()
From source file:net.sf.fdshare.RootFileProvider.java
/** * {@inheritDoc}//from w ww.j a va2s. co m * * Note, that cancellation is inherently racy. The caller must close whatever Cursor may be returned regardless. */ @Override @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, CancellationSignal signal) { Cursor result = null; try (Closeable cancellation = new ThreadInterrupter(signal)) { result = super.query(uri, projection, selection, selectionArgs, sortOrder); return result; } catch (IOException ioe) { return null; } finally { if (signal != null && signal.isCanceled()) { if (result != null) { result.close(); } // override whatever exception have resulted from interruption signal.throwIfCanceled(); } } }
From source file:org.alfresco.mobile.android.application.providers.storage.StorageAccessDocumentsProvider.java
public static boolean copyFile(InputStream src, long size, File dest, CancellationSignal signal) { IOUtils.ensureOrCreatePathAndFile(dest); OutputStream os = null;//from w ww.j a v a 2s . c o m boolean copied = true; int downloaded = 0; try { os = new BufferedOutputStream(new FileOutputStream(dest)); byte[] buffer = new byte[MAX_BUFFER_SIZE]; while (size - downloaded > 0) { if (size - downloaded < MAX_BUFFER_SIZE) { buffer = new byte[(int) (size - downloaded)]; } int read = src.read(buffer); if (read == -1) { break; } os.write(buffer, 0, read); downloaded += read; if (signal != null && signal.isCanceled()) { signal.throwIfCanceled(); } } } catch (Exception e) { Log.e(TAG, Log.getStackTraceString(e)); copied = false; } finally { IOUtils.closeStream(src); IOUtils.closeStream(os); } return copied; }
From source file:net.sf.fdshare.RootFileProvider.java
/** * {@inheritDoc}//w w w. ja va 2 s. c o m * * Note, that cancellation is inherently racy. The caller must close whatever descriptor may be returned regardless. */ @Override @SuppressLint("NewApi") @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public ParcelFileDescriptor openFile(Uri uri, String mode, CancellationSignal signal) throws FileNotFoundException { ParcelFileDescriptor result = null; try (Closeable cancellation = new ThreadInterrupter(signal)) { result = super.openFile(uri, mode); return result; } catch (IOException ioe) { throw new FileNotFoundException(ioe.getMessage()); } finally { if (signal != null && signal.isCanceled()) { if (result != null) { try { result.close(); } catch (IOException ignored) { } } // override whatever exception have resulted from interruption signal.throwIfCanceled(); } } }