Search in sources :

Example 46 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.

the class ConfigParser method write.

/**
 * Write contents of Map map to the File file. Output is written
 * with one key, value pair on each line, in the format: key=value.
 * Write to a temp file in the same directory and then rename, to not corrupt
 * simultaneous accesses by the router. Except on Windows where renameTo()
 * will fail if the target exists.
 *
 * @param map
 *            A Map to write to file.
 * @param file
 *            A File to write the Map to.
 * @throws IOException
 *             if file cannot be written to.
 */
public static void write(Map<String, String> map, File file) throws IOException {
    boolean success = false;
    if (!isWindows) {
        File tmp = SecureFile.createTempFile("temp-", ".tmp", file.getAbsoluteFile().getParentFile());
        write(map, new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(tmp), "UTF-8")));
        success = tmp.renameTo(file);
        if (!success) {
            tmp.delete();
        // System.out.println("Warning: addressbook rename fail from " + tmp + " to " + file);
        }
    }
    if (!success) {
        // hmm, that didn't work, try it the old way
        write(map, new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file), "UTF-8")));
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFile(net.i2p.util.SecureFile) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 47 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.

the class CertUtil method saveCert.

/**
 *  Write a certificate to a file in base64 format.
 *
 *  @return success
 *  @since 0.8.2, moved from SSLEepGet in 0.9.9
 */
public static boolean saveCert(Certificate cert, File file) {
    OutputStream os = null;
    try {
        os = new SecureFileOutputStream(file);
        exportCert(cert, os);
        return true;
    } catch (CertificateEncodingException cee) {
        error("Error writing X509 Certificate " + file.getAbsolutePath(), cee);
        return false;
    } catch (IOException ioe) {
        error("Error writing X509 Certificate " + file.getAbsolutePath(), ioe);
        return false;
    } finally {
        try {
            if (os != null)
                os.close();
        } catch (IOException foo) {
        }
    }
}
Also used : SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStream(java.io.OutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) CertificateEncodingException(java.security.cert.CertificateEncodingException) IOException(java.io.IOException)

Example 48 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.

the class SingleFileNamingService method putIfAbsent.

/**
 *  @param hostname case-sensitive; caller should convert to lower case
 *  @param options if non-null, any prefixed with '=' will be appended
 *                 in subscription format
 */
@Override
public boolean putIfAbsent(String hostname, Destination d, Properties options) {
    BufferedWriter out = null;
    if (!getWriteLock())
        return false;
    try {
        if (_isClosed)
            return false;
        // simply check if present, and if not, append
        try {
            if (getKey(hostname) != null)
                return false;
        } catch (IOException ioe) {
            if (_file.exists()) {
                _log.error("Error adding " + hostname, ioe);
                return false;
            }
        // else new file
        }
        out = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(_file, true), "UTF-8"));
        // FIXME fails if previous last line didn't have a trailing \n
        out.write(hostname);
        out.write('=');
        out.write(d.toBase64());
        // subscription options
        if (options != null)
            writeOptions(options, out);
        out.write('\n');
        for (NamingServiceListener nsl : _listeners) {
            nsl.entryAdded(this, hostname, d, options);
        }
        return true;
    } catch (IOException ioe) {
        _log.error("Error adding " + hostname, ioe);
        return false;
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
            }
        releaseWriteLock();
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 49 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p by i2p.

the class PersistentDataStore method write.

private void write(Hash key, DatabaseEntry data) {
    if (_log.shouldLog(Log.INFO))
        _log.info("Writing key " + key);
    OutputStream fos = null;
    File dbFile = null;
    try {
        String filename = null;
        if (data.getType() == DatabaseEntry.KEY_TYPE_LEASESET)
            filename = getLeaseSetName(key);
        else if (data.getType() == DatabaseEntry.KEY_TYPE_ROUTERINFO)
            filename = getRouterInfoName(key);
        else
            throw new IOException("We don't know how to write objects of type " + data.getClass().getName());
        dbFile = new File(_dbDir, filename);
        long dataPublishDate = getPublishDate(data);
        if (dbFile.lastModified() < dataPublishDate) {
            // our filesystem is out of date, lets replace it
            fos = new SecureFileOutputStream(dbFile);
            fos = new BufferedOutputStream(fos);
            try {
                data.writeBytes(fos);
                fos.close();
                dbFile.setLastModified(dataPublishDate);
            } catch (DataFormatException dfe) {
                _log.error("Error writing out malformed object as " + key + ": " + data, dfe);
                dbFile.delete();
            }
        } else {
            // we've already written the file, no need to waste our time
            if (_log.shouldLog(Log.DEBUG))
                _log.debug("Not writing " + key.toBase64() + ", as its up to date on disk (file mod-publish=" + (dbFile.lastModified() - dataPublishDate) + ")");
        }
    } catch (IOException ioe) {
        _log.error("Error writing out the object", ioe);
    } finally {
        if (fos != null)
            try {
                fos.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : DataFormatException(net.i2p.data.DataFormatException) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 50 with SecureFileOutputStream

use of net.i2p.util.SecureFileOutputStream in project i2p.i2p-bote by i2p.

the class EmailFolder method saveMetadata.

private void saveMetadata(EmailMetadata metadata, File file) throws PasswordException, FileNotFoundException, IOException, GeneralSecurityException {
    log.info("Mail folder <" + storageDir + ">: storing metadata file: <" + file.getAbsolutePath() + ">");
    OutputStream emailOutputStream = new BufferedOutputStream(new EncryptedOutputStream(new SecureFileOutputStream(file), passwordHolder));
    try {
        metadata.writeTo(emailOutputStream);
    } catch (IOException e) {
        log.error("Can't write metadata to file <" + file.getAbsolutePath() + ">", e);
        throw e;
    } finally {
        if (emailOutputStream != null)
            emailOutputStream.close();
    }
    for (FolderListener listener : folderListeners) listener.elementUpdated();
}
Also used : EncryptedOutputStream(i2p.bote.fileencryption.EncryptedOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream) EncryptedOutputStream(i2p.bote.fileencryption.EncryptedOutputStream)

Aggregations

SecureFileOutputStream (net.i2p.util.SecureFileOutputStream)55 IOException (java.io.IOException)50 File (java.io.File)33 OutputStream (java.io.OutputStream)22 OutputStreamWriter (java.io.OutputStreamWriter)21 FileOutputStream (java.io.FileOutputStream)19 BufferedWriter (java.io.BufferedWriter)16 FileInputStream (java.io.FileInputStream)11 SecureFile (net.i2p.util.SecureFile)9 BufferedOutputStream (java.io.BufferedOutputStream)8 PrintWriter (java.io.PrintWriter)7 InputStream (java.io.InputStream)6 GeneralSecurityException (java.security.GeneralSecurityException)6 DataFormatException (net.i2p.data.DataFormatException)6 EncryptedOutputStream (i2p.bote.fileencryption.EncryptedOutputStream)4 KeyStore (java.security.KeyStore)4 X509Certificate (java.security.cert.X509Certificate)4 Properties (java.util.Properties)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Writer (java.io.Writer)3