aspose file tools*
The moose likes Beginning Java and the fly likes Returning Steams instead of Byte Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Returning Steams instead of Byte Arrays" Watch "Returning Steams instead of Byte Arrays" New topic
Author

Returning Steams instead of Byte Arrays

Raistlen Majere
Greenhorn

Joined: Aug 03, 2009
Posts: 13
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Raistlen Majere
Greenhorn

Joined: Aug 03, 2009
Posts: 13
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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

Joined: Aug 03, 2009
Posts: 13
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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Returning Steams instead of Byte Arrays
 
Similar Threads
How to correctly use this read() write()
Error while read zip file
fileREader and FileWriter
Java endian and bit order and signed and unsigned and python and more confusion than I can deal with
Please help with response.getoutputStream