fix bugs in TerminologyCacheManager

This commit is contained in:
Grahame Grieve 2021-11-10 17:05:32 +11:00
parent 1e69909466
commit 78eeba7045
2 changed files with 19 additions and 3 deletions

View File

@ -104,7 +104,7 @@ public class TerminologyCacheManager {
Utilities.createDirectory(path);
} else {
Utilities.createDirectory(Utilities.getDirectoryForFile(path));
TextFile.streamToFile(zipIn, path);
TextFile.streamToFileNoClose(zipIn, path);
}
}
}
@ -154,6 +154,8 @@ public class TerminologyCacheManager {
String url = "https://tx.fhir.org/post/tx-cache/"+ghOrg+"/"+ghRepo+"/"+ghBranch+".zip";
System.out.println("Sending tx-cache to "+url+" ("+Utilities.describeSize(bs.toByteArray().length)+")");
SimpleHTTPClient http = new SimpleHTTPClient();
http.setUsername(token.substring(0, token.indexOf(':')));
http.setPassword(token.substring(token.indexOf(':')+1));
HTTPResult res = http.put(url, "application/zip", bs.toByteArray(), null); // accept doesn't matter
if (res.getCode() >= 300) {
System.out.println("sending cache failed: "+res.getCode());

View File

@ -166,14 +166,23 @@ public class TextFile {
if (input== null) {
return null;
}
// Define a size if you have an idea of it.
ByteArrayOutputStream r = new ByteArrayOutputStream(2048);
byte[] read = new byte[512]; // Your buffer size.
byte[] read = new byte[512];
for (int i; -1 != (i = input.read(read)); r.write(read, 0, i));
input.close();
return r.toByteArray();
}
public static byte[] streamToBytesNoClose(InputStream input) throws IOException {
if (input== null) {
return null;
}
ByteArrayOutputStream r = new ByteArrayOutputStream(2048);
byte[] read = new byte[512];
for (int i; -1 != (i = input.read(read)); r.write(read, 0, i));
return r.toByteArray();
}
public static void bytesToFile(byte[] bytes, String path) throws IOException {
File file = new CSFile(path);
OutputStream sw = new FileOutputStream(file);
@ -231,4 +240,9 @@ public class TextFile {
byte[] cnt = streamToBytes(stream);
bytesToFile(cnt, filename);
}
public static void streamToFileNoClose(InputStream stream, String filename) throws IOException {
byte[] cnt = streamToBytesNoClose(stream);
bytesToFile(cnt, filename);
}
}