@Override public void writeTo(BufferedSink sink) throws IOException { Source source = null; if (this.file != null) { source = Okio.source(this.file); } else if (this.data != null) { source = Okio.source(new ByteArrayInputStream(this.data)); } else if (this.inputStream != null) { source = Okio.source(this.inputStream); } long total = 0; long read, toRead, remain; while (total < contentLength) { remain = contentLength - total; toRead = Math.min(remain, SEGMENT_SIZE); read = source.read(sink.buffer(), toRead); if (read == -1) { break; } total += read; sink.flush(); if (callback != null) { callback.onProgress(MNSRequestTask.this.context.getRequest(), total, contentLength); } } if (source != null) { source.close(); } }
public Builder socket(Socket socket) throws IOException { return socket( socket, ((InetSocketAddress) socket.getRemoteSocketAddress()).getHostName(), Okio.buffer(Okio.source(socket)), Okio.buffer(Okio.sink(socket))); }
/** * Download file from the given response. * * @param response An instance of the Response object * @throws ApiException If fail to read file content from response and write to disk * @return Downloaded file */ public File downloadFileFromResponse(Response response) throws ApiException { try { File file = prepareDownloadFile(response); BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); return file; } catch (IOException e) { throw new ApiException(e); } }
private byte[] generateBody() throws IOException { RequestBody body = mRequest.body(); if (body != null) { ByteArrayOutputStream out = new ByteArrayOutputStream(); BufferedSink sink = Okio.buffer(Okio.sink(out)); body.writeTo(sink); sink.flush(); return out.toByteArray(); } else { return null; } }
@Test public void receiveGoAwayHttp2() throws Exception { peer.setVariantAndClient(HTTP_2, false); // write the mocking script peer.acceptFrame(); // SYN_STREAM 3 peer.acceptFrame(); // SYN_STREAM 5 peer.sendFrame().goAway(3, PROTOCOL_ERROR, EMPTY_BYTE_ARRAY); peer.acceptFrame(); // PING peer.sendFrame().ping(true, 1, 0); peer.acceptFrame(); // DATA STREAM 3 peer.play(); // play it back FramedConnection connection = connection(peer, HTTP_2); FramedStream stream1 = connection.newStream(headerEntries("a", "android"), true, true); FramedStream stream2 = connection.newStream(headerEntries("b", "banana"), true, true); connection.ping().roundTripTime(); // Ensure the GO_AWAY that resets stream2 has been received. BufferedSink sink1 = Okio.buffer(stream1.getSink()); BufferedSink sink2 = Okio.buffer(stream2.getSink()); sink1.writeUtf8("abc"); try { sink2.writeUtf8("abc"); sink2.flush(); fail(); } catch (IOException expected) { assertEquals("stream was reset: REFUSED_STREAM", expected.getMessage()); } sink1.writeUtf8("def"); sink1.close(); try { connection.newStream(headerEntries("c", "cola"), true, true); fail(); } catch (IOException expected) { assertEquals("shutdown", expected.getMessage()); } assertTrue(stream1.isOpen()); assertFalse(stream2.isOpen()); assertEquals(1, connection.openStreamCount()); // verify the peer received what was expected MockSpdyPeer.InFrame synStream1 = peer.takeFrame(); assertEquals(TYPE_HEADERS, synStream1.type); MockSpdyPeer.InFrame synStream2 = peer.takeFrame(); assertEquals(TYPE_HEADERS, synStream2.type); MockSpdyPeer.InFrame ping = peer.takeFrame(); assertEquals(TYPE_PING, ping.type); MockSpdyPeer.InFrame data1 = peer.takeFrame(); assertEquals(TYPE_DATA, data1.type); assertEquals(3, data1.streamId); assertTrue(Arrays.equals("abcdef".getBytes("UTF-8"), data1.data)); }
public Schema load() throws IOException { ImmutableList<Path> directories = this.directories.build(); if (directories.isEmpty()) { throw new IllegalStateException("No directories added."); } Deque<Path> protos = new ArrayDeque<>(this.protos.build()); if (protos.isEmpty()) { // TODO traverse all files in every directory. } Map<Path, ProtoFile> loaded = new LinkedHashMap<>(); while (!protos.isEmpty()) { Path proto = protos.removeFirst(); if (loaded.containsKey(proto)) { continue; } ProtoFileElement element = null; for (Path directory : directories) { if (proto.isAbsolute() && !proto.startsWith(directory)) { continue; } Path resolvedPath = directory.resolve(proto); if (Files.exists(resolvedPath)) { Location location = Location.get(directory.toString(), proto.toString()); try (Source source = Okio.source(resolvedPath)) { String data = Okio.buffer(source).readUtf8(); element = ProtoParser.parse(location, data); } catch (IOException e) { throw new IOException("Failed to load " + proto, e); } break; } } if (element == null) { throw new FileNotFoundException("Failed to locate " + proto + " in " + directories); } ProtoFile protoFile = ProtoFile.get(element); loaded.put(proto, protoFile); // Queue dependencies to be loaded. FileSystem fs = proto.getFileSystem(); for (String importPath : element.imports()) { protos.addLast(fs.getPath(importPath)); } } return new Linker(loaded.values()).link(); }
@Test public void maxFrameSizeHonored() throws Exception { peer.setVariantAndClient(HTTP_2, false); byte[] buff = new byte[peer.maxOutboundDataLength() + 1]; Arrays.fill(buff, (byte) '*'); // write the mocking script peer.acceptFrame(); // SYN_STREAM peer.sendFrame().synReply(false, 3, headerEntries("a", "android")); peer.acceptFrame(); // DATA peer.acceptFrame(); // DATA peer.play(); // play it back FramedConnection connection = connection(peer, HTTP_2); FramedStream stream = connection.newStream(headerEntries("b", "banana"), true, true); BufferedSink out = Okio.buffer(stream.getSink()); out.write(buff); out.flush(); out.close(); MockSpdyPeer.InFrame synStream = peer.takeFrame(); assertEquals(TYPE_HEADERS, synStream.type); MockSpdyPeer.InFrame data = peer.takeFrame(); assertEquals(peer.maxOutboundDataLength(), data.data.length); data = peer.takeFrame(); assertEquals(1, data.data.length); }
@Override public BufferedSource source() { if (mBufferedSource == null) { mBufferedSource = Okio.buffer(source(mResponseBody.source())); } return mBufferedSource; }
@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 static Source source(String proto, Path directory) throws IOException { Path resolvedPath = directory.resolve(proto); if (Files.exists(resolvedPath)) { return Okio.source(resolvedPath); } return null; }
@Override public void writeTo(BufferedSink sink) throws IOException { if (bufferedSink == null) { bufferedSink = Okio.buffer(sink(sink)); } requestBody.writeTo(bufferedSink); bufferedSink.flush(); }
private Schema loadFromDirectories(List<Path> directories) throws IOException { final Deque<String> protos = new ArrayDeque<>(this.protos); if (protos.isEmpty()) { for (final Path directory : directories) { Files.walkFileTree( directory, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { protos.add(directory.relativize(file).toString()); return FileVisitResult.CONTINUE; } }); } } Map<String, ProtoFile> loaded = new LinkedHashMap<>(); while (!protos.isEmpty()) { String proto = protos.removeFirst(); if (loaded.containsKey(proto)) { continue; } ProtoFileElement element = null; for (Path directory : directories) { Source source = source(proto, directory); if (source == null) { continue; } try { Location location = Location.get(directory.toString(), proto); String data = Okio.buffer(source).readUtf8(); element = ProtoParser.parse(location, data); } catch (IOException e) { throw new IOException("Failed to load " + proto + " from " + directory, e); } finally { source.close(); } } if (element == null) { throw new FileNotFoundException("Failed to locate " + proto + " in " + sources); } ProtoFile protoFile = ProtoFile.get(element); loaded.put(proto, protoFile); // Queue dependencies to be loaded. for (String importPath : element.imports()) { protos.addLast(importPath); } } return new Linker(loaded.values()).link(); }
@Override public void writeTo(BufferedSink sink) throws IOException { BufferedSink bufferedSink; countingSink = new CountingSink(sink); bufferedSink = Okio.buffer(countingSink); delegate.writeTo(bufferedSink); bufferedSink.flush(); }
/** * Reads an entry from an input stream. A typical entry looks like this: * * <pre>{@code * http://google.com/foo * GET * 2 * Accept-Language: fr-CA * Accept-Charset: UTF-8 * HTTP/1.1 200 OK * 3 * Content-Type: image/png * Content-Length: 100 * Cache-Control: max-age=600 * }</pre> * * <p>A typical HTTPS file looks like this: * * <pre>{@code * https://google.com/foo * GET * 2 * Accept-Language: fr-CA * Accept-Charset: UTF-8 * HTTP/1.1 200 OK * 3 * Content-Type: image/png * Content-Length: 100 * Cache-Control: max-age=600 * * AES_256_WITH_MD5 * 2 * base64-encoded peerCertificate[0] * base64-encoded peerCertificate[1] * -1 * }</pre> * * The file is newline separated. The first two lines are the URL and the request method. Next * is the number of HTTP Vary request header lines, followed by those lines. * * <p>Next is the response status line, followed by the number of HTTP response header lines, * followed by those lines. * * <p>HTTPS responses also contain SSL session information. This begins with a blank line, and * then a line containing the cipher suite. Next is the length of the peer certificate chain. * These certificates are base64-encoded and appear each on their own line. The next line * contains the length of the local certificate chain. These certificates are also * base64-encoded and appear each on their own line. A length of -1 is used to encode a null * array. */ public Entry(Source in) throws IOException { try { BufferedSource source = Okio.buffer(in); url = source.readUtf8LineStrict(); requestMethod = source.readUtf8LineStrict(); if (readInt(source) == 1) { MediaType contentType = MediaType.parse(source.readUtf8LineStrict()); int contentLength = readInt(source); requestBody = RequestBody.create(contentType, source.readByteArray(contentLength)); } else { requestBody = null; } Headers.Builder varyHeadersBuilder = new Headers.Builder(); int varyRequestHeaderLineCount = readInt(source); for (int i = 0; i < varyRequestHeaderLineCount; i++) { OkHttpAccess.addLenient(varyHeadersBuilder, source.readUtf8LineStrict()); } varyHeaders = varyHeadersBuilder.build(); StatusLine statusLine = StatusLine.parse(source.readUtf8LineStrict()); protocol = statusLine.protocol; code = statusLine.code; message = statusLine.message; Headers.Builder responseHeadersBuilder = new Headers.Builder(); int responseHeaderLineCount = readInt(source); for (int i = 0; i < responseHeaderLineCount; i++) { OkHttpAccess.addLenient(responseHeadersBuilder, source.readUtf8LineStrict()); } responseHeaders = responseHeadersBuilder.build(); if (isHttps()) { String blank = source.readUtf8LineStrict(); if (blank.length() > 0) { throw new IOException("expected \"\" but was \"" + blank + "\""); } String cipherSuite = source.readUtf8LineStrict(); List<Certificate> peerCertificates = readCertificateList(source); List<Certificate> localCertificates = readCertificateList(source); handshake = Handshake.get(cipherSuite, peerCertificates, localCertificates); } else { handshake = null; } } finally { in.close(); } }
private void writeTo(DiskLruCache.Editor editor) throws IOException { BufferedSink sink = Okio.buffer(editor.newSink(RESPONSE_METADATA)); sink.writeUtf8(url); sink.writeByte('\n'); sink.writeUtf8(requestMethod); sink.writeByte('\n'); sink.writeDecimalLong(requestBody == null ? 0 : 1); sink.writeByte('\n'); if (requestBody != null) { sink.writeUtf8(requestBody.contentType().toString()); sink.writeByte('\n'); sink.writeDecimalLong(requestBody.contentLength()); sink.writeByte('\n'); requestBody.writeTo(sink); } sink.writeDecimalLong(varyHeaders.size()); sink.writeByte('\n'); for (int i = 0, size = varyHeaders.size(); i < size; i++) { sink.writeUtf8(varyHeaders.name(i)); sink.writeUtf8(": "); sink.writeUtf8(varyHeaders.value(i)); sink.writeByte('\n'); } sink.writeUtf8(new StatusLine(protocol, code, message).toString()); sink.writeByte('\n'); sink.writeDecimalLong(responseHeaders.size()); sink.writeByte('\n'); for (int i = 0, size = responseHeaders.size(); i < size; i++) { sink.writeUtf8(responseHeaders.name(i)); sink.writeUtf8(": "); sink.writeUtf8(responseHeaders.value(i)); sink.writeByte('\n'); } if (isHttps()) { sink.writeByte('\n'); sink.writeUtf8(handshake.cipherSuite()); sink.writeByte('\n'); writeCertList(sink, handshake.peerCertificates()); writeCertList(sink, handshake.localCertificates()); } sink.close(); }
private Response response(DiskLruCache.Snapshot snapshot) throws IOException { String contentType = responseHeaders.get("Content-Type"); Request cacheRequest = new Request.Builder() .url(url) .method(requestMethod, requestBody) .headers(varyHeaders) .build(); BufferedSource source = Okio.buffer(snapshot.getSource(RESPONSE_BODY)); byte[] bodyContent = source.readByteArray(); MediaType mediaType = contentType == null ? null : MediaType.parse(contentType); return new Response.Builder() .request(cacheRequest) .protocol(protocol) .code(code) .message(message) .headers(responseHeaders) .body(new OkHttpUtil.ReusableResponseBody(mediaType, bodyContent)) .handshake(handshake) .build(); }
private void readMessageFrame() throws IOException { final MediaType type; switch (opcode) { case OPCODE_TEXT: type = WebSocket.TEXT; break; case OPCODE_BINARY: type = WebSocket.BINARY; break; default: throw new ProtocolException("Unknown opcode: " + toHexString(opcode)); } final BufferedSource source = Okio.buffer(framedMessageSource); ResponseBody body = new ResponseBody() { @Override public MediaType contentType() { return type; } @Override public long contentLength() { return -1; } @Override public BufferedSource source() { return source; } }; messageClosed = false; frameCallback.onMessage(body); if (!messageClosed) { throw new IllegalStateException("Listener failed to call close on message payload."); } }
@Test public void clientSendsEmptyDataServerDoesntSendWindowUpdateHttp2() throws Exception { peer.setVariantAndClient(HTTP_2, false); // Write the mocking script. peer.acceptFrame(); // SYN_STREAM peer.acceptFrame(); // DATA peer.sendFrame().synReply(false, 3, headerEntries("a", "android")); peer.play(); // Play it back. FramedConnection connection = connection(peer, HTTP_2); FramedStream client = connection.newStream(headerEntries("b", "banana"), true, true); BufferedSink out = Okio.buffer(client.getSink()); out.write(EMPTY_BYTE_ARRAY); out.flush(); out.close(); // Verify the peer received what was expected. assertEquals(TYPE_HEADERS, peer.takeFrame().type); assertEquals(TYPE_DATA, peer.takeFrame().type); assertEquals(3, peer.frameCount()); }
public static Buffer fileToBytes(File file) throws IOException { Buffer result = new Buffer(); result.writeAll(Okio.source(file)); return result; }
@Override public void writeTo(OutputStream os) throws IOException { BufferedSink sink = Okio.buffer(Okio.sink(os)); requestBody.writeTo(sink); sink.flush(); }
public ForwardingResponseBody(ResponseBody body, InputStream interceptedStream) { mBody = body; mInterceptedSource = Okio.buffer(Okio.source(interceptedStream)); }
@Override public ResponseBody openResponseBody(Response response) throws IOException { Source source = getTransferStream(response); return new RealResponseBody(response.headers(), Okio.buffer(source)); }
@Override protected void onHandleIntent(Intent intent) { NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notification) .setProgress(100, 0, false) .setOngoing(true) .setContentTitle(getResources().getString(R.string.loading_offline_whatif)) .setAutoCancel(true); NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNotificationManager.notify(1, mBuilder.build()); PrefHelper prefHelper = new PrefHelper(getApplicationContext()); File sdCard = prefHelper.getOfflinePath(); File dir = new File(sdCard.getAbsolutePath() + OFFLINE_WHATIF_OVERVIEW_PATH); OkHttpClient client = new OkHttpClient(); Document doc; if (!dir.exists()) dir.mkdirs(); // download overview if (!BuildConfig.DEBUG) { try { doc = Jsoup.connect("https://what-if.xkcd.com/archive/") .userAgent( "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.19 Safari/537.36") .get(); StringBuilder sb = new StringBuilder(); Elements titles = doc.select("h1"); prefHelper.setNewestWhatif(titles.size()); sb.append(titles.first().text()); titles.remove(0); for (Element title : titles) { sb.append("&&"); sb.append(title.text()); } prefHelper.setWhatIfTitles(sb.toString()); Elements img = doc.select("img.archive-image"); int count = 1; for (Element image : img) { String url = image.absUrl("src"); try { Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); File file = new File(dir, String.valueOf(count) + ".png"); BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); response.body().close(); } catch (Exception e) { e.printStackTrace(); } int p = (int) (count / ((float) img.size()) * 100); mBuilder.setProgress(100, p, false); mNotificationManager.notify(1, mBuilder.build()); count++; } if (prefHelper.getNewestWhatIf() == 0) prefHelper.setNewestWhatif(count - 1); } catch (IOException e) { e.printStackTrace(); } // download html int size = prefHelper.getNewestWhatIf(); for (int i = 1; i <= size; i++) { try { doc = Jsoup.connect("https://what-if.xkcd.com/" + String.valueOf(i)).get(); dir = new File(sdCard.getAbsolutePath() + OFFLINE_WHATIF_PATH + String.valueOf(i)); dir.mkdirs(); File file = new File(dir, String.valueOf(i) + ".html"); BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(doc.outerHtml()); writer.close(); // download images int count = 1; for (Element e : doc.select(".illustration")) { try { String url = "http://what-if.xkcd.com" + e.attr("src"); Request request = new Request.Builder().url(url).build(); Response response = client.newCall(request).execute(); dir = new File(sdCard.getAbsolutePath() + OFFLINE_WHATIF_PATH + String.valueOf(i)); if (!dir.exists()) dir.mkdirs(); file = new File(dir, String.valueOf(count) + ".png"); BufferedSink sink = Okio.buffer(Okio.sink(file)); sink.writeAll(response.body().source()); sink.close(); response.body().close(); count++; } catch (Exception e2) { Log.e("article" + i, e2.getMessage()); } } int p = (int) (i / ((float) size) * 100); mBuilder.setProgress(100, p, false); mBuilder.setContentText(i + "/" + size); mNotificationManager.notify(1, mBuilder.build()); } catch (Exception e) { Log.e("article" + i, e.getMessage()); } } } prefHelper.setSunbeamLoaded(); Intent restart = new Intent("de.tap.easy_xkcd.ACTION_COMIC"); restart.putExtra("number", prefHelper.getLastComic()); PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, restart, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder .setContentIntent(pendingIntent) .setContentText(getResources().getString(R.string.not_restart)); mNotificationManager.notify(1, mBuilder.build()); }
public BufferedSource newSource(Path pathRelativeToProjectRoot) throws IOException { return Okio.buffer(Okio.source(getPathForRelativePath(pathRelativeToProjectRoot))); }