11package org .datatransferproject .transfer ;
22
3- import java . io . IOException ;
4- import java . io . InputStream ;
3+ import static org . datatransferproject . spi . api . transport . DiscardingStreamCounter . discardForLength ;
4+
55import java .util .Collection ;
66import java .util .LinkedHashMap ;
77import java .util .Map ;
@@ -19,7 +19,9 @@ public class CallableSizeCalculator implements Callable<Map<String, Long>> {
1919 private final Collection <? extends DownloadableItem > items ;
2020
2121 public CallableSizeCalculator (
22- UUID jobId , ConnectionProvider connectionProvider , Collection <? extends DownloadableItem > items ) {
22+ UUID jobId ,
23+ ConnectionProvider connectionProvider ,
24+ Collection <? extends DownloadableItem > items ) {
2325 this .jobId = Objects .requireNonNull (jobId );
2426 this .connectionProvider = Objects .requireNonNull (connectionProvider );
2527 this .items = Objects .requireNonNull (items );
@@ -32,26 +34,12 @@ public Map<String, Long> call() throws Exception {
3234 InputStreamWrapper stream = connectionProvider .getInputStreamForItem (jobId , item );
3335 long size = stream .getBytes ();
3436 if (size <= 0 ) {
35- size = computeSize (stream );
37+ size = discardForLength (stream . getStream () );
3638 }
3739
3840 result .put (item .getIdempotentId (), size );
3941 }
4042
4143 return result ;
4244 }
43-
44- // Reads the input stream in full
45- private Long computeSize (InputStreamWrapper stream ) throws IOException {
46- long size = 0 ;
47- try (InputStream inStream = stream .getStream ()) {
48- byte [] buffer = new byte [1024 * 1024 ]; // 1MB
49- int chunkBytesRead ;
50- while ((chunkBytesRead = inStream .read (buffer )) != -1 ) {
51- size += chunkBytesRead ;
52- }
53- }
54-
55- return size ;
56- }
5745}
0 commit comments