Search in sources :

Example 1 with GzipFileBuffer

use of i2p.susi.util.GzipFileBuffer 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)

Example 2 with GzipFileBuffer

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

the class PersistentMailCache method write.

/**
 * Save data to disk.
 *
 * @return success
 */
private static boolean write(Buffer rb, File f) {
    InputStream in = null;
    OutputStream out = null;
    try {
        in = rb.getInputStream();
        GzipFileBuffer gb = new GzipFileBuffer(f);
        out = gb.getOutputStream();
        DataHelper.copy(in, out);
        return true;
    } catch (IOException ioe) {
        Debug.debug(Debug.ERROR, "Error writing: " + f + ": " + ioe);
        return false;
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException ioe) {
            }
        if (out != null)
            try {
                out.close();
            } catch (IOException ioe) {
            }
    }
}
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) IOException(java.io.IOException) GzipFileBuffer(i2p.susi.util.GzipFileBuffer)

Aggregations

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