@Override public void run() { try { // wczytanie nazwy przesylanego pliku InputStream is = clientSocket.getInputStream(); byte[] buffer = new byte[50]; is.read(buffer); name = new String(buffer).trim(); // czyszczenie ze spacji path += name; System.out.println("Pobieranie pliku " + name + " w toku."); Path assumedPath = Paths.get(path); // if(Files.deleteIfExists(assumedPath)) // System.out.println("Poprzedni plik o nazwie " +assumedPath.getFileName()+" zosta³ // skasowany."); Files.createFile(assumedPath); } catch (IOException e1) { System.out.println("Mamy problem."); e1.printStackTrace(); } try ( // Strumien zapisu do pliku FileOutputStream fos = new FileOutputStream(path); // Strumien odczytu danych z socketu InputStream is = clientSocket.getInputStream(); ) { byte[] buffer = new byte[1024]; while (is.read(buffer) > -1) { fos.write(buffer); // zapis do pliku fos.flush(); } // proces zamkniecia socketu AutoCloseable acsock = clientSocket; AutoCloseable acfos = fos; AutoCloseable acis = is; try { acsock.close(); acfos.close(); acis.close(); System.out.println("Zakonczono przesylanie pliku: " + name); } catch (Exception e) { System.out.println("Nie mo¿na zamkn¹æ socketu!"); e.printStackTrace(); } } catch (IOException e) { System.out.println(e.getMessage()); } }
/** * Close the given closeable. * * @param ac An {@link AutoCloseable} to be closed. */ private void close(AutoCloseable ac) { try { ac.close(); } catch (Exception e) { logger.error("Failed to close instance: " + ac, e); } }
@Override public void close() throws Exception { if (deactivator != null) { deactivator.close(); deactivator = null; } }
@Override public void close() throws Exception { for (Object o : content) { if (o instanceof AutoCloseable) { ((AutoCloseable) o).close(); } } }
private void stopConfiguration() throws Exception { if (configuration == null) { // abort received but script not prepared, therefore entire script failed if (progress == null) { progress = new ScriptProgress(newSequential(0, 0), ""); } RegionInfo scriptInfo = progress.getScriptInfo(); progress.addScriptFailure(scriptInfo); } else { // stopping the configuration will implicitly trigger the script complete listener // to handle incomplete script that is being aborted by canceling the finish future // clear out the pipelines for new connections to avoid impacting the observed script for (ServerBootstrapResolver serverResolver : configuration.getServerResolvers()) { ServerBootstrap server = serverResolver.resolve(); server.setPipelineFactory(pipelineFactory(pipeline(closeOnExceptionHandler))); } for (ClientBootstrapResolver clientResolver : configuration.getClientResolvers()) { ClientBootstrap client = clientResolver.resolve(); client.setPipelineFactory(pipelineFactory(pipeline(closeOnExceptionHandler))); } // remove each handler from the configuration pipelines // this will trigger failures for any handlers on a pipeline for an incomplete stream // including pipelines not yet associated with any channel for (ChannelPipeline pipeline : configuration.getClientAndServerPipelines()) { stopStream(pipeline); } // cancel any pending binds and connects for (ChannelFuture bindFuture : bindFutures) { bindFuture.cancel(); } for (ChannelFuture connectFuture : connectFutures) { connectFuture.cancel(); } // close server and client channels final ChannelGroupFuture closeFuture = serverChannels.close(); closeFuture.addListener( new ChannelGroupFutureListener() { @Override public void operationComplete(final ChannelGroupFuture future) { clientChannels.close(); } }); for (AutoCloseable resource : configuration.getResources()) { try { resource.close(); } catch (Exception e) { // ignore } } } }
@Override public synchronized void close() throws Exception { factories.clear(); for (AutoCloseable reg : registrations.values()) { reg.close(); } registrations.clear(); listeners.clear(); }
protected void closeQuietly(AutoCloseable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception ignore) { // ignore } } }
private void close(AutoCloseable closeable) { if (closeable != null) { try { closeable.close(); } catch (Exception e) { // muted } } }
private void closeQuietly(Res result) { if (result instanceof AutoCloseable) { try { ((AutoCloseable) result).close(); } catch (RuntimeException rethrown) { throw rethrown; } catch (Exception ignored) { } } }
public static void close(final AutoCloseable... closableList) { for (final AutoCloseable closable : closableList) { if (closable != null) { try { closable.close(); } catch (final Throwable ignored) { } } } }
public static void closeQuietly(AutoCloseable closeable) { if (closeable != null) { try { closeable.close(); } catch (RuntimeException rethrown) { throw rethrown; } catch (Exception ignored) { } } }
private static void close(AutoCloseable closable) { try { if (closable != null) { closable.close(); closable = null; } } catch (Exception sqle) { sqle.printStackTrace(); } }
public static void closeQuietly(AutoCloseable resource) { if (resource != null) { try { resource.close(); logger.debug("The {} successfully closed", resource.getClass()); } catch (Exception e) { /* NOP */ } } }
public static void closeRes(AutoCloseable res) { try { if (res != null) { res.close(); res = null; } } catch (Exception e) { e.printStackTrace(); } }
@Override protected void after() { for (AutoCloseable toClose : toCloseAfterwards) { try { toClose.close(); } catch (Exception e) { throw new RuntimeException(e); } } }
/** * Close a resource, logging an error if an error occurs. * * @param resource the resource to close */ public static void safeClose(final AutoCloseable resource) { try { if (resource != null) { closeMsg.closingResource(resource); resource.close(); } } catch (ClosedChannelException ignored) { } catch (Throwable t) { closeMsg.resourceCloseFailed(t, resource); } }
/** * This method is called when an {@link Error} or {@link RuntimeException} occured and the * connection should be closed. * * @param closeable The closeable instance that should be closed. * @param e The {@link Error} or {@link RuntimeException} that occured. */ protected void closeCloseableDueToThrowable(final AutoCloseable closeable, final Throwable e) { try { closeable.close(); } catch (Throwable th) { e.addSuppressed(th); } if (e instanceof Error) { throw (Error) e; } else if (e instanceof RuntimeException) { throw (RuntimeException) e; } }
@Override public final void close() { synchronized (this) { if (closed) return; closed = true; } onClose(); notifier.quit(); try { if (conn != null) conn.close(); } catch (final Exception ignore) { } }
@Override public void close() { Exception toThrow = null; for (AutoCloseable c : this) { try { c.close(); } catch (Exception e) { toThrow = e; } } if (toThrow != null) { throw new RuntimeException(toThrow); } }
@Override public synchronized void onRemoveNetconfOperationServiceFactory( NetconfOperationServiceFactory service) { factories.remove(service); for (final AutoCloseable autoCloseable : registrations.get(service)) { try { autoCloseable.close(); } catch (Exception e) { LOG.warn("Unable to close listener registration", e); } } registrations.removeAll(service); }
/** {@inheritDoc} */ @Override public void close() { Iterator<T> i; if ((i = iter) != null) { iter = null; if (i instanceof AutoCloseable) { try { ((AutoCloseable) i).close(); } catch (Exception e) { throw new IgniteException(e); } } } }
@Override public synchronized void stop(BundleContext context) throws Exception { Exception lastException = null; for (AutoCloseable autoCloseable : autoCloseables) { try { autoCloseable.close(); } catch (Exception e) { if (lastException == null) { lastException = e; } else { lastException.addSuppressed(e); } } } if (lastException != null) { throw lastException; } }
/** * Close the given resource, catching and adding any caught exceptions via {@link * #addException(Exception)}. If an exception is caught, it will change the result state to * FAILED, regardless of what its current value. * * @param autoCloseable the resource to close */ private void suppressingClose(final AutoCloseable autoCloseable) { Preconditions.checkState(!isClosed); Preconditions.checkState(resultState != null); if (autoCloseable == null) { return; } try { autoCloseable.close(); } catch (final Exception e) { /* * Even if the query completed successfully, we'll still report failure if we have * problems cleaning up. */ resultState = QueryState.FAILED; addException(e); } }
@Test public void testCreateInstance() throws Exception { // configure mocks DependencyResolver dependencyResolver = mock(DependencyResolver.class); BindingAwareBroker broker = mock(BindingAwareBroker.class); when(dependencyResolver.resolveInstance( eq(BindingAwareBroker.class), any(ObjectName.class), any(JmxAttribute.class))) .thenReturn(broker); // create instance of module with injected mocks VtnappModule module = new VtnappModule(mock(ModuleIdentifier.class), dependencyResolver); // getInstance calls resolveInstance to get the broker dependency and then calls createInstance AutoCloseable closeable = module.getInstance(); // verify that the module registered the returned provider with the broker verify(broker).registerProvider((VtnappProvider) closeable); // ensure no exceptions on close closeable.close(); }
@Override public void write(ISession session, O msg) { final ConcurrentHashMap<Long, IChannel> channels = m_channels; if (channels == null) return; final IChannel channel = channels.get(session.id()); if (channel != null) { channel.write(msg); return; } final Object remoteAddr = session.remoteAddress(); c_logger.warn( StrUtil.join( session, "(remoteAddr=", remoteAddr, ") failed to send(channel closed): ", StrUtil.getLineSeparator(), msg)); if (msg instanceof AutoCloseable) { try { ((AutoCloseable) msg).close(); } catch (Throwable t) { c_logger.error( StrUtil.join( session, "(remoteAddr=", remoteAddr, ") failed to close message: ", StrUtil.getLineSeparator(), msg), t); } } }
@Override public void close() throws Exception { if (content instanceof AutoCloseable) { ((AutoCloseable) content).close(); } }
/** * tear down test case - close registrations * * @throws Exception */ @After public void tearDown() throws Exception { regToOFJava.close(); regToOFJava.close(); }
/** * Test method for {@link * org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl#unregister(org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey, * org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava)}. * * @throws Exception */ @Test public void testUnregisterMessageTypeKeyOfQConvertorFromOFJavaOfQQ() throws Exception { regFromOFJava.close(); Assert.assertNull(manager.getConverter(keyFromOFJava)); }
/** * Test method for {@link * org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl#unregister(org.opendaylight.openflowplugin.extension.api.ConverterExtensionKey, * org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava)}. * * @throws Exception */ @Test public void testUnregisterConverterExtensionKeyOfQConvertorToOFJavaOfQQ() throws Exception { regToOFJava.close(); Assert.assertNull(manager.getConverter(keyToOFJava)); }
public static <E extends Exception> void destroy(E exception, Object... objects) throws E { for (Object object : objects) { if (log.isDebugEnabled()) log.debug("Destroying " + object); if (object instanceof AutoCloseable) { try { if (log.isDebugEnabled()) log.debug("Invoking inferred destruction method AutoCloseable.close() for " + object); ((AutoCloseable) object).close(); return; } catch (Exception e) { exception.addSuppressed(e); throw exception; } } for (Method method : object.getClass().getMethods()) { if (method.isAnnotationPresent(PreDestroy.class)) { if (method.getParameterTypes().length > 0) exception.addSuppressed( new IllegalArgumentException( "@PreDestroy method in " + object.getClass() + " implementing " + asList(object.getClass().getInterfaces()) + " has arguments")); else if (!"close".equals(method.getName())) try { if (log.isDebugEnabled()) log.debug( "Invoking annotated destruction method " + method.getName() + " for " + object); method.invoke(object); return; } catch (Exception e) { exception.addSuppressed(e); } } } if (object instanceof Object[]) for (Object element : (Object[]) object) try { if (log.isDebugEnabled()) log.debug("Inferred array"); destroy(exception, element); } catch (Exception e) { } else if (object instanceof Iterable<?>) for (Object element : (Iterable<?>) object) try { if (log.isDebugEnabled()) log.debug("Inferred collection"); destroy(exception, element); } catch (Exception e) { } else if (object instanceof Map<?, ?>) { try { destroy(exception, ((Map<?, ?>) object).keySet()); } catch (Exception e) { } try { destroy(exception, ((Map<?, ?>) object).values()); } catch (Exception e) { } } } if (exception.getSuppressed().length > 0) throw exception; }