@Test public void testReadNoFileLeak() throws Exception { TrackingFileManager tfm = new TrackingFileManager(); for ( int i = 0; i < 1000; i++ ) { File propFile = TestFileUtils.createTempFile( "#COMMENT\nkey1=value1\nkey2 : value2" ); assertNotNull( tfm.read( propFile ) ); assertTrue( "Leaked file: " + propFile, propFile.delete() ); } }
public Properties read( File file ) { synchronized ( getLock( file ) ) { FileLock lock = null; FileInputStream stream = null; try { if ( !file.exists() ) { return null; } stream = new FileInputStream( file ); lock = lock( stream.getChannel(), Math.max( 1, file.length() ), true ); Properties props = new Properties(); props.load( stream ); return props; } catch ( IOException e ) { logger.warn( "Failed to read resolution tracking file " + file, e ); } finally { release( lock, file ); close( stream, file ); } } return null; }
TrackingFileManager { public Properties read( File file ) { synchronized ( getLock( file ) ) { FileLock lock = null; FileInputStream stream = null; try { if ( !file.exists() ) { return null; } stream = new FileInputStream( file ); lock = lock( stream.getChannel(), Math.max( 1, file.length() ), true ); Properties props = new Properties(); props.load( stream ); return props; } catch ( IOException e ) { logger.warn( "Failed to read resolution tracking file " + file, e ); } finally { release( lock, file ); close( stream, file ); } } return null; } }
TrackingFileManager { public Properties read( File file ) { synchronized ( getLock( file ) ) { FileLock lock = null; FileInputStream stream = null; try { if ( !file.exists() ) { return null; } stream = new FileInputStream( file ); lock = lock( stream.getChannel(), Math.max( 1, file.length() ), true ); Properties props = new Properties(); props.load( stream ); return props; } catch ( IOException e ) { logger.warn( "Failed to read resolution tracking file " + file, e ); } finally { release( lock, file ); close( stream, file ); } } return null; }  }
TrackingFileManager { public Properties read( File file ) { synchronized ( getLock( file ) ) { FileLock lock = null; FileInputStream stream = null; try { if ( !file.exists() ) { return null; } stream = new FileInputStream( file ); lock = lock( stream.getChannel(), Math.max( 1, file.length() ), true ); Properties props = new Properties(); props.load( stream ); return props; } catch ( IOException e ) { logger.warn( "Failed to read resolution tracking file " + file, e ); } finally { release( lock, file ); close( stream, file ); } } return null; }  TrackingFileManager setLogger( Logger logger ); Properties read( File file ); Properties update( File file, Map<String, String> updates ); }
TrackingFileManager { public Properties read( File file ) { synchronized ( getLock( file ) ) { FileLock lock = null; FileInputStream stream = null; try { if ( !file.exists() ) { return null; } stream = new FileInputStream( file ); lock = lock( stream.getChannel(), Math.max( 1, file.length() ), true ); Properties props = new Properties(); props.load( stream ); return props; } catch ( IOException e ) { logger.warn( "Failed to read resolution tracking file " + file, e ); } finally { release( lock, file ); close( stream, file ); } } return null; }  TrackingFileManager setLogger( Logger logger ); Properties read( File file ); Properties update( File file, Map<String, String> updates );  }