How to measure data transfer speed using the Java + Google Data API

I am writing a Java client application that uses the Google Data API to upload content to YouTube. I am wondering how I will track the progress of the download using the Google Data API library, I just call service.insert to insert a new video that locks until it finishes.

Has anyone else come up with a solution to monitor download status and count bytes as they are sent?

Thanks for any ideas.

Link

http://code.google.com/apis/youtube/2.0/developers_guide_java.html#Direct_Upload

+3
source share
1 answer

Extend com.google.gdata.data.media.MediaSource writeTo () to enable the bytesRead counter:

     public static void writeTo(MediaSource source, OutputStream outputStream)
        throws IOException {

      InputStream sourceStream = source.getInputStream();
      BufferedOutputStream bos = new BufferedOutputStream(outputStream);
      BufferedInputStream bis = new BufferedInputStream(sourceStream);
      long byteCounter = 0L; 

      try {
        byte [] buf = new byte[2048]; // Transfer in 2k chunks
        int bytesRead = 0;
        while ((bytesRead = bis.read(buf, 0, buf.length)) >= 0) {
          // byte counter
          byteCounter += bytesRead;
          bos.write(buf, 0, bytesRead); 
        }
        bos.flush();
      } finally {
        bis.close();
      }
    }
  }
+1
source

Source: https://habr.com/ru/post/1795659/


All Articles