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