Search in sources :

Example 6 with FileSuffixFilter

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;
}
Also used : ParserException(org.cybergarage.xml.ParserException) Log(net.i2p.util.Log) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(org.cybergarage.xml.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) FileSuffixFilter(net.i2p.util.FileSuffixFilter) File(java.io.File)

Example 7 with FileSuffixFilter

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;
}
Also used : ArrayList(java.util.ArrayList) FileSuffixFilter(net.i2p.util.FileSuffixFilter) IOException(java.io.IOException) File(java.io.File)

Example 8 with FileSuffixFilter

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]);
    }
}
Also used : FileSuffixFilter(net.i2p.util.FileSuffixFilter) File(java.io.File)

Aggregations

File (java.io.File)8 FileSuffixFilter (net.i2p.util.FileSuffixFilter)8 IOException (java.io.IOException)6 BufferedInputStream (java.io.BufferedInputStream)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 GZIPInputStream (java.util.zip.GZIPInputStream)2 Log (net.i2p.util.Log)2 FixCRLFOutputStream (i2p.susi.util.FixCRLFOutputStream)1 GzipFileBuffer (i2p.susi.util.GzipFileBuffer)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 GeneralSecurityException (java.security.GeneralSecurityException)1 X509CRL (java.security.cert.X509CRL)1