Search in sources :

Example 51 with SecureFileOutputStream

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

the class EmailFolder method add.

/**
 * Stores an email in the folder. If an email with the same
 * message ID exists already, nothing happens.
 * @param email
 * @throws IOException
 * @throws MessagingException
 * @throws PasswordException
 * @throws GeneralSecurityException
 */
public void add(Email email) throws IOException, MessagingException, PasswordException, GeneralSecurityException {
    // check if an email exists already with that message id
    if (getEmailFile(email.getMessageID()).exists()) {
        log.debug("Not storing email because there is an existing one with the same message ID: <" + email.getMessageID() + ">");
        return;
    }
    // write out the email file
    File emailFile = getEmailFile(email);
    log.info("Mail folder <" + storageDir + ">: storing email file: <" + emailFile.getAbsolutePath() + ">");
    OutputStream emailOutputStream = new BufferedOutputStream(new EncryptedOutputStream(new SecureFileOutputStream(emailFile), passwordHolder));
    try {
        email.writeTo(emailOutputStream);
    } finally {
        emailOutputStream.close();
    }
    saveMetadata(email);
    for (FolderListener listener : folderListeners) listener.elementAdded(email.getMessageID());
}
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) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) EncryptedOutputStream(i2p.bote.fileencryption.EncryptedOutputStream)

Example 52 with SecureFileOutputStream

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

the class MessageIdCache method write.

private void write(File cacheFile) {
    log.debug("Writing message ID cache file: <" + cacheFile.getAbsolutePath() + ">");
    String newLine = System.getProperty("line.separator");
    Writer writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(cacheFile.getAbsolutePath())));
        for (UniqueId id : idList) writer.write(id.toBase64() + newLine);
    } catch (IOException e) {
        log.error("Can't write message ID cache file.", e);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e) {
                log.error("Error closing Writer.", e);
            }
    }
}
Also used : UniqueId(i2p.bote.UniqueId) OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter)

Example 53 with SecureFileOutputStream

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

the class PacketFolder method add.

/**
 * Saves a packet to a file in the folder. If the file already exists, it is overwritten.
 * @param packetToStore
 * @param filename The filename to store the packet under, relative to this folder's storage directory.
 */
protected void add(I2PBotePacket packetToStore, String filename) {
    FileOutputStream outputStream = null;
    File file = new File(storageDir, filename);
    try {
        outputStream = new SecureFileOutputStream(file);
        packetToStore.writeTo(outputStream);
    } catch (Exception e) {
        log.error("Can't save packet to file: <" + filename + ">", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                log.error("Can't close file: <" + filename + ">", e);
            }
            if (file.length() == 0) {
                log.error("Nothing was written, deleting empty file: <" + file.getAbsolutePath() + ">");
                file.delete();
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File) MalformedPacketException(i2p.bote.packet.MalformedPacketException) PasswordException(i2p.bote.fileencryption.PasswordException) IOException(java.io.IOException)

Example 54 with SecureFileOutputStream

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

the class Identities method save.

/**
 * Saves all identities to file.
 */
public void save() throws IOException, GeneralSecurityException, PasswordException {
    initializeIfNeeded();
    OutputStream encryptedStream = new EncryptedOutputStream(new SecureFileOutputStream(identitiesFile), passwordHolder);
    try {
        Properties properties = saveToProperties();
        properties.store(new OutputStreamWriter(encryptedStream, "UTF-8"), null);
    } catch (IOException e) {
        log.error("Can't save email identities to file <" + identitiesFile.getAbsolutePath() + ">.", e);
        throw e;
    } finally {
        encryptedStream.close();
    }
}
Also used : EncryptedOutputStream(i2p.bote.fileencryption.EncryptedOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Properties(java.util.Properties) SortedProperties(i2p.bote.util.SortedProperties) EncryptedOutputStream(i2p.bote.fileencryption.EncryptedOutputStream)

Example 55 with SecureFileOutputStream

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

the class RelayPeerManager method writePeers.

private void writePeers(File file) {
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(file.getAbsolutePath())));
        writer.write("# Each line is in the format: <dest> [resp1] [resp2] ...");
        writer.newLine();
        writer.write("#   dest  = the I2P destination");
        writer.newLine();
        writer.write("#   resp* = true or false, depending on whether the peer responded");
        writer.newLine();
        writer.write("# The fields are separated by a tab character.");
        writer.newLine();
        writer.write("# Lines starting with a # are ignored.");
        writer.newLine();
        writer.write("# Do not edit this file while I2P-Bote is running as it will be overwritten.");
        writer.newLine();
        for (RelayPeer peer : peers) {
            writer.write(peer.toBase64());
            for (boolean responded : peer.getAllSamples()) writer.write("\t" + responded);
            writer.newLine();
        }
    } catch (IOException e) {
        log.error("Can't write peers to file <" + file.getAbsolutePath() + ">", e);
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException e) {
                log.error("Can't close BufferedWriter for file <" + file.getAbsolutePath() + ">", e);
            }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) RelayPeer(i2p.bote.network.RelayPeer) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

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