本文整理汇总了Java中java.io.Closeable类的典型用法代码示例。如果您正苦于以下问题:Java Closeable类的具体用法?Java Closeable怎么用?Java Closeable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Closeable类属于java.io包,在下文中一共展示了Closeable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAppContextClassHierarchy
import java.io.Closeable; //导入依赖的package包/类
public void testAppContextClassHierarchy() {
Class<?>[] clazz =
ClassUtils.getClassHierarchy(OsgiBundleXmlApplicationContext.class, ClassUtils.ClassSet.ALL_CLASSES);
//Closeable.class,
Class<?>[] expected =
new Class<?>[] { OsgiBundleXmlApplicationContext.class,
AbstractDelegatedExecutionApplicationContext.class, AbstractOsgiBundleApplicationContext.class,
AbstractRefreshableApplicationContext.class, AbstractApplicationContext.class,
DefaultResourceLoader.class, ResourceLoader.class,
AutoCloseable.class,
DelegatedExecutionOsgiBundleApplicationContext.class,
ConfigurableOsgiBundleApplicationContext.class, ConfigurableApplicationContext.class,
ApplicationContext.class, Lifecycle.class, Closeable.class, EnvironmentCapable.class, ListableBeanFactory.class,
HierarchicalBeanFactory.class, ApplicationEventPublisher.class, ResourcePatternResolver.class,
MessageSource.class, BeanFactory.class, DisposableBean.class };
assertTrue(compareArrays(expected, clazz));
}
示例2: destroy
import java.io.Closeable; //导入依赖的package包/类
@After
public void destroy() {
if (nativeHibernateSessionFactoryBootstrap()) {
sf.close();
} else {
emf.close();
}
for (Closeable closeable : closeables) {
try {
closeable.close();
} catch (IOException e) {
LOGGER.error("Failure", e);
}
}
closeables.clear();
}
示例3: getSQLCommandsFromFile_closeStream_ioException
import java.io.Closeable; //导入依赖的package包/类
@Test
public void getSQLCommandsFromFile_closeStream_ioException()
throws Exception {
// given
handler = spy(new DatabaseUpgradeHandler());
// when
try {
handler.getSQLCommandsFromFile(new File("unknownpath"));
fail();
} catch (FileNotFoundException e) {
// then
assertNotNull(e);
verify(handler, times(3)).closeStream(any(Closeable.class));
}
}
示例4: testCheckSchemaSupport2
import java.io.Closeable; //导入依赖的package包/类
/**
* Test the default functionality of schema support method. In
* this case the schema source property is set.
* @throws Exception If any errors occur.
*/
@Test(dataProvider = "schema-source")
public void testCheckSchemaSupport2(Object schemaSource) throws Exception {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
dbf.setNamespaceAware(true);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
W3C_XML_SCHEMA_NS_URI);
dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", schemaSource);
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
db.parse(new File(XML_DIR, "test1.xml"));
assertFalse(eh.isErrorOccured());
} finally {
if (schemaSource instanceof Closeable) {
((Closeable) schemaSource).close();
}
}
}
示例5: handleClose
import java.io.Closeable; //导入依赖的package包/类
private void handleClose(MsgClose m) throws IOException {
int fileID = m.getFileID();
log.info(getName()+": Request close file "+fileID);
Closeable stream;
synchronized (localIS) {
stream = localIS.get(m.getFileID());
}
if (stream == null) { // Maybe an OutputStream?
synchronized (localOS) {
stream = localOS.get(m.getFileID());
}
}
if (stream == null) { // File descriptor not found
log.warning(getName()+": Local file ID "+fileID+" not found");
} else {
try {
stream.close();
log.fine(getName()+": Closed file "+fileID);
} catch (IOException e) { // Exception during skip
log.warning(getName()+": Error when closing file ID "+fileID+": "+e.getMessage());
}
}
}
示例6: close
import java.io.Closeable; //导入依赖的package包/类
@Override
public void close() {
Set<Node> nodes;
synchronized (current) {
nodes = new HashSet<>(current);
}
for (Node n : nodes) {
for (Closeable c : n.getLookup().lookupAll(Closeable.class)) {
try {
c.close();
} catch (IOException ex) {
LOGGER.log(Level.INFO, null, ex);
}
}
}
}
示例7: verifyIndexMetadata
import java.io.Closeable; //导入依赖的package包/类
/**
* This method verifies that the given {@code metaData} holds sane values to create an {@link IndexService}.
* This method tries to update the meta data of the created {@link IndexService} if the given {@code metaDataUpdate} is different from the given {@code metaData}.
* This method will throw an exception if the creation or the update fails.
* The created {@link IndexService} will not be registered and will be closed immediately.
*/
public synchronized void verifyIndexMetadata(IndexMetaData metaData, IndexMetaData metaDataUpdate) throws IOException {
final List<Closeable> closeables = new ArrayList<>();
try {
IndicesFieldDataCache indicesFieldDataCache = new IndicesFieldDataCache(settings, new IndexFieldDataCache.Listener() {});
closeables.add(indicesFieldDataCache);
IndicesQueryCache indicesQueryCache = new IndicesQueryCache(settings);
closeables.add(indicesQueryCache);
// this will also fail if some plugin fails etc. which is nice since we can verify that early
final IndexService service =
createIndexService("metadata verification", metaData, indicesQueryCache, indicesFieldDataCache, emptyList(), s -> {});
closeables.add(() -> service.close("metadata verification", false));
service.mapperService().merge(metaData, MapperService.MergeReason.MAPPING_RECOVERY, true);
if (metaData.equals(metaDataUpdate) == false) {
service.updateMetaData(metaDataUpdate);
}
} finally {
IOUtils.close(closeables);
}
}
示例8: close
import java.io.Closeable; //导入依赖的package包/类
/**
* Close closable object and wrap {@link IOException} with {@link RuntimeException}
* @param closeable closeable object
*/
public static void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
throw new RuntimeException("IOException occurred. ", e);
}
}
}
示例9: closeQuietly
import java.io.Closeable; //导入依赖的package包/类
static void closeQuietly(/*Auto*/Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (RuntimeException rethrown) {
throw rethrown;
} catch (Exception ignored) {
}
}
}
示例10: connectionIsMonitored
import java.io.Closeable; //导入依赖的package包/类
@Test
public void connectionIsMonitored() throws Exception {
factory.getProcessor(transport);
ArgumentCaptor<TTransport> transportCaptor = ArgumentCaptor.forClass(TTransport.class);
ArgumentCaptor<Closeable> handlerCaptor = ArgumentCaptor.forClass(Closeable.class);
verify(transportMonitor).monitor(transportCaptor.capture(), handlerCaptor.capture());
assertThat(transportCaptor.getValue(), is(transport));
assertThat(handlerCaptor.getValue(), is(instanceOf(FederatedHMSHandler.class)));
}
示例11: quietClose
import java.io.Closeable; //导入依赖的package包/类
public static void quietClose(Closeable closeable){
if (closeable != null) {
try {
closeable.close();
} catch (Throwable e) {
}
}
}
示例12: tryToClose
import java.io.Closeable; //导入依赖的package包/类
private void tryToClose(@Nullable Closeable c) {
try {
if (c != null) c.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例13: close
import java.io.Closeable; //导入依赖的package包/类
public static void close(Closeable closeable) {
if (null == closeable) return;
try {
closeable.close();
} catch (IOException e) {
e.printStackTrace();
}
}
示例14: storeClassLoader
import java.io.Closeable; //导入依赖的package包/类
private ClassLoader storeClassLoader(ClassLoader classLoader) {
final ClassLoader previous = currentClassloader.getAndSet(classLoader);
if (previous != null && previous instanceof Closeable) {
loadersToClose.add(new SoftReference<Closeable>(Cast.cast(Closeable.class, previous)));
}
return classLoader;
}
示例15: closeQuietly
import java.io.Closeable; //导入依赖的package包/类
/**
* Closes a {@link Closeable}, suppressing any {@link IOException} that may occur. Both {@link
* java.io.OutputStream} and {@link InputStream} are {@code Closeable}.
*
* @param closeable The {@link Closeable} to close.
*/
public static void closeQuietly(Closeable closeable) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException e) {
// Ignore.
}
}