reset issue in bufferedInputStream

J

Jimmy Zhang

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
 
P

Paul Lutus

Jimmy said:
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.
 
J

Jimmy Zhang

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.
 
A

ak

Jimmy Zhang said:
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!
 
P

Paul Lutus

Jimmy said:
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.

*************************************************
 
S

soft-eng

Jimmy Zhang said:
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.
 
J

Jimmy Zhang

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 said:
Jimmy Zhang said:
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!
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top