-1

I need to know how to behave readInt() and available() function to read data stream

e.g:- [{"frame_id":"48","get_person":true,"type":"get_person","sensor_type":"mjpeg","data_size":41,"engine_id":"rtface"}{"type": "GET_PEOPLE_RESP", "people": []}]

Regards Janaka

HWJ
  • 87
  • 2
  • 9

2 Answers2

1

If you like to use JSON, this would work.

JsonElement e = new JsonParser().parse(new InputStreamReader(inputStream));
JSONObject obj = new JSONObject(e.getAsJsonObject().toString());

int dataSize = obj.getInt("data_size");
boolean getPerson = obj.getBoolean("get_person");
String sensorType = obj.getString("sensor_type");
jelic98
  • 723
  • 1
  • 12
  • 28
0

To read data, I have used the readInt() function, but it returns part of the incoming stream.

No it doesn't. It returns an integer.

To take rest of the part, I have used available().

Why? It doesn't 'take the rest' of anything, let alone 'part'. It 'returns an estimate of the number of bytes that can be read without blocking'. See the Javadoc.

It returns the rest of the part

No it doesn't.

and stops the thread

No it doesn't.

when it comes again into readInt() position inside looping in the sending thread.

Incomprehensible.

Full Incoming stream:-

{"frame_id":"48","get_person":true,"type":"get_person","sensor_type":"mjpeg","data_size":41,"engine_id":"rtface"}{"type": "GET_PEOPLE_RESP", "people": []}

readInt() return:

{"frame_id":"48","get_person":true,"type":"get_person","sensor_type":"mjpeg","data_size":41,"engine_id":"rtface"}

Nonsense. readInt() returns an integer, not a string.

available() return:

{"type": "GET_PEOPLE_RESP", "people": []}

Nonsense. available() returns an integer, not a string.

how to control this?

How to control what?

EDIT

Now that you've posted your code, you are making the fairly common mistake of assuming that TCP is a message-oriented protocol, which it isn't, and that available() can be used to delimit messages, which it can't. If you want messages, you have to delimit them yourself somehow: or else just treat the input as a byte-stream, which it is, and hand over to another parser, such as a JSON stream parser, and let it do the figuring.

user207421
  • 305,947
  • 44
  • 307
  • 483