@Override public synchronized void close() throws IOException { closed = true; sink.close(); }
@Override public void close() throws IOException { closed = true; if (expectedContentLength != -1L && bytesReceived < expectedContentLength) { throw new ProtocolException("expected " + expectedContentLength + " bytes but received " + bytesReceived); } sink.close(); } };
/** * Flushes and closes this writer and the underlying {@link Sink}. * * @throws JsonDataException if the JSON document is incomplete. */ @Override public void close() throws IOException { sink.close(); int size = stackSize; if (size > 1 || size == 1 && scopes[size - 1] != NONEMPTY_DOCUMENT) { throw new IOException("Incomplete document"); } stackSize = 0; }
@Override public void writeTo(BufferedSink sink) throws IOException { BufferedSink gzipSink = Okio.buffer(new GzipSink(sink)); body.writeTo(gzipSink); gzipSink.close(); } };
/** Closes this cache. Stored values will remain on the filesystem. */ @Override public synchronized void close() throws IOException { if (!initialized || closed) { closed = true; return; } // Copying for safe iteration. for (Entry entry : lruEntries.values().toArray(new Entry[lruEntries.size()])) { if (entry.currentEditor != null) { entry.currentEditor.abort(); } } trimToSize(); journalWriter.close(); journalWriter = null; closed = true; }
@Override public void onResponse(Call call, Response response) throws IOException { File downloadedFile = new File(context.getCacheDir(), filename); BufferedSink sink = Okio.buffer(Okio.sink(downloadedFile)); sink.writeAll(response.body().source()); sink.close(); }
@Override public void run() { try { sink.writeUtf8("Numbers\n"); sink.writeUtf8("-------\n"); for (int i = 2; i <= 997; i++) { System.out.println(i); Thread.sleep(10); sink.writeUtf8(String.format(" * %s = %s\n", i, factor(i))); } sink.close(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } }
@Nullable @Override public byte[] body() throws IOException { RequestBody body = mRequest.body(); if (body == null) { return null; } OutputStream out = mRequestBodyHelper.createBodySink(firstHeaderValue("Content-Encoding")); BufferedSink bufferedSink = Okio.buffer(Okio.sink(out)); try { body.writeTo(bufferedSink); } finally { bufferedSink.close(); } return mRequestBodyHelper.getDisplayBody(); }
@Test public void sinkIsOpen() throws Exception { BufferedSink sink = Okio.buffer((Sink) new Buffer()); assertTrue(sink.isOpen()); sink.close(); assertFalse(sink.isOpen()); }
@Override public long read(Buffer sink, long byteCount) throws IOException { long bytesRead; try { bytesRead = source.read(sink, byteCount); } catch (IOException e) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheRequest.abort(); // Failed to write a complete cache response. } throw e; } if (bytesRead == -1) { if (!cacheRequestClosed) { cacheRequestClosed = true; cacheBody.close(); // The cache response is complete! } return -1; } sink.copyTo(cacheBody.buffer(), sink.size() - bytesRead, bytesRead); cacheBody.emitCompleteSegments(); return bytesRead; }
@Test public void readableChannelNioFile() throws Exception { File file = temporaryFolder.newFile(); BufferedSink initialData = Okio.buffer(Okio.sink(file)); initialData.writeUtf8("abcdefghijklmnopqrstuvwxyz"); initialData.close(); FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ); testReadableByteChannel(fileChannel); }
@Test public void closeWithExceptionWhenWriting() throws IOException { MockSink mockSink = new MockSink(); mockSink.scheduleThrow(0, new IOException()); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.writeByte('a'); try { bufferedSink.close(); fail(); } catch (IOException expected) { } mockSink.assertLog("write([text=a], 1)", "close()"); }
@Test public void closeWithExceptionWhenClosing() throws IOException { MockSink mockSink = new MockSink(); mockSink.scheduleThrow(1, new IOException()); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.writeByte('a'); try { bufferedSink.close(); fail(); } catch (IOException expected) { } mockSink.assertLog("write([text=a], 1)", "close()"); }
@Test public void readWritePath() throws Exception { Path path = temporaryFolder.newFile().toPath(); BufferedSink sink = Okio.buffer(Okio.sink(path)); sink.writeUtf8("Hello, java.nio file!"); sink.close(); assertTrue(Files.exists(path)); assertEquals(21, Files.size(path)); BufferedSource source = Okio.buffer(Okio.source(path)); assertEquals("Hello, java.nio file!", source.readUtf8()); source.close(); }
@Test public void closeWithExceptionWhenWritingAndClosing() throws IOException { MockSink mockSink = new MockSink(); mockSink.scheduleThrow(0, new IOException("first")); mockSink.scheduleThrow(1, new IOException("second")); BufferedSink bufferedSink = Okio.buffer(mockSink); bufferedSink.writeByte('a'); try { bufferedSink.close(); fail(); } catch (IOException expected) { assertEquals("first", expected.getMessage()); } mockSink.assertLog("write([text=a], 1)", "close()"); }
@Test public void appendFile() throws Exception { File file = temporaryFolder.newFile(); BufferedSink sink = Okio.buffer(Okio.appendingSink(file)); sink.writeUtf8("Hello, "); sink.close(); assertTrue(file.exists()); assertEquals(7, file.length()); sink = Okio.buffer(Okio.appendingSink(file)); sink.writeUtf8("java.io file!"); sink.close(); assertEquals(20, file.length()); BufferedSource source = Okio.buffer(Okio.source(file)); assertEquals("Hello, java.io file!", source.readUtf8()); source.close(); }
@Test public void readWriteFile() throws Exception { File file = temporaryFolder.newFile(); BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeUtf8("Hello, java.io file!"); sink.close(); assertTrue(file.exists()); assertEquals(20, file.length()); BufferedSource source = Okio.buffer(Okio.source(file)); assertEquals("Hello, java.io file!", source.readUtf8()); source.close(); }
@Test public void closeEmitsBufferedBytes() throws IOException { sink.writeByte('a'); sink.close(); assertEquals('a', data.readByte()); }
journalWriter.close();
sink.writeUtf8(handshake.tlsVersion().javaName()).writeByte('\n'); sink.close();