0

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?
Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
ironwood
  • 8,936
  • 15
  • 65
  • 114
  • What part of the Javadoc for the respective classes didn't you understand? This is *all documented.* You shouldn't need to consult a forum or SO to resolve this sort of thing. – user207421 Oct 17 '12 at 23:28
  • Hmmm... I actually went through the DOCS. But I decided to ask iy in the forum to clear my self – ironwood Oct 18 '12 at 04:39

1 Answers1

1

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.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the Answer Jon. It is true that I can wrap the InputStream by DataInputStream if I am willing to use the methods in DataInputStream. But if so, I must declare it as a DataInputStream not as InputStream right? If the declaration type is InputStream, then is there any difference when the InputStream taken as DataInputStream and InputStream is taken as it is? – ironwood Oct 17 '12 at 13:33
  • I mean will there be any issue, if I taken InputStream mIstr3 = new DataInputStream(mUConn.getInputStream()); as InputStream mIstr3 = mUConn.getInputStream();? – ironwood Oct 17 '12 at 13:36
  • @NamalFernando: There may be some subtle differences, but apart from anything else it would be pointless and distracting. If you only need the methods in `InputStream`, why wrap it at all? Never do anything just for the sake of it. – Jon Skeet Oct 17 '12 at 13:38
  • yep Jon, totally agreed with you. Actually, I am in a middle of refactoring a code that was previously implemented. And I found this in between the code that's why I raised this question. Thank you very much. – ironwood Oct 17 '12 at 13:41