public static long discardForLength(InputStream stream) throws IOException {
    CountingInputStream counter = new CountingInputStream(stream);
    while (true) {
      if (counter.skip(Integer.MAX_VALUE) < Integer.MAX_VALUE) {
        counter.close();
        return counter.getCount();
      }
    }
  }
Returns byte size of stream, discarding its contents and closing the stream.
portability-spi-api/src/main/java/org/datatransferproject/spi/api/transport/DiscardingStreamCounter.java