reset issue in bufferedInputStream

Discussion in 'Java' started by Jimmy Zhang, Oct 15, 2003.

  1. Jimmy Zhang

    Jimmy Zhang Guest

    I have got a ByteArrayInputStream that I use to instantiate a
    BufferedInputStream. Then everything I finish reading the entire byte array,
    my code calls the reset method of the BbufferedInputStream, which causes an
    exception.
    After I change by code to reset teh underlying ByteArrayInputStream instead,
    the exception goes away.

    Why the BufferedInputStream doesn't recognize teh reset method and delegate
    to the underlying ByteArrayInputStream???
    Anyone help??
    Thanks,
    Jimmy
     
    Jimmy Zhang, Oct 15, 2003
    #1
    1. Advertising

  2. Jimmy Zhang

    Paul Lutus Guest

    Jimmy Zhang wrote:

    > I have got a ByteArrayInputStream that I use to instantiate a
    > BufferedInputStream. Then everything I finish reading the entire byte
    > array, my code calls the reset method of the BbufferedInputStream, which
    > causes an exception.
    > After I change by code to reset teh underlying ByteArrayInputStream
    > instead, the exception goes away.
    >
    > Why the BufferedInputStream doesn't recognize teh reset method and
    > delegate to the underlying ByteArrayInputStream???


    Must be your code. You know, the code you didn't post?

    Post the minimum number of compilable lines of code that shows the problem
    and raises the exception. You could post several million words of prose
    like "Why is this not working?" and we would be no closer to a solution.
    But post the code, and it's done very quickly.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 15, 2003
    #2
    1. Advertising

  3. Jimmy Zhang

    Jimmy Zhang Guest

    Here is the code

    ByteArrayInputStream bais = new ByteArrayInputStream(ba);
    BufferedInputStream bfis = new BufferedInputStream(bais);
    int a = 0;
    System.out.println("Something is wrong "+bfis.markSupported());

    for(int i=0;i<100000;i++)
    {
    while((a = bfis.read())>0)
    {
    //System.out.print((char)a);
    }
    bais.reset();
    }

    Notice that if I change bais to bfis, then an exception is thrown.



    "Paul Lutus" <> wrote in message
    news:...
    > Jimmy Zhang wrote:
    >
    > > I have got a ByteArrayInputStream that I use to instantiate a
    > > BufferedInputStream. Then everything I finish reading the entire byte
    > > array, my code calls the reset method of the BbufferedInputStream, which
    > > causes an exception.
    > > After I change by code to reset teh underlying ByteArrayInputStream
    > > instead, the exception goes away.
    > >
    > > Why the BufferedInputStream doesn't recognize teh reset method and
    > > delegate to the underlying ByteArrayInputStream???

    >
    > Must be your code. You know, the code you didn't post?
    >
    > Post the minimum number of compilable lines of code that shows the problem
    > and raises the exception. You could post several million words of prose
    > like "Why is this not working?" and we would be no closer to a solution.
    > But post the code, and it's done very quickly.
    >
    > --
    > Paul Lutus
    > http://www.arachnoid.com
    >
     
    Jimmy Zhang, Oct 15, 2003
    #3
  4. Jimmy Zhang

    ak Guest

    "Jimmy Zhang" <> schrieb im Newsbeitrag
    news:Ud6jb.774809$Ho3.206586@sccrnsc03...
    > Here is the code
    >
    > ByteArrayInputStream bais = new ByteArrayInputStream(ba);
    > BufferedInputStream bfis = new BufferedInputStream(bais);
    > int a = 0;
    > System.out.println("Something is wrong "+bfis.markSupported());
    >
    > for(int i=0;i<100000;i++)
    > {
    > while((a = bfis.read())>0)
    > {
    > //System.out.print((char)a);
    > }
    > bais.reset();
    > }
    >
    > Notice that if I change bais to bfis, then an exception is thrown.
    >


    before you call reset() you must set the mark!

    >
    >
    > "Paul Lutus" <> wrote in message
    > news:...
    > > Jimmy Zhang wrote:
    > >
    > > > I have got a ByteArrayInputStream that I use to instantiate a
    > > > BufferedInputStream. Then everything I finish reading the entire byte
    > > > array, my code calls the reset method of the BbufferedInputStream,

    which
    > > > causes an exception.
    > > > After I change by code to reset teh underlying ByteArrayInputStream
    > > > instead, the exception goes away.
    > > >
    > > > Why the BufferedInputStream doesn't recognize teh reset method and
    > > > delegate to the underlying ByteArrayInputStream???

    > >
    > > Must be your code. You know, the code you didn't post?
    > >
    > > Post the minimum number of compilable lines of code that shows the

    problem
    > > and raises the exception. You could post several million words of prose
    > > like "Why is this not working?" and we would be no closer to a solution.
    > > But post the code, and it's done very quickly.
    > >
    > > --
    > > Paul Lutus
    > > http://www.arachnoid.com
    > >

    >
    >
     
    ak, Oct 15, 2003
    #4
  5. Jimmy Zhang

    Paul Lutus Guest

    Jimmy Zhang wrote:

    > Here is the code
    >
    > ByteArrayInputStream bais = new ByteArrayInputStream(ba);
    > BufferedInputStream bfis = new BufferedInputStream(bais);
    > int a = 0;
    > System.out.println("Something is wrong "+bfis.markSupported());
    >
    > for(int i=0;i<100000;i++)
    > {
    > while((a = bfis.read())>0)
    > {
    > //System.out.print((char)a);
    > }
    > bais.reset();


    Can I ask why you are doing this? What is the purpose of (1) the program,
    and (2) this specific line? The description below tells me this prevents
    your program from doing anything worthwhile. And where are you setting a
    marked position?

    *************************************************

    reset

    public void reset()

    Resets the buffer to the marked position. The marked position is 0
    unless another position was marked or an offset was specified in the
    constructor.

    *************************************************


    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 15, 2003
    #5
  6. Jimmy Zhang

    soft-eng Guest

    "Jimmy Zhang" <> wrote in message news:<Ud6jb.774809$Ho3.206586@sccrnsc03>...
    > Here is the code
    >
    > ByteArrayInputStream bais = new ByteArrayInputStream(ba);
    > BufferedInputStream bfis = new BufferedInputStream(bais);
    > int a = 0;
    > System.out.println("Something is wrong "+bfis.markSupported());
    >
    > for(int i=0;i<100000;i++)
    > {
    > while((a = bfis.read())>0)
    > {
    > //System.out.print((char)a);
    > }
    > bais.reset();
    > }
    >
    > Notice that if I change bais to bfis, then an exception is thrown.


    BufferedInputStream has its own "reset", it does not delegate
    to the underlying stream (it can't, the underlying stream
    is of type InputStream and may not have markSupported.) It
    holds a certain amount of data in a buffer, and will reset if
    the data is still in buffer. If it has emptied its original
    buffer and you ask it to reset back to somewhere there -- it
    can't, and will therefore throw an exception. In this
    case, you have a large enough read (100000) that with the
    default buffer size, it's original buffer has been emptied many times.
    But your mark is still sitting in the very first
    buffer, that BufferedInputStream no longer has.

    The ByteArrayInputStream, on the other hand, has the
    full array available, so can easily reset to zero.
     
    soft-eng, Oct 15, 2003
    #6
  7. Jimmy Zhang

    Jimmy Zhang Guest

    Ok, first of all, mark, if unset, is default to 0 automatically.
    Second, as decribed in the Java documentation,
    it says something like "look under the contract of the underlying
    InputStream," my understanding is that it does some kind of delegation.

    Am I wrong??


    "ak" <> wrote in message news:bmiug2$8rc$...
    >
    > "Jimmy Zhang" <> schrieb im Newsbeitrag
    > news:Ud6jb.774809$Ho3.206586@sccrnsc03...
    > > Here is the code
    > >
    > > ByteArrayInputStream bais = new ByteArrayInputStream(ba);
    > > BufferedInputStream bfis = new BufferedInputStream(bais);
    > > int a = 0;
    > > System.out.println("Something is wrong "+bfis.markSupported());
    > >
    > > for(int i=0;i<100000;i++)
    > > {
    > > while((a = bfis.read())>0)
    > > {
    > > //System.out.print((char)a);
    > > }
    > > bais.reset();
    > > }
    > >
    > > Notice that if I change bais to bfis, then an exception is thrown.
    > >

    >
    > before you call reset() you must set the mark!
    >
    > >
    > >
    > > "Paul Lutus" <> wrote in message
    > > news:...
    > > > Jimmy Zhang wrote:
    > > >
    > > > > I have got a ByteArrayInputStream that I use to instantiate a
    > > > > BufferedInputStream. Then everything I finish reading the entire

    byte
    > > > > array, my code calls the reset method of the BbufferedInputStream,

    > which
    > > > > causes an exception.
    > > > > After I change by code to reset teh underlying ByteArrayInputStream
    > > > > instead, the exception goes away.
    > > > >
    > > > > Why the BufferedInputStream doesn't recognize teh reset method and
    > > > > delegate to the underlying ByteArrayInputStream???
    > > >
    > > > Must be your code. You know, the code you didn't post?
    > > >
    > > > Post the minimum number of compilable lines of code that shows the

    > problem
    > > > and raises the exception. You could post several million words of

    prose
    > > > like "Why is this not working?" and we would be no closer to a

    solution.
    > > > But post the code, and it's done very quickly.
    > > >
    > > > --
    > > > Paul Lutus
    > > > http://www.arachnoid.com
    > > >

    > >
    > >

    >
    >
     
    Jimmy Zhang, Oct 15, 2003
    #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. Krick
    Replies:
    2
    Views:
    14,326
    Marco Schmidt
    Aug 28, 2003
  2. Jimmy Zhang
    Replies:
    1
    Views:
    691
    Roedy Green
    Oct 31, 2003
  3. Marc Twain
    Replies:
    5
    Views:
    4,867
    Andrew Thompson
    Jan 15, 2004
  4. Daniel

    BufferedInputstream problem

    Daniel, Mar 9, 2005, in forum: Java
    Replies:
    7
    Views:
    1,094
    Rogan Dawes
    Mar 10, 2005
  5. Andersen
    Replies:
    8
    Views:
    30,135
    Roedy Green
    Oct 6, 2005
Loading...

Share This Page