From cfed736cd68213a800bcfe9a7a7fba5993105d9e Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 20 Aug 2024 11:14:54 -0400 Subject: [PATCH] Add TRUNCATE_EXISTING and WRITE open options (fix overwrite behavior) --- .../java/org/hl7/fhir/utilities/TextFile.java | 10 +++---- .../org/hl7/fhir/utilities/TextFileTest.java | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 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 f9b3d3ea4..fe137ea79 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 @@ -69,7 +69,7 @@ public class TextFile { public static void writeAllLines(String path, List lines) throws IOException { final File file = ManagedFileAccess.csfile(path); - Files.write(file.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.SYNC); + Files.write(file.toPath(), lines, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.SYNC, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); } public static void stringToStream(final String content, final OutputStream stream) throws IOException { @@ -87,13 +87,13 @@ public class TextFile { public static void stringToFile(final String content, final File file) throws IOException { - try (final OutputStream output = Files.newOutputStream(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.SYNC)) { + try (final OutputStream output = Files.newOutputStream(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.SYNC, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { output.write(content.getBytes(StandardCharsets.UTF_8)); } } public static void stringToFileWithBOM(final String content, final File file) throws IOException { - try (final OutputStream output = Files.newOutputStream(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.SYNC)) { + try (final OutputStream output = Files.newOutputStream(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.SYNC, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { output.write(new byte[]{(byte)239, (byte)187, (byte)191}); output.write(content.getBytes(StandardCharsets.UTF_8)); } @@ -153,8 +153,8 @@ public class TextFile { public static void appendBytesToFile(final byte[] bytes, final String path) throws IOException { byte[] linebreak = new byte[] {13, 10}; - Files.write(Paths.get(path), linebreak, StandardOpenOption.APPEND, StandardOpenOption.SYNC); - Files.write(Paths.get(path), bytes, StandardOpenOption.APPEND, StandardOpenOption.SYNC); + Files.write(Paths.get(path), linebreak, StandardOpenOption.APPEND, StandardOpenOption.SYNC, StandardOpenOption.WRITE); + Files.write(Paths.get(path), bytes, StandardOpenOption.APPEND, StandardOpenOption.SYNC, StandardOpenOption.WRITE); } public static byte[] fileToBytes(final String srcFile) throws FileNotFoundException, IOException { diff --git a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/TextFileTest.java b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/TextFileTest.java index 0463ca0e9..292e2f1e1 100644 --- a/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/TextFileTest.java +++ b/org.hl7.fhir.utilities/src/test/java/org/hl7/fhir/utilities/TextFileTest.java @@ -3,6 +3,7 @@ package org.hl7.fhir.utilities; import org.hl7.fhir.utilities.filesystem.ManagedFileAccess; import org.junit.jupiter.api.*; + import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -24,6 +25,8 @@ import org.junit.jupiter.api.*; private static final List SAMPLE_CONTENT_LINES = List.of("Line 1", "Line 2", "Line 3"); private static final String BOM = "\uFEFF"; + private static final byte[] BOM_BYTES = new byte[]{(byte)239, (byte)187, (byte)191}; + private static File readFile; private final static List createdFiles = new ArrayList<>(4); @@ -104,6 +107,33 @@ import org.junit.jupiter.api.*; assertArrayEquals(SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8), read); } + @Test + void testBytesToFile() throws IOException { + final var writeFile = createTempFile(); + TextFile.bytesToFile(BOM_BYTES, writeFile); + assertArrayEquals(BOM_BYTES, Files.readAllBytes(writeFile.toPath())); + } + + @Test + void testAppendBytesToFile() throws IOException { + final var writeFile = createTempFile(); + TextFile.bytesToFile(BOM_BYTES, writeFile); + assertArrayEquals(BOM_BYTES, Files.readAllBytes(writeFile.toPath())); + + TextFile.appendBytesToFile(SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8), writeFile.getAbsolutePath()); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream( ); + outputStream.write( BOM_BYTES ); + outputStream.write(new byte[] {13, 10}); //newline + outputStream.write( SAMPLE_CONTENT.getBytes(StandardCharsets.UTF_8) ); + + byte[] expected = outputStream.toByteArray(); + + byte[] actual = Files.readAllBytes(writeFile.toPath()); + assertArrayEquals(expected, actual); + + } + @Test void testStringToFile() throws IOException { final var writeFile = createTempFile();