Calculating number of bytes transferred

Discussion in 'Java' started by Chapman, Sep 16, 2003.

  1. Chapman

    Chapman Guest

    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
     
    Chapman, Sep 16, 2003
    #1
    1. Advertising

  2. On Tue, 16 Sep 2003 03:06:30 GMT, Chapman wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 16, 2003
    #2
    1. Advertising

  3. Chapman

    Chapman Guest

    "Gordon Beaton" <> wrote in message
    news:3f66b646$...
    > On Tue, 16 Sep 2003 03:06:30 GMT, Chapman wrote:
    > > 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?


    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.

    > 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
    >
    > --
    > [ do not email me copies of your followups ]
    > g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Chapman, Sep 16, 2003
    #3
  4. On Tue, 16 Sep 2003 13:03:44 GMT, Chapman wrote:
    > 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

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Sep 16, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. willwork4dsl
    Replies:
    1
    Views:
    375
    Tommy
    Feb 4, 2004
  2. Jeff
    Replies:
    2
    Views:
    446
    Alexey Smirnov
    Apr 3, 2009
  3. Dario Beraldi

    SFTP: max bytes to be transferred

    Dario Beraldi, Dec 1, 2010, in forum: Python
    Replies:
    0
    Views:
    336
    Dario Beraldi
    Dec 1, 2010
  4. Replies:
    0
    Views:
    722
  5. R.A.M.

    GridView with changes transferred to db at end-of-edit

    R.A.M., Dec 20, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    139
    R.A.M.
    Dec 20, 2006
Loading...

Share This Page