Re: Buffered reading seems to corrupt data stream

Discussion in 'Java' started by markspace, Oct 3, 2011.

  1. markspace

    markspace Guest

    On 10/3/2011 7:58 AM, Qu0ll wrote:
    > I am trying to repeatedly send a byte array from a server to a client
    > where both the writing and the reading is done in a buffered manner with
    > the following code on the server:
    >
    > DataOutputStream dos = new DataOutputStream(os);
    > dos.writeInt(bytes.length);
    > dos.write(bytes);
    > dos.flush();



    I got to repeat the request for an SSCCE. The code above is certainly
    not where the problem is. The problems is certainly in some other area
    which you haven't shown us.

    As a suggestion, scan the input above before you write it. You'll
    certainly find the same values going in as you are reading back out.
    Then trace the path up the input side to determine what is actually
    writing that data in the the array "bytes".
     
    markspace, Oct 3, 2011
    #1
    1. Advertising

  2. markspace

    Lew Guest

    markspace wrote:
    > Qu0ll wrote:
    >> I am trying to repeatedly send a byte array from a server to a client
    >> where both the writing and the reading is done in a buffered manner with
    >> the following code on the server:
    >>
    >> DataOutputStream dos = new DataOutputStream(os);
    >> dos.writeInt(bytes.length);
    >> dos.write(bytes);
    >> dos.flush();

    >
    > I got to repeat the request for an SSCCE. The code above is certainly
    > not where the problem is. The problems is certainly in some other area
    > which you haven't shown us.
    >
    > As a suggestion, scan the input above before you write it. You'll
    > certainly find the same values going in as you are reading back out.
    > Then trace the path up the input side to determine what is actually
    > writing that data in the the array "bytes".


    Just this once the question was answerable (and answered, elsethread) without the SSCCE, but truthfully the respondents had to infer the context that an SSCCE would've made explicit. They just happened to guess right. (Well, it was fairly obvious.)

    But to the OP and the rest of us, it really is a good idea to package your example as an SSCCE. Just because we think something is obvious doesn't mean everyone does.

    Coincidentally I was just facing the same problem in a project. I want to independently dip into an input stream without trashing the stream locationby buffering. In my case I did away with the buffering, but for the OP's case here it is much easier to preserve the stream at wider scope than theyoriginally had it.

    --
    Lew
     
    Lew, Oct 4, 2011
    #2
    1. Advertising

  3. markspace

    Lew Guest

    Peter Duniho wrote:
    > Lew wrote:
    >> Just this once the question was answerable (and answered, elsethread) without the SSCCE [...]

    >
    > By that standard, _all_ questions are answerable without an SSCCE. A
    > person attempting an answer can always make a guess, and that guess
    > always has a non-zero probability of being correct.


    You're preaching to the choir!

    I did point out that the omission only worked "just this once", not recommend it.

    And as you point out, the score is less than one for one.

    > It was not possible from the code posted to _know_ that the OP was
    > actually recreating the BufferedInputStream anew each time. One could
    > certainly guess, and of course Ronny did (and did so correctly, as it
    > turns out).
    >
    > But without an SSCCE, we know that the code posted deviates from the
    > _actual_ code in some way, and it could just as easily have been that
    > part of the deviation included an oversimplification of how
    > BufferedInputStream was used.
    >
    > Other guesses were made as well (i.e. my own), based on the same
    > incomplete information, and they were wrong. The guess that
    > BufferedInputStream was being recreated each time could have been wrong
    > too, since the code posted was obviously not really the code the OP was
    > using.
    >
    > That guess turned out to be right, and I give all credit to Ronny for
    > taking the code at face value and making the guess he did. He succeeded
    > where at least some of the rest of us failed. But there was no
    > assurance at all that would have happened (*)
    >
    > An SSCCE isn't just a good idea; it's a practical requirement.


    Amen, brother!

    --
    Lew
     
    Lew, Oct 4, 2011
    #3
  4. On 10/4/2011 12:07 PM, Peter Duniho wrote:
    > It was not possible from the code posted to _know_ that the OP was
    > actually recreating the BufferedInputStream anew each time. One could
    > certainly guess, and of course Ronny did (and did so correctly, as it
    > turns out).
    >
    > But without an SSCCE, we know that the code posted deviates from the
    > _actual_ code in some way, and it could just as easily have been that
    > part of the deviation included an oversimplification of how
    > BufferedInputStream was used.
    >
    > Other guesses were made as well (i.e. my own), based on the same
    > incomplete information, and they were wrong. The guess that
    > BufferedInputStream was being recreated each time could have been wrong
    > too, since the code posted was obviously not really the code the OP was
    > using.


    Really?

    The code posted (excl. debug print was):

    final DataInputStream dis = new DataInputStream(new
    BufferedInputStream(is));
    final int size = dis.readInt();
    final byte[] bytes = new byte[size];
    dis.readFully(bytes);

    It seems rather obvious to me that a new BufferedInputStream
    would be created every time this code is called.

    Arne
     
    Arne Vajhøj, Nov 6, 2011
    #4
  5. On 11/6/2011 7:03 PM, Peter Duniho wrote:
    > On 11/6/11 3:23 PM, Arne Vajhøj wrote:
    >> [...]
    >> It seems rather obvious to me that a new BufferedInputStream
    >> would be created every time this code is called.

    >
    > Really? You resurrected a thread that's over a month old, just to tell
    > us what's obvious to you?


    It seemed relevant to point out that the guy that provided
    that solution was not guessing, but was a logical conclusion
    based on the code posted and that SSCCE requestors just
    BS'ed.

    Arne
     
    Arne Vajhøj, Nov 7, 2011
    #5
  6. On 11/6/2011 8:35 PM, Peter Duniho wrote:
    > On 11/6/11 4:46 PM, Arne Vajhøj wrote:
    >> On 11/6/2011 7:03 PM, Peter Duniho wrote:
    >>> On 11/6/11 3:23 PM, Arne Vajhøj wrote:
    >>>> [...]
    >>>> It seems rather obvious to me that a new BufferedInputStream
    >>>> would be created every time this code is called.
    >>>
    >>> Really? You resurrected a thread that's over a month old, just to tell
    >>> us what's obvious to you?

    >>
    >> It seemed relevant to point out that the guy that provided
    >> that solution was not guessing [...]

    >
    > Sure he was. It was a good guess. But still a guess.
    >
    > Since the code that was posted was useless on its own, it's obvious that
    > code was not the code the OP actually was using.
    >
    > Once we know the code posted isn't the code actually being used, it's
    > impossible to say for sure what the code actually being used really
    > looks like.
    >
    > Maybe it was, as the posted code did, creating a new object each time.
    > Maybe it wasn't.
    >
    > You can ASSume it was if you like, but some of us prefer to stick with
    > what we actually _know_.


    The posted code was incorrect and could give the described symptoms.

    Assuming under those circumstances that the incorrectness indeed cause
    the symptoms is more than guessing/assuming.

    Not pointing that out but wanting a complete program just in case
    there were a second incorrectness that coincidentally would cause
    the same symptoms is absurd.

    Arne
     
    Arne Vajhøj, Nov 7, 2011
    #6
  7. On 11/6/2011 9:30 PM, Peter Duniho wrote:
    > On 11/6/11 6:16 PM, Arne Vajhøj wrote:
    >> [...]
    >> Not pointing that out but wanting a complete program just in case
    >> there were a second incorrectness that coincidentally would cause
    >> the same symptoms is absurd.

    >
    > Actually, what's absurd is reviving a thread over a month later just say
    > what you _think_ is obvious.


    Are you telling me that you don't think it is obvious that

    final DataInputStream dis = new DataInputStream(new
    BufferedInputStream(is));
    final int size = dis.readInt();
    final byte[] bytes = new byte[size];
    dis.readFully(bytes);

    will create a new BufferedInputStream every time the code
    is executed??

    Arne
     
    Arne Vajhøj, Nov 7, 2011
    #7
    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. sjl
    Replies:
    5
    Views:
    2,903
    S. Justin Gengo
    Nov 8, 2005
  2. Replies:
    9
    Views:
    719
    Michael Wojcik
    Aug 23, 2005
  3. Replies:
    4
    Views:
    3,348
    Jan Faerber
    Sep 24, 2008
  4. iporter
    Replies:
    6
    Views:
    619
    Anthony Jones
    Mar 30, 2007
  5. Jeff Thies

    reading buffered form data

    Jeff Thies, Jun 30, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    144
    Greg Bacon
    Jun 30, 2003
Loading...

Share This Page