Fix windows file renaming issue

This commit is contained in:
Grahame Grieve 2023-06-30 13:42:46 +10:00
parent 99d6060363
commit dfb0e0d5b9
3 changed files with 70 additions and 14 deletions

View File

@ -62,13 +62,18 @@ public class TextFile {
List<String> result = new ArrayList<String>();
File file = new CSFile(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
while( reader.ready() )
result.add(reader.readLine());
reader.close();
return result;
FileInputStream fs = new FileInputStream(file);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(fs,"UTF-8"));
while( reader.ready() )
result.add(reader.readLine());
reader.close();
return result;
} finally {
fs.close();
}
}
public static void writeAllLines(String path, List<String> lines) throws IOException
@ -141,12 +146,22 @@ public class TextFile {
}
public static String fileToString(File f) throws FileNotFoundException, IOException {
return streamToString(new FileInputStream(f));
FileInputStream fs = new FileInputStream(f);
try {
return streamToString(fs);
} finally {
fs.close();
}
}
public static String fileToString(String src) throws FileNotFoundException, IOException {
return streamToString(new FileInputStream(new CSFile(src)));
}
FileInputStream fs = new FileInputStream(new CSFile(src));
try {
return streamToString(fs);
} finally {
fs.close();
}
}
public static String streamToString(InputStream input) throws IOException {
InputStreamReader sr = new InputStreamReader(input, "UTF-8");
@ -205,7 +220,12 @@ public class TextFile {
}
public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException {
return streamToBytes(new FileInputStream(new CSFile(srcFile)));
FileInputStream fs = new FileInputStream(new CSFile(srcFile));
try {
return streamToBytes(fs);
} finally {
fs.close();
}
}
/**
@ -218,11 +238,21 @@ public class TextFile {
* @throws IOException
*/
public static byte[] fileToBytesNCS(String srcFile) throws FileNotFoundException, IOException {
return streamToBytes(new FileInputStream(new File(srcFile)));
FileInputStream fs = new FileInputStream(new File(srcFile));
try {
return streamToBytes(fs);
} finally {
fs.close();
}
}
public static byte[] fileToBytes(File file) throws FileNotFoundException, IOException {
return streamToBytes(new FileInputStream(file));
FileInputStream fs = new FileInputStream(file);
try {
return streamToBytes(fs);
} finally {
fs.close();
}
}
public static String bytesToString(byte[] bs) throws IOException {

View File

@ -2033,4 +2033,30 @@ public class Utilities {
return i == 0 ? "" : s.substring(0, i+1);
}
public static void renameDirectory(String source, String dest) throws FHIRException, IOException {
File src = new File(source);
File dst = new File(dest);
if (!src.renameTo(dst)) {
int i = 0;
do {
try {
Thread.sleep(20);
} catch (Exception e) {
// nothing
}
System.gc();
i++;
} while (!src.renameTo(dst) && i < 10);
if (src.exists()) {
copyDirectory(source, dest, null);
try {
src.delete();
} catch (Exception e) {
// nothing
}
}
}
}
}

View File

@ -453,7 +453,7 @@ public class FilesystemPackageCacheManager extends BasePackageCacheManager imple
} catch (Throwable t) {
log("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later");
}
new File(tempDir).renameTo(new File(packRoot));
Utilities.renameDirectory(tempDir, packRoot);
IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.setTimeStampFormat("yyyyMMddhhmmss");