![JAR search and dependency download from the Maven repository](/logo.png)
com.tsc9526.monalisa.tools.io.MelpClose Maven / Gradle / Ivy
/*******************************************************************************************
* Copyright (c) 2016, zzg.zhou([email protected])
*
* Monalisa is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*******************************************************************************************/
package com.tsc9526.monalisa.tools.io;
import java.io.Closeable;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.util.Timer;
import java.util.TimerTask;
/**
*
* @author zzg.zhou([email protected])
*/
public class MelpClose {
public static void delayClose(final Object x, int delay){
final Timer timer=new Timer(true);
timer.schedule(new TimerTask() {
public void run() {
try{
close(x);
}finally{
timer.cancel();
}
}
}, delay*1000);
}
public static void close(Object ... xs){
for(Object x:xs){
if(x instanceof Closeable){
close((Closeable)x);
}else if(x instanceof AutoCloseable){
close((AutoCloseable)x);
}else if(x instanceof HttpURLConnection){
close((HttpURLConnection)x);
}else{
try{
Method m=x.getClass().getMethod("close");
m.setAccessible(true);
m.invoke(x);
}catch(NoSuchMethodException e){
//do nothing
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
}
public static void close(HttpURLConnection c){
try{
if(c!=null){
c.disconnect();
}
}catch(Exception e){}
}
public static void close(Closeable c){
try{
if(c!=null){
c.close();
}
}catch(Exception e){}
}
public static void close(AutoCloseable c) {
try{
if(c!=null){
c.close();
}
}catch(Exception e){}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy