Search in sources :

Example 11 with SecureFileOutputStream

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

the class SingleFileNamingService method remove.

/**
 *  @param hostname case-sensitive; caller should convert to lower case
 *  @param options ignored
 */
@Override
public boolean remove(String hostname, Properties options) {
    BufferedReader in = null;
    BufferedWriter out = null;
    if (!getWriteLock())
        return false;
    try {
        if (!_file.exists())
            return false;
        if (_isClosed)
            return false;
        in = new BufferedReader(new InputStreamReader(new FileInputStream(_file), "UTF-8"), 16 * 1024);
        File tmp = SecureFile.createTempFile("temp-", ".tmp", _file.getAbsoluteFile().getParentFile());
        out = new BufferedWriter(new OutputStreamWriter(new SecureFileOutputStream(tmp), "UTF-8"));
        String line = null;
        String search = hostname + '=';
        boolean success = false;
        while ((line = in.readLine()) != null) {
            if (line.startsWith(search)) {
                success = true;
                continue;
            }
            out.write(line);
            out.newLine();
        }
        in.close();
        out.close();
        if (!success) {
            tmp.delete();
            return false;
        }
        success = FileUtil.rename(tmp, _file);
        if (success) {
            for (NamingServiceListener nsl : _listeners) {
                nsl.entryRemoved(this, hostname);
            }
        }
        return success;
    } catch (IOException ioe) {
        _log.error("Error removing " + hostname, ioe);
        return false;
    } finally {
        if (in != null)
            try {
                in.close();
            } catch (IOException e) {
            }
        if (out != null)
            try {
                out.close();
            } catch (IOException e) {
            }
        releaseWriteLock();
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) SecureFile(net.i2p.util.SecureFile) File(java.io.File) FileInputStream(java.io.FileInputStream) BufferedWriter(java.io.BufferedWriter)

Example 12 with SecureFileOutputStream

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

the class AddressbookBean method save.

private void save() throws IOException {
    String filename = properties.getProperty(getBook() + "_addressbook");
    FileOutputStream fos = null;
    try {
        fos = new SecureFileOutputStream(new File(addressbookDir(), filename));
        addressbook.store(fos, null);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ioe) {
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 13 with SecureFileOutputStream

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

the class DataHelper method storeProps.

/**
 * Writes the props to the file, unsorted (unless props is an OrderedProperties)
 * Note that this does not escape the \r or \n that are unescaped in loadProps() above.
 * As of 0.8.1, file will be mode 600.
 *
 * Properties from the defaults table of props (if any) are not written out by this method.
 *
 * Leading or trailing whitespace in values is not checked but
 * will be trimmed by loadProps()
 *
 * @throws IllegalArgumentException if a key contains any of "#=\n" or starts with ';',
 *                                  or a value contains '#' or '\n'
 */
public static void storeProps(Properties props, File file) throws IOException {
    FileOutputStream fos = null;
    PrintWriter out = null;
    IllegalArgumentException iae = null;
    File tmpFile = new File(file.getPath() + ".tmp");
    try {
        fos = new SecureFileOutputStream(tmpFile);
        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")));
        out.println("# NOTE: This I2P config file must use UTF-8 encoding");
        for (Map.Entry<Object, Object> entry : props.entrySet()) {
            String name = (String) entry.getKey();
            String val = (String) entry.getValue();
            if (ILLEGAL_KEY.matcher(name).matches()) {
                if (iae == null)
                    iae = new IllegalArgumentException("Invalid character (one of \"#;=\\r\\n\") in key: \"" + name + "\" = \"" + val + '\"');
                continue;
            }
            if (ILLEGAL_VALUE.matcher(val).matches()) {
                if (iae == null)
                    iae = new IllegalArgumentException("Invalid character (one of \"#\\r\\n\") in value: \"" + name + "\" = \"" + val + '\"');
                continue;
            }
            out.println(name + "=" + val);
        }
        if (SHOULD_SYNC) {
            out.flush();
            fos.getFD().sync();
        }
        out.close();
        if (out.checkError()) {
            out = null;
            tmpFile.delete();
            throw new IOException("Failed to write properties to " + tmpFile);
        }
        out = null;
        if (!FileUtil.rename(tmpFile, file))
            throw new IOException("Failed rename from " + tmpFile + " to " + file);
    } finally {
        if (out != null)
            out.close();
        if (fos != null)
            try {
                fos.close();
            } catch (IOException ioe) {
            }
    }
    if (iae != null)
        throw iae;
}
Also used : SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) FileOutputStream(java.io.FileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 14 with SecureFileOutputStream

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

the class PrivateKeyFile method write.

/**
 *  Copied from I2PClientImpl.createDestination()
 */
public void write() throws IOException, DataFormatException {
    OutputStream out = null;
    try {
        out = new SecureFileOutputStream(this.file);
        this.dest.writeBytes(out);
        this.privKey.writeBytes(out);
        this.signingPrivKey.writeBytes(out);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ioe) {
            }
        }
    }
}
Also used : SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) OutputStream(java.io.OutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException)

Example 15 with SecureFileOutputStream

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

the class EventLog method addEvent.

/**
 *  Append an event. Fails silently.
 *  @param event no spaces or newlines, e.g. "started"
 *  @param info no newlines, may be blank or null
 *  @throws IllegalArgumentException if event contains a space or either contains a newline
 */
public synchronized void addEvent(String event, String info) {
    if (event.contains(" ") || event.contains("\n") || (info != null && info.contains("\n")))
        throw new IllegalArgumentException();
    _cache.remove(event);
    _cacheTime.remove(event);
    OutputStream out = null;
    try {
        out = new SecureFileOutputStream(_file, true);
        StringBuilder buf = new StringBuilder(128);
        buf.append(_context.clock().now()).append(' ').append(event);
        if (info != null && info.length() > 0)
            buf.append(' ').append(info);
        if (SystemVersion.isWindows())
            buf.append('\r');
        buf.append('\n');
        out.write(buf.toString().getBytes("UTF-8"));
    } catch (IOException ioe) {
    } finally {
        if (out != null)
            try {
                out.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : OutputStream(java.io.OutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) SecureFileOutputStream(net.i2p.util.SecureFileOutputStream) IOException(java.io.IOException)

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