/** * Safely close, absorbing exceptions and handling <code>null</code> * graciously. * * @param object object to close. */ public static void safeClose(Closeable object) { try { if (object != null) { object.close(); } } catch (IOException e) { log(object, e); } }
private void writeToFile(byte[] data) throws IOException { if (file == null) return; file.getParentFile().mkdirs(); FileOutputStream out = new FileOutputStream(file); try { out.write(data); } finally { CloseUtils.safeClose(out); } }
/** * Safely close a socket, absorbing exceptions and handling * <code>null</code> graciously. * * @param socket object to close. */ public static void safeClose(Socket socket) { try { if (socket != null) { socket.close(); } } catch (IOException e) { log(socket, e); } }
public DicomInputStream(File f) throws IOException { super(new BufferedInputStream(new FileInputStream(f))); try { this.ts = guessTransferSyntax(); } catch (IOException e) { CloseUtils.safeClose(this); throw e; } }
/** * Safely close a JDBC Statement, absorbing exceptions and handling * <code>null</code> graciously. * * @param statement * to close. */ public static void safeClose(Statement statement) { try { if (statement != null) { statement.close(); } } catch (SQLException e) { log(statement, e); } }
private void closeSocket() { if (state == State.STA13) { try { Thread.sleep(connector.getSocketCloseDelay()); } catch (InterruptedException e) { log.warn("Interrupted Socket Close Delay", e); } } setState(State.STA1); CloseUtils.safeClose(out); CloseUtils.safeClose(in); if (!closed) { log.info("{}: close {}", name, socket); CloseUtils.safeClose(socket); closed = true; onClosed(); } }
/** * Safely close a server socket, absorbing exceptions and handling * <code>null</code> graciously. * * @param socket object to close. */ public static void safeClose(ServerSocket socket) { try { if (socket != null) { socket.close(); } } catch (IOException e) { log(socket, e); } }
protected ImageReaderWriterFactory(String key, String def) { String val = System.getProperty(key, def); URL url; try { url = new URL(val); } catch (MalformedURLException e) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl == null || (url = cl.getResource(val)) == null) { if ((url = ImageReaderWriterFactory.class.getClassLoader() .getResource(val)) == null) { throw new ConfigurationError("missing resource: " + val); } } } InputStream is = null; try { is = url.openStream(); config.load(is); } catch (IOException e) { throw new ConfigurationError( "failed to load imageio configuration from " + url, e); } finally { CloseUtils.safeClose(is); } }
/** * Safely flush, absorbing smells, sounds, exceptions and handling <code>null</code> * graciously. * * @param object object to close. */ public static void safeFlush(Flushable object) { try { if (object != null) { object.flush(); } } catch (IOException e) { log(object, e); } }
public static Object loadResource(String name) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is; if (cl == null || (is = cl.getResourceAsStream(name)) == null) { is = ResourceLocator.class.getClassLoader().getResourceAsStream( name); if (is == null) { throw new ConfigurationError("Missing Resource: " + name); } } try { ObjectInputStream ois = new ObjectInputStream(is); return ois.readObject(); } catch (Exception e) { throw new ConfigurationError("Failed to load Resource " + name, e); } finally { CloseUtils.safeClose(is); } }
/** * Safely close an ImageInputStream stream, absorbing exceptions and * handling <code>null</code> graciously. * * @param is to close. */ public static void safeClose(ImageInputStream is) { try { if (is != null) { is.close(); } } catch (IOException e) { log(is, e); } }
+ url, e); } finally { CloseUtils.safeClose(in);
public static List<String> findResources(Class<?> c) { ArrayList<String> list = new ArrayList<String>(); try { for (Enumeration<URL> configs = enumResources(PREFIX + c.getName()); configs.hasMoreElements();) { URL u = configs.nextElement(); InputStream in = u.openStream(); try { BufferedReader r = new BufferedReader( new InputStreamReader(in, "utf-8")); String ln; while ((ln = r.readLine()) != null) { int end = ln.indexOf('#'); if (end >= 0) ln = ln.substring(0, end); ln = ln.trim(); if (ln.length() > 0) list.add(ln); } } finally { CloseUtils.safeClose(in); } } return list; } catch (IOException e) { throw new ConfigurationError("Failed to find Resources for " + c, e); } }
return; } finally { CloseUtils.safeClose(in);
return; } finally { CloseUtils.safeClose(in);
private DicomObject readDicomObject(TransferSyntax ts) throws IOException { DicomObject dcm = new BasicDicomObject(); DicomInputStream din = new DicomInputStream(this, ts); try { din.readDicomObject(dcm, -1); } catch (DicomCodingException e) { log.warn(as.toString() + ": Failed to decode dicom object: " + e.getMessage()); throw new AAbort(); } finally { CloseUtils.safeClose(din); } return dcm; }