static void closeQuietly(Source source) { try { source.close(); } catch (Exception ignore) { // ignore } }
@Override public void close() throws IOException { source.close(); } }
@Override public void run() { try { pipe.source().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS);
/** * Parses the {@code .wire} file at {@code base/path} and returns it. Returns null if no such * file exists. */ private ProfileFileElement loadProfileFile(Path base, String path) throws IOException { Source source = source(base, path); if (source == null) return null; try { Location location = Location.get(base.toString(), path); String data = Okio.buffer(source).readUtf8(); return new ProfileParser(location, data).read(); } catch (IOException e) { throw new IOException("Failed to load " + source + " from " + base, e); } finally { source.close(); } }
@Override public void load(@NonNull Picasso picasso, @NonNull Request request, @NonNull Callback callback) { initializeIfFirstTime(); boolean signaledCallback = false; try { Source source = Okio.source(assetManager.open(getFilePath(request))); try { Bitmap bitmap = decodeStream(source, request); signaledCallback = true; callback.onSuccess(new Result(bitmap, DISK)); } finally { try { source.close(); } catch (IOException ignored) { } } } catch (Exception e) { if (!signaledCallback) { callback.onError(e); } } }
throw new IOException("Failed to load " + proto + " from " + base, e); } finally { source.close();
/** Reads all bytes from {@code source} and writes them to {@code sink}. */ private Long readAllAndClose(Source source, Sink sink) throws IOException { long result = 0L; Buffer buffer = new Buffer(); for (long count; (count = source.read(buffer, SEGMENT_SIZE)) != -1L; result += count) { sink.write(buffer, count); } source.close(); sink.close(); return result; }
body.close();
@Test public void sourceClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.source().close(); try { pipe.source().read(new Buffer(), 3); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } }
@Test public void wrappedSourceCloseTimesOut() throws Exception { Source source = new ForwardingSource(new Buffer()) { @Override public void close() throws IOException { try { Thread.sleep(500); } catch (InterruptedException e) { throw new AssertionError(); } } }; AsyncTimeout timeout = new AsyncTimeout(); timeout.timeout(250, TimeUnit.MILLISECONDS); Source timeoutSource = timeout.source(source); try { timeoutSource.close(); fail(); } catch (InterruptedIOException expected) { } }
@Override public ByteString call() throws Exception { Buffer blackhole = new Buffer(); HashingSink hashingSink = HashingSink.sha1(blackhole); Buffer buffer = new Buffer(); while (pipe.source().read(buffer, Long.MAX_VALUE) != -1) { hashingSink.write(buffer, buffer.size()); blackhole.clear(); } pipe.source().close(); return hashingSink.hash(); } });
@Test public void sinkCloseFailsIfReaderIsClosedBeforeAllDataIsRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.source().close(); try { pipe.sink().close(); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); } }
@Test public void sinkFlushFailsIfReaderIsClosedBeforeAllDataIsRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.source().close(); try { pipe.sink().flush(); fail(); } catch (IOException expected) { assertEquals("source is closed", expected.getMessage()); } }
@Test public void test() throws Exception { Pipe pipe = new Pipe(6); pipe.sink().write(new Buffer().writeUtf8("abc"), 3L); Source source = pipe.source(); Buffer readBuffer = new Buffer(); assertEquals(3L, source.read(readBuffer, 6L)); assertEquals("abc", readBuffer.readUtf8()); pipe.sink().close(); assertEquals(-1L, source.read(readBuffer, 6L)); source.close(); }