mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 18:03:12 +01:00
fix problem with temp file on win8
This commit is contained in:
parent
8509c60485
commit
6395c4f92f
@ -218,41 +218,51 @@ public class Storage
|
||||
|
||||
private void saveObjectToFile(Serializable serializable)
|
||||
{
|
||||
File tempFile = null;
|
||||
FileOutputStream fileOutputStream = null;
|
||||
ObjectOutputStream objectOutputStream = null;
|
||||
try
|
||||
{
|
||||
final File tempFile = FileUtil.getTempFile("temp_" + 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);
|
||||
tempFile = FileUtil.getTempFile(prefix);
|
||||
|
||||
// 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.
|
||||
fileOutputStream.flush();
|
||||
fileOutputStream.getFD().sync();
|
||||
fileOutputStream.close();
|
||||
// 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
|
||||
// manually before replacing file with temp file
|
||||
fileOutputStream = new FileOutputStream(tempFile);
|
||||
objectOutputStream = new ObjectOutputStream(fileOutputStream);
|
||||
|
||||
FileUtil.writeTempFileToFile(tempFile, storageFile);
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
log.error("saveObjectToFile failed." + e);
|
||||
objectOutputStream.writeObject(serializable);
|
||||
|
||||
if (tempFile.exists())
|
||||
{
|
||||
log.warn("Temp file still exists after failed save.");
|
||||
if (!tempFile.delete())
|
||||
{
|
||||
log.warn("Cannot delete temp file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
// 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.
|
||||
fileOutputStream.flush();
|
||||
fileOutputStream.getFD().sync();
|
||||
|
||||
// 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)
|
||||
{
|
||||
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();
|
||||
final File pubKeyTempFile = FileUtil.getTempFile("pubKey_temp_" + prefix);
|
||||
final File privKeyTempFile = FileUtil.getTempFile("privKey_temp_" + prefix);
|
||||
try (final FileOutputStream pubKeyFileOutputStream = new FileOutputStream(pubKeyTempFile);
|
||||
final FileOutputStream privKeyFileOutputStream = new FileOutputStream(privKeyTempFile))
|
||||
final FileOutputStream pubKeyFileOutputStream = new FileOutputStream(pubKeyTempFile);
|
||||
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 X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
|
||||
@ -113,9 +116,17 @@ public class DSAKeyUtil
|
||||
privKeyFileOutputStream.flush();
|
||||
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(privKeyTempFile, privKeyFile);
|
||||
} catch (IOException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
log.error("saveKeyPairToFiles failed." + e);
|
||||
|
||||
} finally
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user