// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

package org.openqa.selenium.net;

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 {

  final int firstEphemeralPort;
  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) {
        throw new RuntimeException(e);
      }
      return new LinuxEphemeralPortRangeDetector(inputFil);
    }
    return new LinuxEphemeralPortRangeDetector(new StringReader("49152 65535"));
  }

  LinuxEphemeralPortRangeDetector(Reader inputFil) {
    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");
      lowPort = Integer.parseInt(split[0]);
      highPort = Integer.parseInt(split[1]);
    } catch (IOException ignore) {
    }
    firstEphemeralPort = lowPort;
    lastEphemeralPort = highPort;
  }

  public int getLowestEphemeralPort() {
    return firstEphemeralPort;
  }

  public int getHighestEphemeralPort() {
    return lastEphemeralPort;
  }
}

@@ -887,46 +887,8 @@
 le;%0A
-import java.io.FileNotFoundException;%0A
 impo
@@ -1035,24 +1035,16 @@
 Detector
-%0A       
  impleme
@@ -1079,16 +1079,24 @@
 or %7B%0A%0A  
+private 
 final in
@@ -1119,16 +1119,24 @@
 Port;%0A  
+private 
 final in
@@ -1338,16 +1338,21 @@
 %7B%0A      
+try (
 Reader i
@@ -1366,63 +1366,90 @@
  = n
-ull;%0A      try %7B%0A        inputFil = new FileReader(f
+ew FileReader(file)) %7B%0A        return new LinuxEphemeralPortRangeDetector(inputF
 il
-e
 );%0A 
@@ -1466,20 +1466,10 @@
 ch (
-FileNotFound
+IO
 Exce
@@ -1530,68 +1530,8 @@
   %7D%0A
-      return new LinuxEphemeralPortRangeDetector(inputFil);%0A
     
@@ -1859,24 +1859,17 @@
     try 
-%7B%0A      
+(
 Buffered
@@ -1912,87 +1912,48 @@
 Fil)
-;
+) %7B
 %0A      
-final String s;%0A      s = in.readLine();%0A      final String%5B%5D split = s
+String%5B%5D split = in.readLine()
 .spl

Fix a resource leak in the `LinuxEphemeralPortRangeDetector`