Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determine Data Type From Stream - DataInputStream/DataOutputStream

 
James Gordon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm just wondering, if a socket server is writing a mixture of int, short, text, etc
(DataOutputStream: writeInt, writeShort, writeBytes)
to a client, is there anyway for the client to determine the data type
so that the message can be constructed back properly.

Sample code:


ie: From the client perspective, are we able to determine that
the 1st piece of data is short, 2nd a string and 3rd is an integer?

Of course, an alternative is to have a fixed format.


Thanks.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is nothing in a DataInputStream to indicate the type the next bit of data but it is easy enough to create your own protocol. For example you could use a prefix byte with say 0 being an int, 1 being a double, 2 being a utf8 string and 0xff indicating the end of data. The problem I have with this is that one has no context. Just knowing the next item is an int does not tell you what it represents. I prefer to send complete fixed format records with a prefix byte to indicate what type of record it is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic