Java BufferedInputStream.read() fails

102 Views Asked by At

I am working on an android app that has continuous communication between the phone and a local server on the computer through cable and Android Open Accessory connection(AOA), the server(the computer) keeps sending packets of bytes to the android, then android parse them, all kinds of data gets received successfully.

In the implementation I am using BufferedInputStream like this:

val bufferedInputStream = BufferedInputStream(inputStream, 8192) // 8192 is the def buffer size

As I said everything works fine UNTIL android received a packet that is larger than 8192 which is the buffer size, in this case I need to loop until I read the whole packet is read(I know the size of the packets because I send that in the beginning of the packet)

When this kind of packet gets received, the call

bufferedInputStream.read(anyByteArrayOfTheWriteSize)

fails with message "Invalid argument", the annoying thing is that I can read the beginning of the packet which has the size and parse it successfully, so for example it tells android that the size of this packet is for ex 10192 bytes, this means that it needs to read 8192 then will do another iteration and read the remaining 200 bytes.

but it does not, it just fails once it reaches the call of the method read.

I was trying to use inputStream instead of bufferedInputStream but it did not work at all, bufferedInputStream kinda worked like magic, not sure why?! but this is not the topic of the question, just adding this info if incase it is needed.

0

There are 0 best solutions below