Are you sure mate? Because the code you just gave me:
Code:
boolean exit = false;
while (!exit) {
byte[] data = new byte[BUFFER_SIZE];
int amountOfData = in.read(data);
if (amountOfData < 0) {
exit = true;
} else {
processData(data, amountOfData);
}
}
That is blocking @ int amountOfData = in.read(data);
The reason is that:
1st loop there is data, ammounOfData > 0 therefore processData
2nd loop there is no data to read, so in.read(data) blocks just right there.
So there's no reason to do if(ammountOfData < 0) exit = true; because that condition is only true when the stream is closed therefore connection is closed.
You're getting my point mate ? That was my whole problem from the begining that i need to either now beforehand if there's data in the inputstream, which is impossible to do without using inputStream.available() which still is not a stable function.
Or either, use multithreading, and read data in one thread, and retrieve it from the other.
The thing that's worrying me is that the same code works on Sonim & Emulator but on Nokia 6120 classic isn't working.
Btw this is the new code that i'm talking about: (Maybe you'll notice something which i'm doing wrong)
Code:
public void readingData(boolean rd) throws Exception {
read = rd;
buffer = new byte[512]; // 512/1024 is a good size...
readTimer = new Thread() {
public void run() {
while (read) {
if (inputStream != null) {
// retrieving the data from Web server
while ((bufferUsed = inputStream.read(buffer)) > 0) {
writing = true; //i'm using this to make sure that the other thread doesn't read the baos while this thread is writing
baos.write(buffer, 0, bufferUsed); //TODO int offset should be always zero?
writing = false;
}
} else {
read = false;
}
}
}
};
readTimer.start();
}
Thanks a lot for your help mate, i do really appreciate it...