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; } }
@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(); }
@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()); }
@Override public void writeTo(OutputStream os) throws IOException { BufferedSink sink = Okio.buffer(Okio.sink(os)); requestBody.writeTo(sink); sink.flush(); }