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();
}
}
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) {
}
}
}
}
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;
}
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) {
}
}
}
}
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) {
}
}
}
Aggregations