/** * Close an input stream without throwing an exception. * * @param in the input stream or null */ public static void closeSilently(InputStream in) { if (in != null) { try { trace("closeSilently", null, in); in.close(); } catch (Exception e) { // ignore } } }
/** * Close a Closeable without throwing an exception. * * @param out the Closeable or null */ public static void closeSilently(Closeable out) { if (out != null) { try { trace("closeSilently", null, out); out.close(); } catch (Exception e) { // ignore } } }
/** * Close an AutoCloseable without throwing an exception. * * @param out the AutoCloseable or null */ public static void closeSilently(AutoCloseable out) { if (out != null) { try { trace("closeSilently", null, out); out.close(); } catch (Exception e) { // ignore } } }
/** * Call the garbage collection and run finalization. This close all files * that were not closed, and are no longer referenced. */ static void freeMemoryAndFinalize() { IOUtils.trace("freeMemoryAndFinalize", null, null); Runtime rt = Runtime.getRuntime(); long mem = rt.freeMemory(); for (int i = 0; i < 16; i++) { rt.gc(); long now = rt.freeMemory(); rt.runFinalization(); if (now == mem) { break; } mem = now; } }
@Override public void delete() { File file = new File(name); for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) { IOUtils.trace("delete", name, null); boolean ok = file.delete(); if (ok || !file.exists()) { return; } wait(i); } throw DbException.get(ErrorCode.FILE_DELETE_FAILED_1, name); }
@Override public OutputStream newOutputStream(boolean append) throws IOException { try { File file = new File(name); File parent = file.getParentFile(); if (parent != null) { FileUtils.createDirectories(parent.getAbsolutePath()); } FileOutputStream out = new FileOutputStream(name, append); IOUtils.trace("openFileOutputStream", name, out); return out; } catch (IOException e) { freeMemoryAndFinalize(); return new FileOutputStream(name); } }
IOUtils.trace("openFileInputStream", name, in); return in;
/** * Add a file to the list of temp files to delete. The file is deleted once * the file object is garbage collected. * * @param fileName the file name * @param file the object to monitor * @return the reference that can be used to stop deleting the file */ public synchronized Reference<?> addFile(String fileName, Object file) { IOUtils.trace("TempFileDeleter.addFile", fileName, file); PhantomReference<?> ref = new PhantomReference<>(file, queue); refMap.put(ref, fileName); deleteUnused(); return ref; }
/** * This method is called if a file should no longer be deleted if the object * is garbage collected. * * @param ref the reference as returned by addFile * @param fileName the file name */ public void stopAutoDelete(Reference<?> ref, String fileName) { IOUtils.trace("TempFileDeleter.stopAutoDelete", fileName, ref); if (ref != null) { String f2 = refMap.remove(ref); if (SysProperties.CHECK) { if (f2 == null || !f2.equals(fileName)) { DbException.throwInternalError("f2:" + f2 + " " + (f2 == null ? "" : f2) + " f:" + fileName); } } } deleteUnused(); }
IOUtils.trace("rename", name + " >" + newName, null); boolean ok = oldFile.renameTo(newFile); if (ok) {
/** * Delete the given file now. This will remove the reference from the list. * * @param ref the reference as returned by addFile * @param fileName the file name */ public synchronized void deleteFile(Reference<?> ref, String fileName) { if (ref != null) { String f2 = refMap.remove(ref); if (f2 != null) { if (SysProperties.CHECK) { if (fileName != null && !f2.equals(fileName)) { DbException.throwInternalError("f2:" + f2 + " f:" + fileName); } } fileName = f2; } } if (fileName != null && FileUtils.exists(fileName)) { try { IOUtils.trace("TempFileDeleter.deleteFile", fileName, null); FileUtils.tryDelete(fileName); } catch (Exception e) { // TODO log such errors? } } }
@Override public FileChannel open(String mode) throws IOException { FileDisk f; try { f = new FileDisk(name, mode); IOUtils.trace("open", name, f); } catch (IOException e) { freeMemoryAndFinalize(); try { f = new FileDisk(name, mode); } catch (IOException e2) { throw e; } } return f; }
public static void closeSilently(OutputStream out) { if (out != null) { try { trace("closeSilently", null, out); out.close(); } catch (IOException e) { // ignore } } }
public static void closeSilently(InputStream in) { if (in != null) { try { trace("closeSilently", null, in); in.close(); } catch (IOException e) { // ignore } } }
/** * Close an output stream without throwing an exception. * * @param out the output stream or null */ public static void closeSilently(Closeable out) { if (out != null) { try { trace("closeSilently", null, out); out.close(); } catch (Exception e) { // ignore } } }
/** * Close an input stream without throwing an exception. * * @param in the input stream or null */ public static void closeSilently(InputStream in) { if (in != null) { try { trace("closeSilently", null, in); in.close(); } catch (Exception e) { // ignore } } }
@Override public void delete() { File file = new File(name); for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) { IOUtils.trace("delete", name, null); boolean ok = file.delete(); if (ok || !file.exists()) { return; } wait(i); } throw DbException.get(ErrorCode.FILE_DELETE_FAILED_1, name); }
@Override public void delete() { File file = new File(name); for (int i = 0; i < SysProperties.MAX_FILE_RETRY; i++) { IOUtils.trace("delete", name, null); boolean ok = file.delete(); if (ok || !file.exists()) { return; } wait(i); } throw DbException.get(ErrorCode.FILE_DELETE_FAILED_1, name); }
/** * Add a file to the list of temp files to delete. The file is deleted once * the file object is garbage collected. * * @param fileName the file name * @param file the object to monitor * @return the reference that can be used to stop deleting the file */ public synchronized Reference<?> addFile(String fileName, Object file) { IOUtils.trace("TempFileDeleter.addFile", fileName, file); PhantomReference<?> ref = new PhantomReference<Object>(file, queue); refMap.put(ref, fileName); deleteUnused(); return ref; }
@Override public FileChannel open(String mode) throws IOException { FileDisk f; try { f = new FileDisk(name, mode); IOUtils.trace("open", name, f); } catch (IOException e) { freeMemoryAndFinalize(); try { f = new FileDisk(name, mode); } catch (IOException e2) { throw e; } } return f; }