Android: SocketServer read returning -1 in middle of upload
I wrote a webserver for android. So yesterday i wanted to add upload capabilities to it.
byte[] receivedBuffer = new byte[BUFFER_SIZE];
BufferedInputStream bis = new BufferedInputStream(_client.getInputStream(), BUFFER_SIZE);
int bytesRead = 0;
while ((bytesRead = bis.read(receivedBuffer,0,BUFFER_SIZE)) != -1) {
//do stuff with b开发者_开发技巧ytes
if (bis.available() == 0) {
break;
}
}
// this point
my problem is when i upload files greater than say 400kb read is returning -1 in the middle of the file upload. And if i look at bis.available() at "this point" it is still showing >0
i tried to change
bytesRead = bis.read(receivedBuffer,0,BUFFER_SIZE)
to this
bytesRead = bis.read(receivedBuffer)
Another thing i tried was lowering the buffer_size and it worked for a little bit bigger files.Also i added thread.sleep just below if(bis.availabe()==0 and again it helped a little. Its almost like the bis is still loading data from the network stream when i call read and at that point there is nothing and then it returns -1.
I need help its been two days i am trying to get this to work.
Solved this one. I was prosessing the data so fast that the avaiable method sometimes returned 0 even if there was some data on its way. So i just adding a quick little while that checks if available is 0 and if it is sleep a while(10 ms) and if after a sec it still available ==0 then it breaks out of the while.
精彩评论