Properly close streams.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@2723 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2010-02-09 20:57:07 +00:00
parent 033c67d81b
commit 7829ca78dc
3 changed files with 42 additions and 19 deletions
+2 -2
View File
@@ -427,8 +427,8 @@ public class ConfigUtil
throws IOException
{
InputStream input = sourceURL.openStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(input));
try {
BufferedReader bin = new BufferedReader(new InputStreamReader(input));
PropRecord record = new PropRecord(path, sourceURL);
boolean started = false;
String line;
@@ -448,7 +448,7 @@ public class ConfigUtil
}
return record;
} finally {
StreamUtil.close(input);
StreamUtil.close(bin);
}
}
@@ -20,6 +20,7 @@
package com.samskivert.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -27,6 +28,8 @@ import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import com.samskivert.io.StreamUtil;
/**
* Utility functions related to properties objects.
*/
@@ -175,33 +178,45 @@ public class PropertiesUtil
}
/**
* Loads up the supplied properties file and returns the specified
* key. Clearly this is an expensive operation and you should load a
* properties file separately if you plan to retrieve multiple keys
* from it. This method, however, is convenient for, say, extracting a
* value from a properties file that contains only one key, like a
* build timestamp properties file, for example.
* Reads and parses the supplied file into a {@link Properties} instance.
*/
public static Properties load (File from)
throws IOException
{
Properties props = new Properties();
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(from));
try {
props.load(bin);
return props;
} finally {
StreamUtil.close(bin);
}
}
/**
* Loads up the supplied properties file and returns the specified key. Clearly this is an
* expensive operation and you should load a properties file separately if you plan to retrieve
* multiple keys from it. This method, however, is convenient for, say, extracting a value from
* a properties file that contains only one key, like a build timestamp properties file, for
* example.
*
* @return the value of the key in question or null if no such key
* exists or an error occurred loading the properties file.
* @return the value of the key in question or null if no such key exists or an error occurred
* loading the properties file.
*/
public static String loadAndGet (File propFile, String key)
{
try {
Properties props = new Properties();
props.load(new FileInputStream(propFile));
return props.getProperty(key);
return load(propFile).getProperty(key);
} catch (IOException ioe) {
return null;
}
}
/**
* Like {@link #loadAndGet(File,String)} but obtains the properties
* data via the classloader.
* Like {@link #loadAndGet(File,String)} but obtains the properties data via the classloader.
*
* @return the value of the key in question or null if no such key
* exists or an error occurred loading the properties file.
* @return the value of the key in question or null if no such key exists or an error occurred
* loading the properties file.
*/
public static String loadAndGet (String loaderPath, String key)
{
+10 -2
View File
@@ -31,6 +31,8 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import com.samskivert.io.StreamUtil;
/**
* Provides access to the very platform specific concept of the terminal
* (ie. vt100, xterm, etc.) in which our code is running. Bear in mind
@@ -100,10 +102,11 @@ public class TermUtil
*/
protected static Dimension getSizeViaResize ()
{
BufferedReader bin = null;
try {
Process proc = Runtime.getRuntime().exec("resize");
InputStream in = proc.getInputStream();
BufferedReader bin = new BufferedReader(new InputStreamReader(in));
bin = new BufferedReader(new InputStreamReader(in));
Pattern regex = Pattern.compile("([0-9]+)");
String line;
int columns = -1, lines = -1;
@@ -124,12 +127,17 @@ public class TermUtil
if (columns != -1 && lines != -1) {
return new Dimension(columns, lines);
}
return null;
} catch (PatternSyntaxException pse) {
return null; // logging a warning here may be annoying
} catch (SecurityException se) {
return null; // logging a warning here may be annoying
} catch (IOException ioe) {
return null; // logging a warning here may be annoying
} finally {
StreamUtil.close(bin);
}
return null;
}
/** Converts the string to an integer, returning -1 on any error. */