How to measure upload bitrate using Java+Google Data API
I'm writing a Java client application which uses the Google Data API to upload things to youtube. I'm wondering how I would go about tracking the progress of an upload, using the Google Data API library I simply call service.insert to insert a new video, which blocks until it is complete.
Has anyone else come up with a solution to monitor the status of the upload and count the bytes as they are sent?
Thanks for any ideas
Link:
http://code.google.com/a开发者_Python百科pis/youtube/2.0/developers_guide_java.html#Direct_Upload
Extend com.google.gdata.data.media.MediaSource writeTo() to include a counter of bytesRead:
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();
}
}
}
精彩评论