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>(); List<String> result = new ArrayList<String>();
File file = new CSFile(path); File file = new CSFile(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8")); FileInputStream fs = new FileInputStream(file);
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(fs,"UTF-8"));
while( reader.ready() ) while( reader.ready() )
result.add(reader.readLine()); result.add(reader.readLine());
reader.close(); reader.close();
return result; return result;
} finally {
fs.close();
}
} }
public static void writeAllLines(String path, List<String> lines) throws IOException public static void writeAllLines(String path, List<String> lines) throws IOException
@ -141,11 +146,21 @@ public class TextFile {
} }
public static String fileToString(File f) throws FileNotFoundException, IOException { 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 { 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 { public static String streamToString(InputStream input) throws IOException {
@ -205,7 +220,12 @@ public class TextFile {
} }
public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException { 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 * @throws IOException
*/ */
public static byte[] fileToBytesNCS(String srcFile) throws FileNotFoundException, 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 { 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 { 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); 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) { } catch (Throwable t) {
log("Unable to clear directory: "+packRoot+": "+t.getMessage()+" - this may cause problems later"); 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")); IniFile ini = new IniFile(Utilities.path(cacheFolder, "packages.ini"));
ini.setTimeStampFormat("yyyyMMddhhmmss"); ini.setTimeStampFormat("yyyyMMddhhmmss");