java - Reading 4 bytes from InputStreamReader -
i'm having issue. i'm working on simple multithread tcp server in java, , have problem:
client can send me input string, such as
[identifier] text\r\n
which okay , have done. every input query needs ended \r\n, except 1 case.
this case when client send image binary data. message has following structure:
photo[space][length of data][space][data][4 bytes of checksum in big endian]
and whole point either return a
202 okay\r\n
message when checksum matches sum of bits in data, or ...
302 bad checksum\r\n
... otherwise. however, i'm having serious issues reading 4 bytes. input source instance of inputstreamreader, acquired from:
in = new inputstreamreader(this.clientsocket.getinputstream());
and function inputstreamreader.read() return int of char has been read. now, tried to
correctchecksumbytes = new byte[4]; for( int = 0 ; < 4 ; ++i ) { correctchecksumbytes[i] = (byte) in.read(); } if (correctchecksumbytes == null) { throw new badsyntax(); } bb = bytebuffer.wrap(correctchecksumbytes); bb.order(byteorder.big_endian); fourbytecheck = bb.getint();
however, understand casting int returned in.read() wrong, since result off hige amount. lead believe usage of bytebuffer correct, dont know how fill byte array correct values.
i tried convert inputsreamreader byte array, found out it's not valid way go, since message...
photo[space][length of data][space][data][4 bytes of checksum in big endian][identifier] text\r\n
...is valid , needs interpreted 2 messages.
i did tests , found out successfuly managed read data , compute valid checksum, next 4 bytes in input 4 bytes of checksum, did not manage read yet.
also, here's example of valid photo message
photo 8 abcdefgh\x00\x00\x02\x24
so i'd ask, how can read these 4 bytes , convert them int compare computed checksum?
thanks lot
inputstreamreader read character based data. have write custom inputstream , read x bytes out of it.
Comments
Post a Comment