@Override public Sink body() throws IOException { OutputStream body = request.getBody(); return body != null ? Okio.sink(body) : null; }
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))); }
void encode(Bitmap bitmap, File file) throws IOException { try (BufferedSink sink = Okio.buffer(Okio.sink(file))) { encode(bitmap, sink); } }
/** * Respond to CONNECT requests until a SWITCH_TO_SSL_AT_END response is * dispatched. */ private void createTunnel() throws IOException, InterruptedException { BufferedSource source = Okio.buffer(Okio.source(raw)); BufferedSink sink = Okio.buffer(Okio.sink(raw)); while (true) { SocketPolicy socketPolicy = dispatcher.peek().getSocketPolicy(); if (!processOneRequest(raw, source, sink)) { throw new IllegalStateException("Tunnel without any CONNECT!"); } if (socketPolicy == UPGRADE_TO_SSL_AT_END) return; } }
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"); } } }
static void save(File f, EncryptionResult result) throws IOException { BufferedSink sink=Okio.buffer(Okio.sink(f)); sink.write(result.iv); sink.write(result.encrypted); sink.close(); }
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */ private void connectSocket(int connectTimeout, int readTimeout, Call call, EventListener eventListener) throws IOException { Proxy proxy = route.proxy(); Address address = route.address(); rawSocket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP ? address.socketFactory().createSocket() : new Socket(proxy); eventListener.connectStart(call, route.socketAddress(), proxy); rawSocket.setSoTimeout(readTimeout); try { Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout); } catch (ConnectException e) { ConnectException ce = new ConnectException("Failed to connect to " + route.socketAddress()); ce.initCause(e); throw ce; } // The following try/catch block is a pseudo hacky way to get around a crash on Android 7.0 // More details: // https://github.com/square/okhttp/issues/3245 // https://android-review.googlesource.com/#/c/271775/ try { source = Okio.buffer(Okio.source(rawSocket)); sink = Okio.buffer(Okio.sink(rawSocket)); } catch (NullPointerException npe) { if (NPE_THROW_WITH_NULL.equals(npe.getMessage())) { throw new IOException(npe); } } }
/** Encode {@code value} and write it to {@code stream}. */ public final void encode(OutputStream stream, E value) throws IOException { checkNotNull(value, "value == null"); checkNotNull(stream, "stream == null"); BufferedSink buffer = Okio.buffer(Okio.sink(stream)); encode(buffer, value); buffer.emit(); }
Sink out = Okio.sink(System.out); BufferedSource source = response.body().source(); while (!source.exhausted()) {
@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(); }
private void enableCompression(Socket socket) throws IOException { InputStream inputStream = new InflaterInputStream(socket.getInputStream(), new Inflater(true)); input = Okio.buffer(Okio.source(inputStream)); ZOutputStream outputStream = new ZOutputStream(socket.getOutputStream(), JZlib.Z_BEST_SPEED, true); outputStream.setFlushMode(JZlib.Z_PARTIAL_FLUSH); output = Okio.buffer(Okio.sink(outputStream)); }
/** 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 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 writeWithoutTimeout() throws Exception { Socket socket = socket(0, ONE_MB); Sink sink = Okio.buffer(Okio.sink(socket)); sink.timeout().timeout(500, TimeUnit.MILLISECONDS); byte[] data = new byte[ONE_MB]; sink.write(new Buffer().write(data), data.length); sink.flush(); socket.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 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 sinkFromOutputStream() throws Exception { Buffer data = new Buffer(); data.writeUtf8("a"); data.writeUtf8(repeat('b', 9998)); data.writeUtf8("c"); ByteArrayOutputStream out = new ByteArrayOutputStream(); Sink sink = Okio.sink(out); sink.write(data, 3); assertEquals("abb", out.toString("UTF-8")); sink.write(data, data.size()); assertEquals("a" + repeat('b', 9998) + "c", out.toString("UTF-8")); }
@Test public void gzipSource() throws Exception { Pipe pipe = new Pipe(1024 * 1024); OutputStream gzipOut = new GZIPOutputStream(Okio.buffer(pipe.sink()).outputStream()) { { // Disable compression to speed up a slow test. Improved from 141s to 33s on one machine. def.setLevel(Deflater.NO_COMPRESSION); } }; Future<Long> future = readAllAndCloseAsync( randomSource(FOUR_GIB_PLUS_ONE), Okio.sink(gzipOut)); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GzipSource gzipSource = new GzipSource(pipe.source()); readAllAndClose(gzipSource, hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }