@Override public void deleteFile(File f) { String path = f.getPath(); // First remove the channel from the map FileChannel fc = streams.remove(path); // Then close the channel (handles null params) Util.close(fc); // Now that the channel is closed we can delete the file f.delete(); }
@Override public void stop() { for (FileChannel channel : streams.values()) { try { channel.force(true); } catch (IOException e) { log.errorFlushingToFileChannel(channel, e); } Util.close(channel); } streams.clear(); }
@Override public void purge(File f) throws IOException { // Avoid a delete per-se because it hampers any fsync-like functionality // cos any cached file channel write won't change the file's exists // status. So, clear the file rather than delete it. FileChannel channel = streams.get(f.getPath()); if (channel == null) { channel = createChannel(f); String path = f.getPath(); FileChannel existingChannel = streams.putIfAbsent(path, channel); if (existingChannel != null) { Util.close(channel); channel = existingChannel; } } channel.truncate(0); // Apart from truncating, it's necessary to reset the position! channel.position(0); }
@Override public void run() { for (Map.Entry<String, FileChannel> entry : streams.entrySet()) { if (trace) log.tracef("Flushing channel in %s", entry.getKey()); FileChannel channel = entry.getValue(); try { channel.force(true); } catch (IOException e) { if (trace) log.tracef(e, "Error flushing output stream for %s", entry.getKey()); flushErrors.putIfAbsent(entry.getKey(), e); // If an error is encountered, close it. Next time it's used, // the exception will be propagated back to the user. Util.close(channel); } } } }, interval, interval, TimeUnit.MILLISECONDS);
@Override public void write(byte[] bytes, File f) throws IOException { if (bytes.length == 0) { // Short circuit for deleting files deleteFile(f); return; } String path = f.getPath(); FileChannel channel = streams.get(path); if (channel == null) { channel = createChannel(f); FileChannel existingChannel = streams.putIfAbsent(path, channel); if (existingChannel != null) { Util.close(channel); channel = existingChannel; } } else if (!f.exists()) { f.createNewFile(); FileChannel oldChannel = channel; channel = createChannel(f); boolean replaced = streams.replace(path, oldChannel, channel); if (replaced) { Util.close(oldChannel); } else { Util.close(channel); channel = streams.get(path); } } channel.write(ByteBuffer.wrap(bytes)); }
public Properties readEnvironmentProperties() throws CacheLoaderException { if (environmentPropertiesFile == null || environmentPropertiesFile.trim().length() == 0) return null; InputStream i = FileLookupFactory.newInstance().lookupFile(environmentPropertiesFile, getClassLoader()); if (i != null) { Properties p = new Properties(); try { p.load(i); } catch (IOException ioe) { throw new CacheLoaderException("Unable to read environment properties file " + environmentPropertiesFile, ioe); } finally { Util.close(i); } return p; } else { return null; } } }