There is a server that sends a number of bytes on command. The amount of data is known in advance - 280 KB. Data is transmitted via a Wi-Fi network over a TCP / IP connection. It uses its own protocol, which does not display the number of transmitted information. The whole “server” feature of the server is that it opens the stream (stream) and transmits the information with frames of different sizes. The size of each frame can vary from 10 to 2048 bytes. It will transmit frames until it transfers the entire amount of data (280 KB). In other words, when receiving data on an Android device, I can’t determine the amount of data received in the stream, so I cannot place all the data in the correct form.
Question: How to properly implement data reception with such "features"?
Possible solution: I decided to try to create an array of 2048 in size (maximum possible frame size) bytes and write each new frame to this array:
DataInputStream din = new DataInputStream(socket.getInputStream()); byte[] buffer = new byte[2048]; din.read(buffer); Then I tried to put the data from my buffer array into the ArrayList collection, but another problem arose: how to determine where the necessary data ended, which I saved in the buffer array?
DataInputStream.readFully. In general, this is not a "server feature", but tcp works this way. - zRrrreadmethod still returns something. Thank! But do not tell me, how is it right in my case to deal with array processing? Each new received byte [] array leads to the form Byte [] and put into the collection or immediately create a huge array (namelybyte[] array = new byte[1024 * 280]) and put the data there? - ivanovd422array), and then write read bytes into it in blocks. Linking lists here is a waste of memory and unnecessary inconvenience (after all, it is necessary to store its actual size for each read "piece"). So I am fornew byte[1024 * 280]andSystem.arraycopy(buffer, 0, array, arrayPointer, readCount). - Regent