Whenever I use HttpConnection Class in Java ME, Android or in BlackBerry, I uses DataInputStream/DataOutputStream class for reading & writing datas over remote server. However there are other class like InputStream/OutputStream which can be use for same purpose. I saw Question regarding InputStream/OutputStream class with HttpConnection. So I would like to know from experts that what are the differences between these two ?
Whenever I use HttpConnection Class in Java ME , Android or in BlackBerry ,
Share
DataInputStream/DataOutputStreamis anInputStream/Outputstream.InputStreamandOutputStreamare the most generic IO streams you can use and they are the base class of all streams in Java. You can read and write raw bytes only with them.DataInputStreamwrites formatted binary data. Instead of just simple unformatted bytes, you can readBytes,Integer,Double,Float,Short, UTF-8 strings, and any mixture of that data. And the same can be said forDataOutputStreamexcept that it writes these higher level data types.A
DataInputStream/DataOutputStreamhas a reference to anInputStream/OutputStreamwhich it reads the raw bytes and interprets those bytes as those previously mentioned data types.Although reading strings from the
DataInputStreamisn’t a good idea because it makes unchangeable assumptions about the character encoding of the underlyingInputStream. Instead, it’s better to use aReaderwhich will properly apply character encodings to the underlying byte stream to read data. That’s whyDataInputStream/DataOutputStreamis of limited use. Typically it’s better to trade textual data between processes because it’s easiest to make a server and client agree on how to parse the data. Trading binary has lots of bit twiddling that has to occur to make sure each process is talking the same language. It’s easy if you have two Java processes usingDataInputStream/DataOutputStream, but if you ever want to add a new client that isn’t Java you’ll have a harder time reusing it. Not impossible, but just harder.