Refine search
public Builder socket(Socket socket) throws IOException { SocketAddress remoteSocketAddress = socket.getRemoteSocketAddress(); String connectionName = remoteSocketAddress instanceof InetSocketAddress ? ((InetSocketAddress) remoteSocketAddress).getHostName() : remoteSocketAddress.toString(); return socket(socket, connectionName, Okio.buffer(Okio.source(socket)), Okio.buffer(Okio.sink(socket))); }
public ByteString readByteString(File file) throws IOException { try (BufferedSource source = Okio.buffer(Okio.source(file))) { return source.readByteString(); } }
@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(); }
Reader(int headerTableSizeSetting, int maxDynamicTableByteCount, Source source) { this.headerTableSizeSetting = headerTableSizeSetting; this.maxDynamicTableByteCount = maxDynamicTableByteCount; this.source = Okio.buffer(source); }
@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(); }
static File copyTestFile(String file) throws IOException { File newFile = File.createTempFile(file, "test"); InputStream in = QueueTestUtils.class.getResourceAsStream(file); try (BufferedSink sink = Okio.buffer(Okio.sink(newFile))) { sink.writeAll(Okio.source(in)); } assertTrue(newFile.exists()); return newFile; }
@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 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()"); }
static EncryptionResult load(File f) throws Exception { BufferedSource source=Okio.buffer(Okio.source(f)); byte[] iv=source.readByteArray(BLOCK_SIZE); byte[] encrypted=source.readByteArray(); source.close(); return new EncryptionResult(iv, encrypted); }
@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); }
public void writeEnv(File file) throws IOException { try (Sink fileSink = Okio.sink(file); BufferedSink bufferedSink = Okio.buffer(fileSink)) { for (Map.Entry<String, String> entry : System.getenv().entrySet()) { bufferedSink.writeUtf8(entry.getKey()); bufferedSink.writeUtf8("="); bufferedSink.writeUtf8(entry.getValue()); bufferedSink.writeUtf8("\n"); } } }
public void readLines(File file) throws IOException { try (Source fileSource = Okio.source(file); BufferedSource bufferedFileSource = Okio.buffer(fileSource)) { while (true) { String line = bufferedFileSource.readUtf8Line(); if (line == null) break; if (line.contains("square")) { System.out.println(line); } } } }
private void readTheList() throws IOException { byte[] publicSuffixListBytes; byte[] publicSuffixExceptionListBytes; InputStream resource = PublicSuffixDatabase.class.getResourceAsStream(PUBLIC_SUFFIX_RESOURCE); if (resource == null) return; try (BufferedSource bufferedSource = Okio.buffer(new GzipSource(Okio.source(resource)))) { int totalBytes = bufferedSource.readInt(); publicSuffixListBytes = new byte[totalBytes]; bufferedSource.readFully(publicSuffixListBytes); int totalExceptionBytes = bufferedSource.readInt(); publicSuffixExceptionListBytes = new byte[totalExceptionBytes]; bufferedSource.readFully(publicSuffixExceptionListBytes); } synchronized (this) { this.publicSuffixListBytes = publicSuffixListBytes; this.publicSuffixExceptionListBytes = publicSuffixExceptionListBytes; } readCompleteLatch.countDown(); }
@Test public void writableChannelNioFile() throws Exception { File file = temporaryFolder.newFile(); FileChannel fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.WRITE); testWritableByteChannel(fileChannel); BufferedSource emitted = Okio.buffer(Okio.source(file)); assertEquals("defghijklmnopqrstuvw", emitted.readUtf8()); emitted.close(); }