@Override public Source source(File file) throws FileNotFoundException { return Okio.source(file); }
@Override public void writeTo(BufferedSink sink) throws IOException { try (Source source = Okio.source(file)) { sink.writeAll(source); } } };
private Buffer fileToBytes(File file) throws IOException { Buffer result = new Buffer(); result.writeAll(Okio.source(file)); return result; }
Source getSource(Uri uri) throws FileNotFoundException { ContentResolver contentResolver = context.getContentResolver(); InputStream inputStream = contentResolver.openInputStream(uri); if (inputStream == null) { throw new FileNotFoundException("can't open input stream, uri: " + uri); } return Okio.source(inputStream); }
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))); }
/** * Creates an OkHttp Response.Body containing the supplied information. */ private static ResponseBody createOkBody(final Headers okHeaders, final CacheResponse cacheResponse) throws IOException { final BufferedSource body = Okio.buffer(Okio.source(cacheResponse.getBody())); return new ResponseBody() { @Override public MediaType contentType() { String contentTypeHeader = okHeaders.get("Content-Type"); return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader); } @Override public long contentLength() { return HttpHeaders.contentLength(okHeaders); } @Override public BufferedSource source() { return body; } }; }
/** * Creates an OkHttp Response.Body containing the supplied information. */ private static ResponseBody createOkBody(final URLConnection urlConnection) throws IOException { if (!urlConnection.getDoInput()) { return null; } final BufferedSource body = Okio.buffer(Okio.source(urlConnection.getInputStream())); return new ResponseBody() { @Override public MediaType contentType() { String contentTypeHeader = urlConnection.getContentType(); return contentTypeHeader == null ? null : MediaType.parse(contentTypeHeader); } @Override public long contentLength() { String s = urlConnection.getHeaderField("Content-Length"); return stringToLong(s); } @Override public BufferedSource source() { return body; } }; }
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); } } } }
public ByteString readByteString(File file) throws IOException { try (BufferedSource source = Okio.buffer(Okio.source(file))) { return source.readByteString(); } }
public Buffer readBuffer(File file) throws IOException { try (Source source = Okio.source(file)) { Buffer buffer = new Buffer(); buffer.writeAll(source); return buffer; } }
/** * 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; } }
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(); }
/** 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); } } }
@Test public void sourceFromInputStreamBounds() throws Exception { Source source = Okio.source(new ByteArrayInputStream(new byte[100])); try { source.read(new Buffer(), -1); fail(); } catch (IllegalArgumentException expected) { } }
@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(); }
@Test public void readWithoutTimeout() throws Exception { Socket socket = socket(ONE_MB, 0); BufferedSource source = Okio.buffer(Okio.source(socket)); source.timeout().timeout(5000, TimeUnit.MILLISECONDS); source.require(ONE_MB); socket.close(); }
@Test public void readWithTimeout() throws Exception { Socket socket = socket(0, 0); BufferedSource source = Okio.buffer(Okio.source(socket)); source.timeout().timeout(250, TimeUnit.MILLISECONDS); try { source.require(ONE_MB); fail(); } catch (SocketTimeoutException expected) { } 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 gzipSink() throws Exception { Pipe pipe = new Pipe(1024 * 1024); GzipSink gzipSink = new GzipSink(pipe.sink()); // Disable compression to speed up a slow test. Improved from 141s to 35s on one machine. gzipSink.deflater().setLevel(Deflater.NO_COMPRESSION); Future<Long> future = readAllAndCloseAsync(randomSource(FOUR_GIB_PLUS_ONE), gzipSink); HashingSink hashingSink = HashingSink.sha256(Okio.blackhole()); GZIPInputStream gzipIn = new GZIPInputStream(Okio.buffer(pipe.source()).inputStream()); readAllAndClose(Okio.source(gzipIn), hashingSink); assertEquals(FOUR_GIB_PLUS_ONE, (long) future.get()); assertEquals(SHA256_RANDOM_FOUR_GIB_PLUS_1, hashingSink.hash()); }