LoadTools.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. var call=client.newCall(request);
  22. t.call=call;
  23. call.enqueue(new Callback() {
  24. @Override
  25. public void onFailure(Call call, IOException e) {
  26. t.uploadError=e.toString();
  27. }
  28. @Override
  29. public void onResponse(Call call, Response response) throws IOException {
  30. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  31. try {
  32. upload(t, response.body().byteStream());
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37. });
  38. }
  39. private static void upload(Transfer t, InputStream is) throws IOException {
  40. RequestBody requestBody = new RequestBody() {
  41. @Override
  42. public MediaType contentType() {
  43. return MEDIA_TYPE_BINARY;
  44. }
  45. @Override
  46. public void writeTo(BufferedSink sink) throws IOException {
  47. is.transferTo(sink.outputStream());
  48. }
  49. };
  50. OkHttpClient client = new OkHttpClient();
  51. Request request = new Request.Builder()
  52. .url(t.to)
  53. .post(requestBody)
  54. .build();
  55. client.newCall(request).enqueue(
  56. new Callback() {
  57. @Override
  58. public void onFailure(Call call, IOException e) {
  59. t.uploadError = e.toString();
  60. }
  61. @Override
  62. public void onResponse(Call call, Response response) throws IOException {
  63. if (!response.isSuccessful()) {
  64. t.uploadError = response.toString();
  65. t.uploadCode = response.code();
  66. }
  67. t.uploadResponseBody = response.body().string();
  68. t.uploadDone = true;
  69. }
  70. }
  71. );
  72. }
  73. public static void startTransfer(Transfer t) throws IOException {
  74. final ProgressListener progressListener = new ProgressListener() {
  75. boolean firstUpdate = true;
  76. @Override
  77. public void update(long bytesRead, long contentLength, boolean done) {
  78. if (done) {
  79. t.downloadDone = true;
  80. } else {
  81. if (firstUpdate) {
  82. firstUpdate = false;
  83. if (contentLength == -1) {
  84. t.contentLength = -1;
  85. } else {
  86. t.contentLength = contentLength;
  87. }
  88. }
  89. t.bytesRead = bytesRead;
  90. t.currentTS = System.currentTimeMillis();
  91. }
  92. }
  93. };
  94. download(t, progressListener);
  95. }
  96. }