• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning Steams instead of Byte Arrays

 
Raistlen Majere
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently the application reads byte by byte, a network connection stream. This stream can contain quite a bit of information. The stream is stored in a temp byte array and then returned to the caller.

The bossman wants me to change the code from returning a byte array to returning a "stream" as how he puts it. His underlying premise is that he does not want a limit placed on the byte array which there currently is.

Now assuming the bossman is always right and gets what he wants regardless of common sense, how exactly would I do this?

My understanding is you must read a "connection" byte by byte anyway, so everything I read shows as each line/byte is read, you store it in some kind of object. Is it actually possible to return one giant "Stream" that contains the read data in a stream type, or is this only possible through some kind of array?

Here is the current code:
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about simply returning connection.getInputStream()? That's an InputStream, so it would give your boss what he wants.

If the byte array size is the only problem, you could use a ByteArrayOutputStream:
Still, this will copy the entire input to memory, and can therefore be quite inefficient. However, if you need the contents of the stream more than once this is the best option.
 
Raistlen Majere
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Boss does not want us to use arrays of any type for these kind of streams. He wants the return type to be the buffered stream. Unfortunately I am converting over from VB to Java so I do not have the know how of why or why not, just that I must do.

So knowing I can not use byte arrays, would the class definition be changed to something like:
or is there such a type as stream. I am just a little confused on how this would be returned.

Also, you suggested just returning connection.getInputStream() which sounds exactly like what is needed, but does that return the entire data or do I still need to step through each byte or line first?
i.e.
Thanks again for the help, and sorry to be so noobish but this is literally my first task in Java.
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the boss says you should return a stream, then a stream it is. The code I showed you can be discarded then.




Raistlen Majere wrote:The Boss does not want us to use arrays of any type for these kind of streams. He wants the return type to be the buffered stream.

Then bis (from BufferedInputStream bis = new BufferedInputStream(iStream);) is just what he's asking for.

Also, you suggested just returning connection.getInputStream() which sounds exactly like what is needed, but does that return the entire data or do I still need to step through each byte or line first?
i.e.

Eventually, you want the data from the stream, so the reading code (like the code you already had) should still be used somewhere along the line.
 
Raistlen Majere
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much for your help. Last question I hope. How would the Class Declaration now look? What type would it need to return?


or something like that?
 
Rob Spoor
Sheriff
Pie
Posts: 20493
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since BufferedInputStream provides no extra methods, you could turn the return type into InputStream:
BufferedInputStream is a subclass of InputStream so you can still return the BufferedInputStream instance, but this way it's easier to change it in the future if necessary.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic