Results 1 to 9 of 9
- 06-10-2011, 07:22 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
Should this input stream be closed?
I have a method which returns a file as input stream as below.
public InputStream getInpStream(){
..
...
return Thread.currentThread().getContextClassLoader().get ResourceAsStream(fileName);
}
And in the invoking method I use this input stream to do a castor mapping as below. My question is should a instance of input stream be explicitly created to close this stream later?
Public Mapping returnMapping(){
Mapping mapping = new Mapping();
mapping.loadMapping(new InputSource(getInpStream()));
return mapping;
}
Thanks
- 06-10-2011, 08:51 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,390
- Blog Entries
- 7
- Rep Power
- 17
Strictly speaking you don't have to close the InputStream because the appropriate Streams close themselves in their finalize() method. It doesn't harm to close it because an open file (or a socket etc.) is a resource and if you don't need it, better get rid of it asap.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-10-2011, 05:22 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
Thanks Jos.
Looks I cannot close it in the finally block of my returnMapping() method as the mapping object retunred from thsi method still references the input stream and when the I access the mapping object down the lane, I get "Stream Already Closed"
Public Mapping returnMapping(){
Mapping mapping = new Mapping();
try{
InputStream inpStream = getInpStream();
mapping.loadMapping(new InputSource(inpStream);
}Catch(Exception e) {
}finally{
if(null != inpStream){
inpStream.close();
}
}
return mapping;
}
- 06-10-2011, 05:33 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,390
- Blog Entries
- 7
- Rep Power
- 17
- 06-10-2011, 06:15 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
my bad..Thats a typo in the process of editing the code manually to post it here..The inputstream is declared outside the try..
- 06-10-2011, 06:32 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,390
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-10-2011, 06:32 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,390
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-10-2011, 06:55 PM #8
Member
- Join Date
- Jun 2011
- Posts
- 4
- Rep Power
- 0
Yes Jos..The mapping object was accessed after I closed the input stream in the finally block and Thanks for the suggestion - barock
- 06-10-2011, 07:11 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,390
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Readline from an input stream and nothing more.
By couling in forum Advanced JavaReplies: 19Last Post: 05-27-2011, 09:20 PM -
Input Stream as an Object?
By sehudson in forum New To JavaReplies: 1Last Post: 03-12-2011, 12:37 AM -
java.sql.SQLException: System or internal error java.io.IOException: Stream closed
By ashok bhagat in forum Advanced JavaReplies: 0Last Post: 01-21-2011, 12:43 PM -
Input stream error
By Johnny68 in forum New To JavaReplies: 10Last Post: 08-05-2010, 06:20 PM -
Stream closed on a ClassLoader input
By RaistlinMajeren in forum Advanced JavaReplies: 15Last Post: 06-03-2010, 07:18 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks