Results 1 to 9 of 9
  1. #1
    barock is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    barock is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    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;
    }

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by barock View Post
    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;
    }
    That code doesn't compile because variable inpStream is out of scope in your finally block ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    barock is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    my bad..Thats a typo in the process of editing the code manually to post it here..The inputstream is declared outside the try..

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by barock View Post
    my bad..Thats a typo in the process of editing the code manually to post it here..The inputstream is declared outside the try..
    Ok, assuming the code is correct, where does your mapping object try to use that InputStream (after you have closed it)? (btw. it is always fine not to close it yourself for reasons I mentioned in my first reply).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by barock View Post
    my bad..Thats a typo in the process of editing the code manually to post it here..The inputstream is declared outside the try..
    Ok, assuming the code is correct, where does your mapping object try to use that InputStream (after you have closed it)? (btw. it is always fine not to close it yourself for reasons I mentioned in my first reply).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    barock is offline Member
    Join Date
    Jun 2011
    Posts
    4
    Rep Power
    0

    Default

    Yes Jos..The mapping object was accessed after I closed the input stream in the finally block and Thanks for the suggestion - barock

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by barock View Post
    Yes Jos..The mapping object was accessed after I closed the input stream in the finally block and Thanks for the suggestion - barock
    What is a Mapping object? I don't know what it is but it seems strange to me that, after loading, it wants to read from the InputStream again ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Readline from an input stream and nothing more.
    By couling in forum Advanced Java
    Replies: 19
    Last Post: 05-27-2011, 09:20 PM
  2. Input Stream as an Object?
    By sehudson in forum New To Java
    Replies: 1
    Last Post: 03-12-2011, 12:37 AM
  3. Replies: 0
    Last Post: 01-21-2011, 12:43 PM
  4. Input stream error
    By Johnny68 in forum New To Java
    Replies: 10
    Last Post: 08-05-2010, 06:20 PM
  5. Stream closed on a ClassLoader input
    By RaistlinMajeren in forum Advanced Java
    Replies: 15
    Last Post: 06-03-2010, 07:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •