下面列出了java.io.Closeable#close ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testGetConnectionHandleNoArgPrefersEmptySourceTarget() throws IOException {
HBaseConnectionHandle connectionHandle = hBaseConnectionPool.getConnectionHandle();
HBaseConnectionWrapper connection = connectionHandle.getConnection();
List<Closeable> toClose = new ArrayList<>();
toClose.add( connectionHandle );
for ( int i = 0; i < 100; i++ ) {
toClose.add( hBaseConnectionPool.getConnectionHandle( "source" ) );
}
for ( int i = 0; i < 100; i++ ) {
toClose.add( hBaseConnectionPool.getConnectionHandle( "target", mock( Properties.class ) ) );
}
for ( Closeable closeable : toClose ) {
closeable.close();
}
assertEquals( connection, hBaseConnectionPool.getConnectionHandle().getConnection() );
}
/**
* Close the given closeable object (Stream) in a safe way: check if it is null and catch-log
* exception thrown.
*
* @param closeable the closable object to close
*/
private static void closeSafe(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignored) {
}
}
}
/**
* Closes 'closeable', ignoring any checked exceptions. Does nothing if 'closeable' is null.
*/
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
/**
* Closes the specified stream.
*
* @param stream The stream to close.
*/
static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
Log.e(LOG_TAG, "Could not close stream", e);
}
}
}
/**
* Closes a closable object.
* @param object the object to close
*/
private void close(final Closeable object) {
if (object != null) {
try {
object.close();
} catch (IOException e) {
LOGGER.warn("Error closing stream", e);
}
}
}
private void tryClosingResourceSilently(Closeable res) {
if (res != null) {
try {
res.close();
} catch (IOException e) {
LOGGER.error("Closing resource: " + res + " failed with error: ", e);
}
}
}
public void stop() {
if (stopped) {
return;
}
LOG.fine("Server is stopping.");
for (Closeable c : endpoint.getCleanupHooks()) {
try {
c.close();
} catch (IOException e) {
//ignore
}
}
if (slcMgr != null) {
slcMgr.stopServer(this);
}
MessageObserver mo = getDestination().getMessageObserver();
if (mo instanceof MultipleEndpointObserver) {
((MultipleEndpointObserver)mo).getEndpoints().remove(endpoint);
if (((MultipleEndpointObserver)mo).getEndpoints().isEmpty()) {
getDestination().setMessageObserver(null);
}
} else {
getDestination().setMessageObserver(null);
}
stopped = true;
}
/**
* Close the io stream quietly.
*
* @param closeables The closeables.
*/
public static void closeIOQuietly(final Closeable... closeables) {
if (closeables == null) return;
for (Closeable closeable : closeables) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignored) {
}
}
}
}
public static void closeSilently(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignored) {
}
}
}
public static void close(Closeable stream) {
try {
if (stream != null)
stream.close();
} catch (IOException e) {
}
}
/**
* Closes the given resource if it is available, logging any closing exceptions to the given log.
*
* @param closeable the object to close
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
// ignore
}
}
}
/**
* Close a closeable object quietly, added because such method in APACHE-IO
* has been deprecated
*
* @param closeable object to be closed
*/
public static void closeSilently(@Nullable final Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (final IOException ignoring) {
}
}
public static void closeSilently(@Nullable Closeable c) {
if (c == null) return;
try {
c.close();
} catch (Throwable t) {
// Do nothing
}
}
/**
* Closes the passed closeable without raising exceptions.
*
* @param closeable the object to close or null
*/
public static void closeSilently(@Nullable Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignore) {
}
}
}
/**
* 关闭 IO
*
* @param closeables closeables
*/
public static void closeIO(Closeable... closeables) {
if (closeables == null) return;
for (Closeable closeable : closeables) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException ignore) {
// skip
}
}
}
@VisibleForTesting
static void close(Closeable closeable) {
try {
closeable.close();
}
catch (IOException ex) {
throw new IllegalStateException("failed to close object", ex);
}
}
/**
* Close the given closeable silently.
* @param c the closeable
*/
void close0(Closeable c) {
try {
if (c != null) {
c.close();
}
} catch (IOException ex) {
// Ignored
}
}
protected static void closeStream ( final Closeable stream )
{
if ( stream == null )
{
return;
}
try
{
stream.close ();
}
catch ( final IOException e )
{
logger.warn ( "Failed to close stream", e );
}
}
private static void closeQuietly(Closeable obj) {
try {
if (obj != null) {
obj.close();
}
} catch (IOException ignored) {
// Ignore.
}
}