Skip to content

Commit

Permalink
Fix a resource leak in the LinuxEphemeralPortRangeDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
shs96c committed May 15, 2017
1 parent 5b8b62d commit ab77a9e
Showing 1 changed file with 8 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,24 @@

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"));
}
Expand All @@ -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) {
Expand Down

0 comments on commit ab77a9e

Please sign in to comment.