Skip to content

Commit 3e7242c

Browse files
author
Tiago Rossi
committed
Optimize control files creation and fixes files being truncated on
some SMB file servers.
1 parent 2b7002e commit 3e7242c

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/main/java/gov/loc/repository/bagit/writer/BagitFileWriter.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.IOException;
44
import java.nio.charset.Charset;
5-
import java.nio.charset.StandardCharsets;
65
import java.nio.file.Files;
76
import java.nio.file.Path;
87
import java.nio.file.StandardOpenOption;
@@ -12,6 +11,7 @@
1211
import org.slf4j.LoggerFactory;
1312

1413
import gov.loc.repository.bagit.domain.Version;
14+
import java.io.BufferedWriter;
1515

1616
/**
1717
* Responsible for writing the bagit.txt to the filesystem
@@ -36,14 +36,17 @@ private BagitFileWriter(){
3636
public static void writeBagitFile(final Version version, final Charset encoding, final Path outputDir) throws IOException{
3737
final Path bagitPath = outputDir.resolve("bagit.txt");
3838
logger.debug(messages.getString("write_bagit_file_to_path"), outputDir);
39-
40-
final String firstLine = "BagIt-Version: " + version + System.lineSeparator();
41-
logger.debug(messages.getString("writing_line_to_file"), firstLine, bagitPath);
42-
Files.write(bagitPath, firstLine.getBytes(StandardCharsets.UTF_8),
43-
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
44-
45-
final String secondLine = "Tag-File-Character-Encoding: " + encoding + System.lineSeparator();
46-
logger.debug(messages.getString("writing_line_to_file"), secondLine, bagitPath);
47-
Files.write(bagitPath, secondLine.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
39+
40+
try (BufferedWriter writer = Files.newBufferedWriter(bagitPath,
41+
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
42+
43+
final String firstLine = "BagIt-Version: " + version + System.lineSeparator();
44+
logger.debug(messages.getString("writing_line_to_file"), firstLine, bagitPath);
45+
writer.append(firstLine);
46+
47+
final String secondLine = "Tag-File-Character-Encoding: " + encoding + System.lineSeparator();
48+
logger.debug(messages.getString("writing_line_to_file"), secondLine, bagitPath);
49+
writer.append(secondLine);
50+
}
4851
}
4952
}

src/main/java/gov/loc/repository/bagit/writer/FetchWriter.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.slf4j.LoggerFactory;
1313

1414
import gov.loc.repository.bagit.domain.FetchItem;
15+
import java.io.BufferedWriter;
1516

1617
/**
1718
* Responsible for writing out the list of {@link FetchItem} to the fetch.txt file on the filesystem
@@ -37,11 +38,14 @@ private FetchWriter(){
3738
public static void writeFetchFile(final List<FetchItem> itemsToFetch, final Path outputDir, final Path bagitRootDir, final Charset charsetName) throws IOException{
3839
logger.debug(messages.getString("writing_fetch_file_to_path"), outputDir);
3940
final Path fetchFilePath = outputDir.resolve("fetch.txt");
40-
41-
for(final FetchItem item : itemsToFetch){
42-
final String line = formatFetchLine(item, bagitRootDir);
43-
logger.debug(messages.getString("writing_line_to_file"), line, fetchFilePath);
44-
Files.write(fetchFilePath, line.getBytes(charsetName), StandardOpenOption.APPEND, StandardOpenOption.CREATE);
41+
42+
try (BufferedWriter writer = Files.newBufferedWriter(fetchFilePath, charsetName,
43+
StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
44+
for (final FetchItem item : itemsToFetch) {
45+
final String line = formatFetchLine(item, bagitRootDir);
46+
logger.debug(messages.getString("writing_line_to_file"), line, fetchFilePath);
47+
writer.append(line);
48+
}
4549
}
4650
}
4751

src/main/java/gov/loc/repository/bagit/writer/ManifestWriter.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.slf4j.LoggerFactory;
1414

1515
import gov.loc.repository.bagit.domain.Manifest;
16+
import java.io.BufferedWriter;
1617

1718
/**
1819
* Responsible for writing out a {@link Manifest} to the filesystem
@@ -65,14 +66,16 @@ private static void writeManifests(final Set<Manifest> manifests, final Path out
6566

6667
Files.deleteIfExists(manifestPath);
6768
Files.createFile(manifestPath);
68-
69-
for(final Entry<Path, String> entry : manifest.getFileToChecksumMap().entrySet()){
70-
//there are 2 spaces between the checksum and the path so that the manifests are compatible with the md5sum tools available on most unix systems.
71-
//This may cause problems on windows due to it being text mode, in which case either replace with a * or try verifying in binary mode with --binary
72-
final String line = entry.getValue() + " " + RelativePathWriter.formatRelativePathString(relativeTo, entry.getKey());
73-
logger.debug(messages.getString("writing_line_to_file"), line, manifestPath);
74-
Files.write(manifestPath, line.getBytes(charsetName),
75-
StandardOpenOption.APPEND, StandardOpenOption.CREATE);
69+
70+
try (BufferedWriter writer = Files.newBufferedWriter(manifestPath, charsetName,
71+
StandardOpenOption.APPEND, StandardOpenOption.CREATE)) {
72+
for (final Entry<Path, String> entry : manifest.getFileToChecksumMap().entrySet()) {
73+
//there are 2 spaces between the checksum and the path so that the manifests are compatible with the md5sum tools available on most unix systems.
74+
//This may cause problems on windows due to it being text mode, in which case either replace with a * or try verifying in binary mode with --binary
75+
final String line = entry.getValue() + " " + RelativePathWriter.formatRelativePathString(relativeTo, entry.getKey());
76+
logger.debug(messages.getString("writing_line_to_file"), line, manifestPath);
77+
writer.append(line);
78+
}
7679
}
7780
}
7881
}

0 commit comments

Comments
 (0)