This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes I/O and Streams and the fly likes IO Streams as parameters. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "IO Streams as parameters." Watch "IO Streams as parameters." New topic
Author

IO Streams as parameters.

Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
I wonder if it's a bad practice to use IO Streams as parameters, and how to walk around this.

Because it will raise a problem: where to close the stream? after the invoking or when the invoked method finished?


To be or not to be. It's a question.
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1

Hi,

I am not able to think of any performance related constraints regarding passing streams as parameters. IMHO, it is the responsibility of the block of code taht opened the resource connection/stream to take care of closing it, handling all exceptions thrown in the appropriate fashion during any operations performed on the same.

Thanks,
Raj.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

Raj Kamal wrote:IMHO, it is the responsibility of the block of code taht opened the resource connection/stream to take care of closing it

I couldn't agree more. The called method that takes an InputStream doesn't know if it is still needed afterwards; perhaps it has a mark that allows the calling method to go back in the InputStream and do some more reading. If the called method closes it then the calling method will have a problem.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: IO Streams as parameters.