21700: Install Bundler system-wide in Rails postinst
[arvados.git] / sdk / java-v2 / src / main / java / org / arvados / client / api / client / CountingRequestBody.java
1 /*
2  * Copyright (C) The Arvados Authors. All rights reserved.
3  *
4  * SPDX-License-Identifier: AGPL-3.0 OR Apache-2.0
5  *
6  */
7
8 package org.arvados.client.api.client;
9
10 import okhttp3.MediaType;
11 import okhttp3.RequestBody;
12 import org.slf4j.Logger;
13
14 abstract class CountingRequestBody<T> extends RequestBody {
15
16     protected static final int SEGMENT_SIZE = 2048; // okio.Segment.SIZE
17     protected static final MediaType CONTENT_BINARY = MediaType.parse(com.google.common.net.MediaType.OCTET_STREAM.toString());
18
19     protected final ProgressListener listener;
20
21     protected final T requestBodyData;
22
23     CountingRequestBody(T file, final ProgressListener listener) {
24         this.requestBodyData = file;
25         this.listener = listener;
26     }
27
28     @Override
29     public MediaType contentType() {
30         return CONTENT_BINARY;
31     }
32
33     static class TransferData {
34
35         private final Logger log = org.slf4j.LoggerFactory.getLogger(TransferData.class);
36         private int progressValue;
37         private long totalSize;
38
39         TransferData(long totalSize) {
40             this.progressValue = 0;
41             this.totalSize = totalSize;
42         }
43
44         void updateTransferProgress(long transferred) {
45             float progress = (transferred / (float) totalSize) * 100;
46             if (progressValue != (int) progress) {
47                 progressValue = (int) progress;
48                 log.debug("{} / {} / {}%", transferred, totalSize, progressValue);
49             }
50         }
51     }
52 }