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