Fix a resource leak in the `LinuxEphemeralPortRangeDetector`
import java.io.BufferedReader;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
-public class LinuxEphemeralPortRangeDetector
- implements EphemeralPortRangeDetector {
+public class LinuxEphemeralPortRangeDetector implements EphemeralPortRangeDetector {
- final int firstEphemeralPort;
- final int lastEphemeralPort;
+ private final int firstEphemeralPort;
+ private final int lastEphemeralPort;
public static LinuxEphemeralPortRangeDetector getInstance() {
File file = new File("/proc/sys/net/ipv4/ip_local_port_range");
if (file.exists() && file.canRead()) {
- Reader inputFil = null;
- try {
- inputFil = new FileReader(file);
- } catch (FileNotFoundException e) {
+ try (Reader inputFil = new FileReader(file)) {
+ return new LinuxEphemeralPortRangeDetector(inputFil);
+ } catch (IOException e) {
throw new RuntimeException(e);
}
- return new LinuxEphemeralPortRangeDetector(inputFil);
}
return new LinuxEphemeralPortRangeDetector(new StringReader("49152 65535"));
}
@@ -49,11 +45,8 @@ public static LinuxEphemeralPortRangeDetector getInstance() {
FixedIANAPortRange defaultRange = new FixedIANAPortRange();
int lowPort = defaultRange.getLowestEphemeralPort();
int highPort = defaultRange.getHighestEphemeralPort();
- try {
- BufferedReader in = new BufferedReader(inputFil);
- final String s;
- s = in.readLine();
- final String[] split = s.split("\\s");
+ try (BufferedReader in = new BufferedReader(inputFil)) {
+ String[] split = in.readLine().split("\\s");
lowPort = Integer.parseInt(split[0]);
highPort = Integer.parseInt(split[1]);
} catch (IOException ignore) {
