Menu

#31 loadType(File) doesn't close the file handler

v1.0_(example)
open
nobody
None
5
2008-01-16
2008-01-16
Anonymous
No

The Yaml.loadType(File ) API doesn't seem to close the file inputstream. So if you have code such as:

File myFile = new File("someYamlFile");
HashSet<String> foo = (HashSet<String>)Yaml.loadType(myFile, HashSet.class);
myFile.delete();

You will not able to remove the yamlFile until the program exits.

Discussion

  • Ben Fortuna

    Ben Fortuna - 2008-05-04

    Logged In: YES
    user_id=14058
    Originator: NO

    I concur. Something like this seems to work in the meantime:

    YamlDecoder decoder = null;
    try {
    decoder = new YamlDecoder(new FileInputStream(getFile()));
    myInstance = (MyType) decoder.readObjectOfType(MyType.class);
    }
    catch (IOException ioe) {
    // handle exception..
    }
    finally {
    if (decoder != null) {
    decoder.close();
    }
    }

     
  • Jordan Angold

    Jordan Angold - 2010-11-23

    Better workaround:

    try {
    InputStream in = new BufferedInputStream ( new FileInputStream ( profileFile ) );
    MyType mt = Yaml.loadType ( in, MyType.class );
    in.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    The bug occurs in YamlConfig.loadType ( File, Class<T> ), which reads:

    return loadType(new YamlDecoder(new FileInputStream(file), this), clazz)

    but should look more like the code above.

     

Log in to post a comment.