Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to clear the data of a DataInputStream in Java? (an Android application)

Tags:

java

android

Updated:

The project related to this question has changed a lot since this post was shared, actually I highly recommend to avoid using TCP sockets for transmitting images since it makes the transmission slower. I fixed the problem transmitting the images via UDP and compressing them from YUV to JPG instead of using RGB. This makes easier the job of transmitting and reconstructing the image since you don't have to handle each layer of the image.

To know the size of the image I just simply add this info at the first bytes of the UDP packet so if the size is not received properly, the time out of the socket will discard that packet and the communication still working when receiving the next UDP packet.

Since this is not the answer for the original question, I will leave it as unanswered so others with the same problem may find a solution and post it here.

------------------------ Original Post: ---------------------------

First of all thanks for helping and apologize for my English, I'm from Spain.

I'm receiving images from a computer via TCP using a socket. For every image I get the number of layers (RGB), rows and columns of every layer, then the values of each pixel. To collect the data I use a DataInputStream.

Usually it works fine, but sometimes I get wrong values of the number of rows and columns causing problems in the next functions i.e. the app freezes waiting for data that will never be sent, or simply causes an index error on the array which holds the data.

So I want to clear the data if the number of rows and columns is too high, but I can't find any way to clear the DataInputStream, full of wrong bytes of pixel values.

In addition, I dont't know the size of the DataInputStream, because the size is calculated the following way: Layers * Width * height. So if I don't get the real values of this camps, I can't know the number of bytes that I have to skip.

Questions:

  1. Anyone knows some way to just clear a DataInputStream?

  2. If not, should I use another type of Input way like BufferedInputStream?

like image 575
user1407368 Avatar asked Dec 05 '25 10:12

user1407368


1 Answers

I suspect that your problem isn't really about clearing the input stream but more about finding a back after getting out of sync. A common way to do this is to use a boundary-pattern (used in multipart html docs and in mjpeg streams). So if you get lost in the stream you continue to search for this pattern till you find it.

If you really want to skip bytes, you can use DataInputStream.skipBytes(int n) method.

like image 122
dacwe Avatar answered Dec 07 '25 22:12

dacwe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!