Skip to content

Commit 0ae502e

Browse files
committed
IO-224 Add closeQuietly(Socket) to IOUtils
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@920497 13f79535-47bb-0310-9956-ffa450edef68
1 parent 99a6100 commit 0ae502e

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

src/java/org/apache/commons/io/IOUtils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.io.PrintWriter;
3232
import java.io.Reader;
3333
import java.io.Writer;
34+
import java.net.Socket;
3435
import java.nio.channels.Channel;
3536
import java.util.ArrayList;
3637
import java.util.Collection;
@@ -221,6 +222,24 @@ public static void closeQuietly(Closeable closeable) {
221222
}
222223
}
223224

225+
/**
226+
* Unconditionally close a <code>Socket</code>.
227+
* <p>
228+
* Equivalent to {@link Socket#close()}, except any exceptions will be ignored.
229+
* This is typically used in finally blocks.
230+
*
231+
* @param sock the Socket to close, may be null or already closed
232+
*/
233+
public static void closeQuietly(Socket sock){
234+
if (sock != null){
235+
try {
236+
sock.close();
237+
} catch (IOException ioe) {
238+
// ignored
239+
}
240+
}
241+
}
242+
224243
/**
225244
* Fetches entire contents of an <code>InputStream</code> and represent
226245
* same data as result InputStream.

0 commit comments

Comments
 (0)