Add TRUNCATE_EXISTING and WRITE open options (fix overwrite behavior)
This commit is contained in:
parent
4daf7feb93
commit
cfed736cd6
|
@ -69,7 +69,7 @@ public class TextFile {
|
|||
public static void writeAllLines(String path, List<String> 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 {
|
||||
|
|
|
@ -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<String> 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<File> 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();
|
||||
|
|
Loading…
Reference in New Issue