LoadTools.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.acdp.transceivr;
  2. import okhttp3.*;
  3. import okio.BufferedSink;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. public class LoadTools {
  7. public static final MediaType MEDIA_TYPE_BINARY
  8. = MediaType.parse("application/octet-stream");
  9. private static void download(Transfer t, ProgressListener progressListener) throws IOException {
  10. Request request = new Request.Builder()
  11. .url(t.from)
  12. .build();
  13. OkHttpClient client = new OkHttpClient.Builder()
  14. .addNetworkInterceptor(chain -> {
  15. Response originalResponse = chain.proceed(chain.request());
  16. return originalResponse.newBuilder()
  17. .body(new ProgressResponseBody(originalResponse.body(), progressListener))
  18. .build();
  19. })
  20. .build();
  21. client.newCall(request).enqueue(new Callback() {
  22. @Override
  23. public void onFailure(Call call, IOException e) {
  24. t.uploadError=e.toString();
  25. }
  26. @Override
  27. public void onResponse(Call call, Response response) throws IOException {
  28. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  29. try {
  30. upload(t, response.body().byteStream());
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. });
  36. }
  37. private static void upload(Transfer t, InputStream is) throws IOException {
  38. RequestBody requestBody = new RequestBody() {
  39. @Override
  40. public MediaType contentType() {
  41. return MEDIA_TYPE_BINARY;
  42. }
  43. @Override
  44. public void writeTo(BufferedSink sink) throws IOException {
  45. is.transferTo(sink.outputStream());
  46. }
  47. };
  48. OkHttpClient client = new OkHttpClient();
  49. Request request = new Request.Builder()
  50. .url(t.to)
  51. .post(requestBody)
  52. .build();
  53. client.newCall(request).enqueue(
  54. new Callback() {
  55. @Override
  56. public void onFailure(Call call, IOException e) {
  57. t.uploadError = e.toString();
  58. }
  59. @Override
  60. public void onResponse(Call call, Response response) throws IOException {
  61. if (!response.isSuccessful()) {
  62. t.uploadError = response.toString();
  63. t.uploadCode = response.code();
  64. }
  65. t.uploadResponseBody = response.body().string();
  66. t.uploadDone = true;
  67. }
  68. }
  69. );
  70. }
  71. public static void startTransfer(Transfer t) throws IOException {
  72. final ProgressListener progressListener = new ProgressListener() {
  73. boolean firstUpdate = true;
  74. @Override
  75. public void update(long bytesRead, long contentLength, boolean done) {
  76. if (done) {
  77. t.downloadDone = true;
  78. } else {
  79. if (firstUpdate) {
  80. firstUpdate = false;
  81. if (contentLength == -1) {
  82. t.contentLength = -1;
  83. } else {
  84. t.contentLength = contentLength;
  85. }
  86. }
  87. t.bytesRead = bytesRead;
  88. t.currentTS = System.currentTimeMillis();
  89. }
  90. }
  91. };
  92. download(t, progressListener);
  93. }
  94. }