@Override public void finishRequest() throws IOException { stream.getSink().close(); }
private void closeQuietly(Sink sink) { try { sink.close(); } catch (Exception ignore) { logger.w(ignore, "Failed to close sink"); } } }
@Override public void finishRequest() throws IOException { stream.getSink().close(); }
@Override public void run() { try { pipe.sink().close(); } catch (IOException e) { throw new AssertionError(); } } }, 1000, TimeUnit.MILLISECONDS);
if (sink != null) { try { sink.close(); } catch (IOException e) { e.printStackTrace();
/** 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; }
/** Use DeflaterOutputStream to deflate source. */ private Buffer deflate(ByteString source) throws IOException { Buffer result = new Buffer(); Sink sink = Okio.sink(new DeflaterOutputStream(result.outputStream())); sink.write(new Buffer().write(source), source.size()); sink.close(); return result; }
@Test public void testReadWriteFile() throws Exception { Path path = temporaryFolder.newFile().toPath(); Sink sink = new FileChannelSink(FileChannel.open(path, w), Timeout.NONE); sink.write(new Buffer().writeUtf8(quote), 317); sink.close(); assertTrue(Files.exists(path)); assertEquals(quote.length(), Files.size(path)); Buffer buffer = new Buffer(); Source source = new FileChannelSource(FileChannel.open(path, r), Timeout.NONE); source.read(buffer, 44); assertThat(buffer.readUtf8()) .isEqualTo("John, the kind of control you're attempting "); source.read(buffer, 31); assertThat(buffer.readUtf8()) .isEqualTo("simply is... it's not possible."); }
@Test public void testAppend() throws Exception { Path path = temporaryFolder.newFile().toPath(); Buffer buffer = new Buffer().writeUtf8(quote); Sink sink; BufferedSource source; sink = new FileChannelSink(FileChannel.open(path, w), Timeout.NONE); sink.write(buffer, 75); sink.close(); assertTrue(Files.exists(path)); assertEquals(75, Files.size(path)); source = Okio.buffer(new FileChannelSource(FileChannel.open(path, r), Timeout.NONE)); assertThat(source.readUtf8()) .isEqualTo("John, the kind of control you're attempting simply is... it's not possible."); sink = new FileChannelSink(FileChannel.open(path, append), Timeout.NONE); sink.write(buffer, buffer.size()); sink.close(); assertTrue(Files.exists(path)); assertEquals(quote.length(), Files.size(path)); source = Okio.buffer(new FileChannelSource(FileChannel.open(path, r), Timeout.NONE)); assertThat(source.readUtf8()) .isEqualTo(quote); } }
@Test public void wrappedSinkCloseTimesOut() throws Exception { Sink sink = new ForwardingSink(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); Sink timeoutSink = timeout.sink(sink); try { timeoutSink.close(); fail(); } catch (InterruptedIOException expected) { } }
@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 sinkCloseDoesntWaitForSourceRead() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().write(new Buffer().writeUtf8("abc"), 3); pipe.sink().close(); BufferedSource bufferedSource = Okio.buffer(pipe.source()); assertEquals("abc", bufferedSource.readUtf8()); assertTrue(bufferedSource.exhausted()); }
@Test public void sinkClose() throws Exception { Pipe pipe = new Pipe(100L); pipe.sink().close(); try { pipe.sink().write(new Buffer().writeUtf8("abc"), 3); fail(); } catch (IllegalStateException expected) { assertEquals("closed", expected.getMessage()); } try { pipe.sink().flush(); fail(); } catch (IllegalStateException expected) { assertEquals("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(); }
@Override public void close() { if (sink != null) { try { sink.close(); } catch (Exception ex) { logger.error("Error while closing tcp transport", ex); } } }
@Override public void close() { if (sink != null) { try { sink.close(); } catch (Exception ex) { logger.error("Error while closing tcp transport", ex); } } }
@Override public void finishRequest() throws IOException { stream.getSink().close(); }
@Override public void finishRequest() throws IOException { stream.getSink().close(); }
@Override public void close() throws IOException { boolean throwOnTimeout = false; enter(); try { sink.close(); throwOnTimeout = true; } catch (IOException e) { throw exit(e); } finally { exit(throwOnTimeout); } }