HBASE-15891. Closeable resources potentially not getting closed if exception is thrown.

Signed-off-by: stack <stack@apache.org>
This commit is contained in:
Sean Mackrory 2016-05-25 07:20:14 -06:00 committed by stack
parent 3b6e6e6c25
commit f4470af95d
4 changed files with 31 additions and 28 deletions

View File

@ -1926,27 +1926,27 @@ public class ZKUtil {
int port = sp.length > 1 ? Integer.parseInt(sp[1])
: HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT;
Socket socket = new Socket();
InetSocketAddress sockAddr = new InetSocketAddress(host, port);
socket.connect(sockAddr, timeout);
try (Socket socket = new Socket()) {
socket.connect(sockAddr, timeout);
socket.setSoTimeout(timeout);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out.println("stat");
out.flush();
ArrayList<String> res = new ArrayList<String>();
while (true) {
String line = in.readLine();
if (line != null) {
res.add(line);
} else {
break;
socket.setSoTimeout(timeout);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out.println("stat");
out.flush();
ArrayList<String> res = new ArrayList<String>();
while (true) {
String line = in.readLine();
if (line != null) {
res.add(line);
} else {
break;
}
}
return res.toArray(new String[res.size()]);
}
socket.close();
return res.toArray(new String[res.size()]);
}
private static void logRetrievedMsg(final ZooKeeperWatcher zkw,

View File

@ -177,8 +177,10 @@ public class CoprocessorClassLoader extends ClassLoaderBase {
if (m.matches()) {
File file = new File(parentDirStr, "." + pathPrefix + "."
+ path.getName() + "." + System.currentTimeMillis() + "." + m.group(1));
IOUtils.copyBytes(jarFile.getInputStream(entry),
new FileOutputStream(file), conf, true);
try (FileOutputStream outStream = new FileOutputStream(file)) {
IOUtils.copyBytes(jarFile.getInputStream(entry),
outStream, conf, true);
}
file.deleteOnExit();
addURL(file.toURI().toURL());
}

View File

@ -72,14 +72,14 @@ public class LogLevel {
System.out.println("Connecting to " + url);
URLConnection connection = url.openConnection();
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
for(String line; (line = in.readLine()) != null; )
if (line.startsWith(MARKER)) {
System.out.println(TAG.matcher(line).replaceAll(""));
try (BufferedReader in = new BufferedReader(new InputStreamReader(
connection.getInputStream()))) {
for(String line; (line = in.readLine()) != null; ) {
if (line.startsWith(MARKER)) {
System.out.println(TAG.matcher(line).replaceAll(""));
}
}
in.close();
}
} catch (IOException ioe) {
System.err.println("" + ioe);
}

View File

@ -124,8 +124,9 @@ public class JarFinder {
jarDir));
}
}
JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile));
jarDir(dir, "", zos);
try (JarOutputStream zos = new JarOutputStream(new FileOutputStream(jarFile))) {
jarDir(dir, "", zos);
}
}
/**