By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

Count number of bytes downloaded

P: 7
Hi

I want to know how we can keep track of number bytes downloaded at client side.
I want to make a program that will keep track of number of bytes downloaded
after downloading is completed.
Also, if user cancel the downloading then also it should stored the number of bytes downloaded at client side so that I can resume downloading when client will ask for downloading
Sep 11 '07 #1
Share this Question
Share on Google+
4 Replies


Nepomuk
Expert 2.5K+
P: 3,112
Hi

I want to know how we can keep track of number bytes downloaded at client side.
I want to make a program that will keep track of number of bytes downloaded
after downloading is completed.
Also, if user cancel the downloading then also it should stored the number of bytes downloaded at client side so that I can resume downloading when client will ask for downloading
How do you download those bytes? With a Socket? A URLConnection? FTP? You'll have to give us more information, so that we can help you.

Greetings,
Nepomuk
Sep 11 '07 #2

P: 7
Actually I want it using URL so that I can include this in any web page
Sep 11 '07 #3

Nepomuk
Expert 2.5K+
P: 3,112
Actually I want it using URL so that I can include this in any web page
It depends. If you only want to download from a source that runs a servlet written by you, you'll have to download packages of bytes (e.g. an Array of 1024 Bytes) one by one and count, how many packages you've downloaded. Of course for that, the servlet must offer the packages one by one.

If however you want to download from any source you chose, you have a problem, as I don't think there is any URL class (at least no standard one) with such a function preimplemented.

I can't think of anything else right now, maybe someone else has a better idea? (Or can correct me, if I'm wrong?)

Greetings,
Nepomuk
Sep 11 '07 #4

Expert 10K+
P: 11,448
I can't think of anything else right now, maybe someone else has a better idea? (Or can correct me, if I'm wrong?)

Greetings,
Nepomuk
Be lazy and think wrappers/decorators; at the end an InputStream is used;
here's the CountingInputStream wrapper class:

Expand|Select|Wrap|Line Numbers
  1. public class CountingInputStream extends InputStream {
  2.    protected long count;
  3.    private InputStream is;
  4.    public CountedInputStream(InputStream is) { this.is= is; }
  5.    public long getCount() { return count; }
  6.    public int read() throws IOException {
  7.       int i= is.read();
  8.       if (i >= 0) count++;
  9.       return i;
  10.    }
  11. }
  12.  
That's all you need; wrap the real InputStream in this thingy, use it for reading and
let it count for you.

Note that I made the 'count' member protected just in case someone gets
enthousiastic and wants to build a ResetableCountingInputStream ;-)

kind regards,

Jos
Sep 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.