• 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.
 
my overalls have superpowers - they repel people who think fashion is important. Tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!