my program be blocked by inputstream ><~~

B

Boki

====================
while ( !done )
{
gui.repaint()
int count_jpg=0;
int bytesToRead = in.available();

if (bytesToRead > 0)
{
nbrOfBytesRead = in.read(byteBuffer);
if (nbrOfBytesRead!=-1)
{
for (count_jpg=0;count_jpg<nbrOfBytesRead;count_jpg++)
{
imageData[GUI.ggg+count_jpg]=byteBuffer[count_jpg];
}
GUI.ggg+=nbrOfBytesRead;
}
else
done=true;
}
}

=================
It seems that the program can't exit the while loop even my file already
send completely.

I see the nbrOfBytesRead is 22 ( not -1 ), and the screen didn't be refresh
again.

It seems that the program be blocked, am I right?

Is that a K750i's bug or my wrong implementation?

Thank you very much for your help/support.

Best regards,
Boki.
 
R

Rogan Dawes

Boki said:
====================
while ( !done )
{
gui.repaint()
int count_jpg=0;
int bytesToRead = in.available();

if (bytesToRead > 0)
{
nbrOfBytesRead = in.read(byteBuffer);
if (nbrOfBytesRead!=-1)
{
for (count_jpg=0;count_jpg<nbrOfBytesRead;count_jpg++)
{
imageData[GUI.ggg+count_jpg]=byteBuffer[count_jpg];
}
GUI.ggg+=nbrOfBytesRead;
}
else
done=true;
}
}

=================
It seems that the program can't exit the while loop even my file already
send completely.

I see the nbrOfBytesRead is 22 ( not -1 ), and the screen didn't be refresh
again.

It seems that the program be blocked, am I right?

Is that a K750i's bug or my wrong implementation?

Thank you very much for your help/support.

Best regards,
Boki.

int got;
int buff=new byte[1024];
while ((got=in.read(buff))>0) {
// do whatever you want with the bytes [0..got-1]
}

is the standard paradigm for reading from an inputstream.

The reason your program in not exiting is that once it has read all the
bytes in the stream, in.available() returns 0, which means that it will
never get to in.read() returning -1, and your logic will never set done=true

Polling in.available() is pretty much useless in this case. Don't do it ;-)

You might also want to look at System.arrayCopy()

Rogan
 

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

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,851
Latest member
CristineKo

Latest Threads

Top