From 57b6027431452ffce40725043a9a82a63ca5670a Mon Sep 17 00:00:00 2001 From: Lloyd McKenzie Date: Wed, 20 Nov 2019 23:27:01 +0100 Subject: [PATCH] Enable packages containing instructions to append files --- .../java/org/hl7/fhir/utilities/TextFile.java | 12 ++++++++++-- .../org/hl7/fhir/utilities/cache/NpmPackage.java | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java index 05091e8fa..6a3fa47bd 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TextFile.java @@ -61,6 +61,9 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.List; @@ -194,8 +197,13 @@ public class TextFile { OutputStream sw = new FileOutputStream(file); sw.write(bytes); sw.flush(); - sw.close(); - + sw.close(); + } + + public static void appendBytesToFile(byte[] bytes, String path) throws IOException { + byte[] linebreak = new byte[] {13, 10}; + Files.write(Paths.get(path), linebreak, StandardOpenOption.APPEND); + Files.write(Paths.get(path), bytes, StandardOpenOption.APPEND); } public static byte[] fileToBytes(String srcFile) throws FileNotFoundException, IOException { diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java index 128414c34..fcbd3847f 100644 --- a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/cache/NpmPackage.java @@ -498,10 +498,26 @@ public class NpmPackage { } public void unPack(String dir) throws IOException { + unPack (dir, false); + } + + public void unPackWithAppend(String dir) throws IOException { + unPack (dir, true); + } + + public void unPack(String dir, boolean withAppend) throws IOException { for (String s : content.keySet()) { String fn = Utilities.path(dir, s); String dn = Utilities.getDirectoryForFile(fn); Utilities.createDirectory(dn); + File f = new File(s); + if (withAppend && f.getName().startsWith("_append.")) { + String appendFn = Utilities.path(dir, Utilities.getDirectoryForFile(s), f.getName().substring(8)); + if (new File(appendFn).exists()) + TextFile.appendBytesToFile(content.get(s), appendFn); + else + TextFile.bytesToFile(content.get(s), appendFn); + } else TextFile.bytesToFile(content.get(s), fn); } if (path != null)