fix problem with temp file on win8

This commit is contained in:
Manfred Karrer 2014-07-09 14:44:58 +02:00
parent 8509c60485
commit 6395c4f92f
2 changed files with 60 additions and 29 deletions

View File

@ -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);
tempFile = FileUtil.getTempFile(prefix);
// 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);
objectOutputStream.writeObject(serializable);
// 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("saveObjectToFile failed." + e);
if (tempFile.exists())
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())
if (!tempFile.delete()) log.error("Cannot delete temp file.");
}
try
{
log.warn("Cannot delete temp file.");
}
}
}
if (objectOutputStream != null) objectOutputStream.close();
if (fileOutputStream != null) fileOutputStream.close();
} catch (IOException e)
{
e.printStackTrace();
log.error("getTempFile failed." + e);
log.error("Cannot close resources.");
}
}
}

View File

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