Wednesday, September 16, 2015

JAVA Closeables Utility


import java.io.Closeable;
import java.io.IOException;
import java.net.ServerSocket;



/**
 * A set of utilities for all {@link Closeable}s
 *
 */
public class Closeables {

/**
* Closes the {@code closeable} passed ignoring any I/O exception by logging.
* Does nothing if {@code closeable} is {@code null}.
*
* @param closeable closeable to be closed, does nothing if {@code null}
*/
public static void closeQuietly(Closeable closeable) {
if (closeable == null) {
return;
}

try {
closeable.close();
} catch (IOException e) {

}
}

/**
* Closes all the {@code closeables} passed ignoring any I/O exceptions by logging.
* Skips any {@code null}s that occur.
*
* @param closeables all the closeables to be closed, does nothing if {@code null} is passed
*/
public static void closeQuietly(Closeable... closeables){
if (closeables == null) {
return;
}

for (Closeable closeable : closeables) {
closeQuietly(closeable);
}

}

/**
* Closes the {@code serverSocket} passed, ignoring any I/O exception by logging.
* Does nothing if {@code serverSocket} is {@code null}.
*
* @param serverSocket a nullable server socket to be closed, does nothing if null
*/
public static void closeQuietly(ServerSocket serverSocket) {
if (serverSocket == null) {
return;
}

try {
serverSocket.close();
} catch (IOException e) {

}
}
}

No comments:

Post a Comment

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...