mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-08-24 22:39:23 -04:00
fix problem with temp file on win8
This commit is contained in:
parent
8509c60485
commit
6395c4f92f
2 changed files with 60 additions and 29 deletions
|
@ -218,41 +218,51 @@ public class Storage
|
||||||
|
|
||||||
private void saveObjectToFile(Serializable serializable)
|
private void saveObjectToFile(Serializable serializable)
|
||||||
{
|
{
|
||||||
|
File tempFile = null;
|
||||||
|
FileOutputStream fileOutputStream = null;
|
||||||
|
ObjectOutputStream objectOutputStream = null;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
final File tempFile = FileUtil.getTempFile("temp_" + prefix);
|
tempFile = FileUtil.getTempFile(prefix);
|
||||||
try (final FileOutputStream fileOutputStream = new FileOutputStream(tempFile))
|
|
||||||
{
|
|
||||||
// don't use closeable resource in try for the ObjectOutputStream as it produces problems on Windows 8
|
|
||||||
// -> rename of temp file fails
|
|
||||||
final ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
|
||||||
objectOutputStream.writeObject(serializable);
|
|
||||||
|
|
||||||
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide
|
// Don't use auto closeable resources in try() as we would need too many try/catch clauses (for tempFile) and we need to close it
|
||||||
// to not write through to physical media for at least a few seconds, but this is the best we can do.
|
// manually before replacing file with temp file
|
||||||
fileOutputStream.flush();
|
fileOutputStream = new FileOutputStream(tempFile);
|
||||||
fileOutputStream.getFD().sync();
|
objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||||
fileOutputStream.close();
|
|
||||||
|
|
||||||
FileUtil.writeTempFileToFile(tempFile, storageFile);
|
objectOutputStream.writeObject(serializable);
|
||||||
} catch (IOException e)
|
|
||||||
{
|
|
||||||
e.printStackTrace();
|
|
||||||
log.error("saveObjectToFile failed." + e);
|
|
||||||
|
|
||||||
if (tempFile.exists())
|
// Attempt to force the bits to hit the disk. In reality the OS or hard disk itself may still decide
|
||||||
{
|
// to not write through to physical media for at least a few seconds, but this is the best we can do.
|
||||||
log.warn("Temp file still exists after failed save.");
|
fileOutputStream.flush();
|
||||||
if (!tempFile.delete())
|
fileOutputStream.getFD().sync();
|
||||||
{
|
|
||||||
log.warn("Cannot delete temp file.");
|
// Close resources before replacing file with temp file because otherwise it causes problems on windows when rename temp file
|
||||||
}
|
fileOutputStream.close();
|
||||||
}
|
objectOutputStream.close();
|
||||||
}
|
|
||||||
|
FileUtil.writeTempFileToFile(tempFile, storageFile);
|
||||||
} catch (IOException e)
|
} catch (IOException e)
|
||||||
{
|
{
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
log.error("getTempFile failed." + e);
|
log.error("save object to file failed." + e);
|
||||||
|
} finally
|
||||||
|
{
|
||||||
|
if (tempFile != null && tempFile.exists())
|
||||||
|
{
|
||||||
|
log.warn("Temp file still exists after failed save.");
|
||||||
|
if (!tempFile.delete()) log.error("Cannot delete temp file.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (objectOutputStream != null) objectOutputStream.close();
|
||||||
|
if (fileOutputStream != null) fileOutputStream.close();
|
||||||
|
} catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
log.error("Cannot close resources.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -97,9 +97,12 @@ public class DSAKeyUtil
|
||||||
lock.lock();
|
lock.lock();
|
||||||
final File pubKeyTempFile = FileUtil.getTempFile("pubKey_temp_" + prefix);
|
final File pubKeyTempFile = FileUtil.getTempFile("pubKey_temp_" + prefix);
|
||||||
final File privKeyTempFile = FileUtil.getTempFile("privKey_temp_" + prefix);
|
final File privKeyTempFile = FileUtil.getTempFile("privKey_temp_" + prefix);
|
||||||
try (final FileOutputStream pubKeyFileOutputStream = new FileOutputStream(pubKeyTempFile);
|
final FileOutputStream pubKeyFileOutputStream = new FileOutputStream(pubKeyTempFile);
|
||||||
final FileOutputStream privKeyFileOutputStream = new FileOutputStream(privKeyTempFile))
|
final FileOutputStream privKeyFileOutputStream = new FileOutputStream(privKeyTempFile);
|
||||||
|
try
|
||||||
{
|
{
|
||||||
|
// Don't use auto closeable resources in try() as we need to close it
|
||||||
|
// manually before replacing file with temp file anyway
|
||||||
|
|
||||||
final PublicKey publicKey = keyPair.getPublic();
|
final PublicKey publicKey = keyPair.getPublic();
|
||||||
final X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
|
final X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
|
||||||
|
@ -113,9 +116,17 @@ public class DSAKeyUtil
|
||||||
privKeyFileOutputStream.flush();
|
privKeyFileOutputStream.flush();
|
||||||
privKeyFileOutputStream.getFD().sync();
|
privKeyFileOutputStream.getFD().sync();
|
||||||
|
|
||||||
|
// Close resources before replacing file with temp file because otherwise it causes problems on windows when rename temp file
|
||||||
|
pubKeyFileOutputStream.close();
|
||||||
|
privKeyFileOutputStream.close();
|
||||||
|
|
||||||
FileUtil.writeTempFileToFile(pubKeyTempFile, pubKeyFile);
|
FileUtil.writeTempFileToFile(pubKeyTempFile, pubKeyFile);
|
||||||
FileUtil.writeTempFileToFile(privKeyTempFile, privKeyFile);
|
FileUtil.writeTempFileToFile(privKeyTempFile, privKeyFile);
|
||||||
|
} catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
log.error("saveKeyPairToFiles failed." + e);
|
||||||
|
|
||||||
} finally
|
} finally
|
||||||
{
|
{
|
||||||
if (pubKeyTempFile.exists())
|
if (pubKeyTempFile.exists())
|
||||||
|
@ -135,6 +146,16 @@ public class DSAKeyUtil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
pubKeyFileOutputStream.close();
|
||||||
|
privKeyFileOutputStream.close();
|
||||||
|
} catch (IOException e)
|
||||||
|
{
|
||||||
|
e.printStackTrace();
|
||||||
|
log.error("Cannot close resources.");
|
||||||
|
}
|
||||||
|
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue