Search in sources :

Example 1 with FixCRLFOutputStream

use of i2p.susi.util.FixCRLFOutputStream in project i2p.i2p by i2p.

the class PersistentMailCache method importMail.

/**
 *  For debugging. Import .eml files from the import/ directory
 *  @since 0.9.34
 */
private void importMail() {
    File importDir = new File(_cacheDir.getParentFile(), DIR_IMPORT);
    if (importDir.exists() && importDir.isDirectory()) {
        File[] files = importDir.listFiles(new FileSuffixFilter(".eml"));
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            File f = files[i];
            // Read in the headers to get the X-UIDL that Thunderbird stuck in there
            String uidl = Long.toString(_context.random().nextLong());
            InputStream in = null;
            try {
                in = new FileInputStream(f);
                for (int j = 0; j < 20; j++) {
                    String line = DataHelper.readLine(in);
                    if (line.length() < 2)
                        break;
                    if (line.startsWith("X-UIDL:")) {
                        uidl = line.substring(7).trim();
                        break;
                    }
                }
            } catch (IOException ioe) {
                Debug.debug(Debug.ERROR, "Import failed " + f, ioe);
                continue;
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ioe) {
                    }
            }
            if (uidl == null)
                uidl = Long.toString(_context.random().nextLong());
            File to = getFullFile(uidl);
            if (to.exists()) {
                Debug.debug(Debug.DEBUG, "Already have " + f + " as UIDL " + uidl);
                f.delete();
                continue;
            }
            in = null;
            OutputStream out = null;
            try {
                in = new FileInputStream(f);
                GzipFileBuffer gb = new GzipFileBuffer(to);
                // Thunderbird exports aren't CRLF terminated
                out = new FixCRLFOutputStream(gb.getOutputStream());
                DataHelper.copy(in, out);
            } catch (IOException ioe) {
                Debug.debug(Debug.ERROR, "Import failed " + f, ioe);
                continue;
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ioe) {
                    }
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException ioe) {
                    }
            }
            f.delete();
            Debug.debug(Debug.DEBUG, "Imported " + f + " as UIDL " + uidl);
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FixCRLFOutputStream(i2p.susi.util.FixCRLFOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileSuffixFilter(net.i2p.util.FileSuffixFilter) IOException(java.io.IOException) GzipFileBuffer(i2p.susi.util.GzipFileBuffer) FixCRLFOutputStream(i2p.susi.util.FixCRLFOutputStream) SecureFile(net.i2p.util.SecureFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

FixCRLFOutputStream (i2p.susi.util.FixCRLFOutputStream)1 GzipFileBuffer (i2p.susi.util.GzipFileBuffer)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 FileSuffixFilter (net.i2p.util.FileSuffixFilter)1 SecureFile (net.i2p.util.SecureFile)1 SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)1