Calculating number of bytes transferred

C

Chapman

How can I calculate the number of bytes transferred when doing Java I/O?

using i = data.read() or i = data.readLine(), I intend to accumulate the
value of i

I did the following but the result is not correct.

result += i;

Thanks
 
G

Gordon Beaton

How can I calculate the number of bytes transferred when doing Java
I/O?

using i = data.read() or i = data.readLine(), I intend to accumulate
the value of i

I did the following but the result is not correct.

result += i;

You say "bytes" in the subject line, but your reference to readLine()
suggests text instead. Which is it?

Anyway this should count bytes just fine:

InputStream in = ...
byte[] buf = new byte[n];
int result = 0;

while ((i = in.read(buf)) != -1) {
result += i;
}

How wrong was your result? What kind of data are you sending? What
kind of Stream or Reader are you reading with?

Note that InputStream.read() and Reader.read() (without arguments)
don't count of how much was read. They read one byte or character and
return its value. Maybe you've been adding all your data together.

/gordon
 
C

Chapman

Gordon Beaton said:
You say "bytes" in the subject line, but your reference to readLine()
suggests text instead. Which is it?

I had two different modules receiving the inputstream from the network
socket and intended to calculate
the number of bytes received after getting the inputstream. Should I use
read for both instead of one using readLine() ?
Anyway this should count bytes just fine:

InputStream in = ...
byte[] buf = new byte[n];
int result = 0;

while ((i = in.read(buf)) != -1) {
result += i;
}

How wrong was your result? What kind of data are you sending? What
kind of Stream or Reader are you reading with?

The program executed normally except the number of bytes differ by 100
times.
I should get 2000 bytes, but the result was 189000 bytes. I was receiving
data from my network socket
and it is DataInputStream. Should I use use in.read(buf) as well in this
case?

Thanks Gordon for the response.
 
G

Gordon Beaton

I had two different modules receiving the inputstream from the
network socket and intended to calculate the number of bytes
received after getting the inputstream. Should I use read for both
instead of one using readLine() ?

Once again, you are talking about bytes, which suggests that your data
is binary, and also about readLine(), which suggests that it is text.
So which is it? Do you want to count bytes or characters?

If your data is *bytes* (i.e. not text), then you should use an
InputStream of some kind. It does not matter which read() method you
use to read the data (mine was only an example).

If your data is *text* then you should use one of the Reader classes.
Be aware that BufferedReader.readLine() and DataInput.readLine() do
not return newline characters, and that conversion from byte to char
can result in fewer chars than bytes, depending on the particular
encoding method used and the actual data sent.

Also, it is generally not so helpful that you talk about methods
without saying which classes they belong to.
The program executed normally except the number of bytes differ by
100 times. I should get 2000 bytes, but the result was 189000 bytes.
I was receiving data from my network socket and it is
DataInputStream.

Perhaps you should post the code where you receive and count the data.

Finally, how do you know that the correct value is 2000?

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top