DataInputStream , GZIPInputStream and InputStream differences

973 views Asked by At

I know that,

GZIPInputStream extends InflaterInputStream extends FilterInputStream extends InputStream and

DataInputStream extends FilterInputStream extends InputStream 

I want to know the difference between following..

InputStream mIstr1 = new DataInputStream(new GZIPInputStream(mUConn.getInputStream()));
InputStream mIstr2 = new GZIPInputStream(mUConn.getInputStream());
InputStream mIstr3 = new DataInputStream(mUConn.getInputStream());
InputStream mIstr4 = mUConn.getInputStream();

mUConn is a HttpURLConnection.

my concerns are

  • what are the occasions that we must get the InputStream as GZIPInputStream in DataInputStream?
  • what are the occasions that we must get the InputStream as GZIPInputStream?
  • what are the occasions that we must get the InputStream as DataInputStream?
1

There are 1 answers

4
Jon Skeet On BEST ANSWER

They really are entirely different classes, and the documentation should give you a good description of what the purpose of each class is.

Do you want to use the methods in DataInputStream which involve reading primitive types and strings from a stream in a well-documented fashion? If so, use DataInputStream to wrap the original stream; otherwise don't.

Does your original stream contain data compressed with the gzip algorithm? If so, use GZIPInputStream; otherwise don't. I wouldn't expect an input stream from an HttpURLConnection to present gzip-compressed data; I'd hope that the HTTP libraries would perform an appropriate inflation based on headers, but it's possible that they're being "dumb" here. (If that's the case, you may well wish to find alternative libraries.)