use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class PersistNews method load.
/**
* This does not check for any missing values.
* Any fields in any NewsEntry may be null.
* Content is not sanitized by NewsXMLParser here, do that before storing.
*
* @return non-null, sorted by updated date, newest first
*/
public static List<NewsEntry> load(I2PAppContext ctx) {
Log log = ctx.logManager().getLog(PersistNews.class);
File dir = new File(ctx.getConfigDir(), DIR);
List<NewsEntry> rv = new ArrayList<NewsEntry>();
File[] files = dir.listFiles(new FileSuffixFilter(PFX, SFX));
if (files == null)
return rv;
for (File file : files) {
String name = file.getName();
XMLParser parser = new XMLParser(ctx);
InputStream in = null;
Node node;
boolean error = false;
try {
in = new GZIPInputStream(new FileInputStream(file));
node = parser.parse(in);
NewsEntry entry = extract(node);
if (entry != null) {
rv.add(entry);
} else {
if (log.shouldWarn())
log.warn("load error from " + file);
error = true;
}
} catch (ParserException pe) {
if (log.shouldWarn())
log.warn("load error from " + file, pe);
error = true;
} catch (IOException ioe) {
if (log.shouldWarn())
log.warn("load error from " + file, ioe);
error = true;
} finally {
if (in != null)
try {
in.close();
} catch (IOException ioe) {
}
}
if (error)
file.delete();
}
Collections.sort(rv);
return rv;
}
use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class SnarkManager method monitorTorrents.
/**
* caller must synchronize on _snarks
*
* @return success, false if an error adding any torrent.
*/
private boolean monitorTorrents(File dir) {
boolean rv = true;
File[] files = dir.listFiles(new FileSuffixFilter(".torrent"));
List<String> foundNames = new ArrayList<String>(0);
if (files != null) {
for (int i = 0; i < files.length; i++) {
try {
foundNames.add(files[i].getCanonicalPath());
} catch (IOException ioe) {
_log.error("Error resolving '" + files[i] + "' in '" + dir, ioe);
}
}
}
Set<String> existingNames = listTorrentFiles();
if (_log.shouldLog(Log.DEBUG))
_log.debug("DirMon found: " + DataHelper.toString(foundNames) + " existing: " + DataHelper.toString(existingNames));
// lets find new ones first...
for (String name : foundNames) {
if (existingNames.contains(name)) {
// already known. noop
} else {
if (shouldAutoStart() && !_util.connect())
addMessage(_t("Unable to connect to I2P!"));
try {
// Snark.fatal() throws a RuntimeException
// don't let one bad torrent kill the whole loop
addTorrent(name, null, !shouldAutoStart());
} catch (RuntimeException e) {
addMessage(_t("Error: Could not add the torrent {0}", name) + ": " + e);
_log.error("Unable to add the torrent " + name, e);
rv = false;
}
}
}
// Don't remove magnet torrents that don't have a torrent file yet
existingNames.removeAll(_magnets);
// now lets see which ones have been removed...
for (String name : existingNames) {
if (foundNames.contains(name)) {
// known and still there. noop
} else {
// known, but removed. drop it
try {
// Snark.fatal() throws a RuntimeException
// don't let one bad torrent kill the whole loop
stopTorrent(name, true);
} catch (RuntimeException e) {
// don't bother with message
}
}
}
return rv;
}
use of net.i2p.util.FileSuffixFilter in project i2p.i2p by i2p.
the class FileDumpHelper method dumpDir.
private static void dumpDir(StringBuilder buf, File dir, String suffix) {
File[] files = dir.listFiles(new FileSuffixFilter(suffix));
if (files == null)
return;
Arrays.sort(files);
for (int i = 0; i < files.length; i++) {
dumpFile(buf, files[i]);
}
}
Aggregations