Skip to content

Commit 983bb66

Browse files
committed
clean up
1 parent c837810 commit 983bb66

File tree

3 files changed

+90
-63
lines changed

3 files changed

+90
-63
lines changed

src/main/java/gov/loc/repository/bagit/reader/BagitTextFileReader.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,19 @@ private static BagitFileValues parseValues(final Path bagitFile) throws Unparsab
7272
for(final SimpleImmutableEntry<String, String> pair : pairs){
7373
if("BagIt-Version".equals(pair.getKey())){
7474
version = pair.getValue();
75-
logger.debug("BagIt-Version is [{}]", version);
7675
values.version = parseVersion(version);
7776
}
7877
if("Tag-File-Character-Encoding".equals(pair.getKey())){
7978
encoding = Charset.forName(pair.getValue());
80-
logger.debug("Tag-File-Character-Encoding is [{}]", encoding);
8179
values.encoding = encoding;
8280
}
8381
if("Payload-Byte-Count".equals(pair.getKey())){ //assume version is 1.0+
84-
logger.debug("Payload-Byte-Count is [{}]", pair.getKey());
8582
values.payloadByteCount = Long.valueOf(pair.getValue());
8683
}
8784
if("Payload-File-Count".equals(pair.getKey())){ //assume version is 1.0+
88-
logger.debug("Payload-File-Count is [{}]", pair.getKey());
8985
values.payloadFileCount = Long.valueOf(pair.getValue());
9086
}
87+
logger.debug("[{}] is [{}]", pair.getKey(), pair.getValue());
9188
}
9289

9390
return values;

src/main/java/gov/loc/repository/bagit/verify/QuickVerifier.java

Lines changed: 80 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,97 +14,131 @@
1414
import gov.loc.repository.bagit.util.PathUtils;
1515

1616
/**
17-
* responsible for all things related to quick verification. Quick verification does not
18-
* mean that a Bag is valid, only that a cursory check has been made. For a full verification
19-
* see {@link BagVerifier}
17+
* responsible for all things related to quick verification. Quick verification
18+
* does not mean that a Bag is valid, only that a cursory check has been made.
19+
* For a full verification see {@link BagVerifier}
2020
*/
2121
public final class QuickVerifier {
2222
private static final Logger logger = LoggerFactory.getLogger(QuickVerifier.class);
2323
private static final String PAYLOAD_OXUM_REGEX = "\\d+\\.\\d+";
24-
25-
private QuickVerifier(){
26-
//intentionally left empty
24+
25+
private QuickVerifier() {
26+
// intentionally left empty
2727
}
28-
28+
2929
/**
30-
* Determine if we can quickly verify by comparing the number of files and the total number of bytes expected
30+
* Determine if we can quickly verify by comparing the number of files and the
31+
* total number of bytes expected
3132
*
32-
* @param bag the {@link Bag} object you wish to check
33+
* @param bag
34+
* the {@link Bag} object you wish to check
3335
* @return true if the bag can be quickly verified
3436
*/
35-
public static boolean canQuickVerify(final Bag bag){
37+
public static boolean canQuickVerify(final Bag bag) {
3638
boolean payloadInfoExists = false;
37-
38-
if(bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null){
39+
final String payloadOxum = getPayloadOxum(bag);
40+
41+
if (bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null) {
3942
logger.debug("Found payload byte and file count, using that instead of payload-oxum");
40-
//TODO check if it matches payload-oxum, and if not issue warning?
43+
if(payloadOxum != null){
44+
comparePayloadOxumWithByteAndFileCount(payloadOxum, bag.getPayloadByteCount(), bag.getPayloadFileCount());
45+
}
4146
payloadInfoExists = true;
4247
}
43-
44-
final String payloadOxum = getPayloadOxum(bag);
45-
if(payloadOxum != null && payloadOxum.matches(PAYLOAD_OXUM_REGEX)){
48+
49+
if (payloadOxum != null && payloadOxum.matches(PAYLOAD_OXUM_REGEX)) {
4650
logger.debug("Found payload-oxum [{}] for bag [{}]", payloadOxum, bag.getRootDir());
4751
payloadInfoExists = true;
4852
}
49-
53+
5054
return payloadInfoExists && bag.getItemsToFetch().size() == 0;
5155
}
52-
56+
5357
/*
5458
* Get the Payload-Oxum value from the key value pairs
5559
*/
56-
private static String getPayloadOxum(final Bag bag){
57-
for(final SimpleImmutableEntry<String,String> keyValue : bag.getMetadata()){
58-
if("Payload-Oxum".equals(keyValue.getKey())){
60+
private static String getPayloadOxum(final Bag bag) {
61+
for (final SimpleImmutableEntry<String, String> keyValue : bag.getMetadata()) {
62+
if ("Payload-Oxum".equals(keyValue.getKey())) {
5963
return keyValue.getValue();
6064
}
6165
}
6266
return null;
6367
}
64-
68+
69+
private static void comparePayloadOxumWithByteAndFileCount(final String payloadOxum, final Long payloadByteCount,
70+
final Long payloadFileCount) {
71+
final SimpleImmutableEntry<Long, Long> payloadOxumValues = parsePayloadOxum(payloadOxum);
72+
73+
if(!payloadOxumValues.getKey().equals(payloadByteCount)){
74+
logger.warn("Payload-Oxum byte count [{}] does not match Payload-Byte-Count [{}]!", payloadOxumValues.getKey(), payloadByteCount);
75+
}
76+
77+
if(!payloadOxumValues.getValue().equals(payloadFileCount)){
78+
logger.warn("Payload-Oxum file count [{}] does not match Payload-File-Count [{}]!", payloadOxumValues.getValue(), payloadFileCount);
79+
}
80+
}
81+
6582
/**
66-
* Quickly verify by comparing the number of files and the total number of bytes expected
83+
* Quickly verify by comparing the number of files and the total number of
84+
* bytes expected
6785
*
68-
* @param bag the bag to quickly verify
69-
* @param ignoreHiddenFiles ignore hidden files found in payload directory
86+
* @param bag
87+
* the bag to quickly verify
88+
* @param ignoreHiddenFiles
89+
* ignore hidden files found in payload directory
7090
*
71-
* @throws IOException if there is an error reading a file
72-
* @throws InvalidPayloadOxumException if either the total bytes or the number of files
73-
* calculated for the payload directory of the bag is different than the supplied values
74-
* @throws PayloadOxumDoesNotExistException if the bag does not contain a payload-oxum.
75-
* To check, run {@link BagVerifier#canQuickVerify}
91+
* @throws IOException
92+
* if there is an error reading a file
93+
* @throws InvalidPayloadOxumException
94+
* if either the total bytes or the number of files calculated for
95+
* the payload directory of the bag is different than the supplied
96+
* values
97+
* @throws PayloadOxumDoesNotExistException
98+
* if the bag does not contain a payload-oxum. To check, run
99+
* {@link BagVerifier#canQuickVerify}
76100
*/
77-
public static void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles) throws IOException, InvalidPayloadOxumException{
101+
public static void quicklyVerify(final Bag bag, final boolean ignoreHiddenFiles)
102+
throws IOException, InvalidPayloadOxumException {
78103
final SimpleImmutableEntry<Long, Long> byteAndFileCount = getByteAndFileCount(bag);
79-
104+
80105
final Path payloadDir = PathUtils.getDataDir(bag);
81106
final FileCountAndTotalSizeVistor vistor = new FileCountAndTotalSizeVistor(ignoreHiddenFiles);
82107
Files.walkFileTree(payloadDir, vistor);
83-
logger.info("supplied payload-oxum: [{}.{}], Calculated payload-oxum: [{}.{}], for payload directory [{}]",
108+
logger.info("supplied payload-oxum: [{}.{}], Calculated payload-oxum: [{}.{}], for payload directory [{}]",
84109
byteAndFileCount.getKey(), byteAndFileCount.getValue(), vistor.getTotalSize(), vistor.getCount(), payloadDir);
85-
86-
if(byteAndFileCount.getKey() != vistor.getTotalSize()){
87-
throw new InvalidPayloadOxumException("Invalid total size. Expected " + byteAndFileCount.getKey() + " but calculated " + vistor.getTotalSize());
110+
111+
if (byteAndFileCount.getKey() != vistor.getTotalSize()) {
112+
throw new InvalidPayloadOxumException(
113+
"Invalid total size. Expected " + byteAndFileCount.getKey() + " but calculated " + vistor.getTotalSize());
88114
}
89-
if(byteAndFileCount.getValue() != vistor.getCount()){
90-
throw new InvalidPayloadOxumException("Invalid file count. Expected " + byteAndFileCount.getValue() + " but found " + vistor.getCount() + " files");
115+
if (byteAndFileCount.getValue() != vistor.getCount()) {
116+
throw new InvalidPayloadOxumException(
117+
"Invalid file count. Expected " + byteAndFileCount.getValue() + " but found " + vistor.getCount() + " files");
91118
}
92119
}
93-
120+
94121
/**
95-
* get either the payload-oxum values or the payload-byte-count and payload-file-count
122+
* get either the payload-oxum values or the payload-byte-count and
123+
* payload-file-count
96124
*
97-
* @param bag the bag to get the payload info from
125+
* @param bag
126+
* the bag to get the payload info from
98127
* @return the byte count, the file count
99128
*/
100-
private static SimpleImmutableEntry<Long, Long> getByteAndFileCount(final Bag bag){
101-
if(bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null){
129+
private static SimpleImmutableEntry<Long, Long> getByteAndFileCount(final Bag bag) {
130+
if (bag.getPayloadByteCount() != null && bag.getPayloadFileCount() != null) {
102131
return new SimpleImmutableEntry<Long, Long>(bag.getPayloadByteCount(), bag.getPayloadFileCount());
103132
}
104-
133+
105134
final String payloadOxum = getPayloadOxum(bag);
106-
if(payloadOxum == null || !payloadOxum.matches(PAYLOAD_OXUM_REGEX)){
107-
throw new PayloadOxumDoesNotExistException("Payload-Oxum or payload-byte-count and payload-file-count does not exist in bag.");
135+
return parsePayloadOxum(payloadOxum);
136+
}
137+
138+
private static SimpleImmutableEntry<Long, Long> parsePayloadOxum(final String payloadOxum){
139+
if (payloadOxum == null || !payloadOxum.matches(PAYLOAD_OXUM_REGEX)) {
140+
throw new PayloadOxumDoesNotExistException(
141+
"Payload-Oxum or payload-byte-count and payload-file-count does not exist in bag.");
108142
}
109143

110144
final String[] parts = payloadOxum.split("\\.");

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,18 @@ private static void writeBagitFileInternal(final Version version, final Charset
5555
final Path bagitPath = outputDir.resolve("bagit.txt");
5656
logger.debug("Writing bagit.txt file to [{}]", outputDir);
5757

58+
final StringBuilder sb = new StringBuilder(100);
5859

59-
final String firstLine = "BagIt-Version : " + version + System.lineSeparator();
60-
logger.debug("Writing line [{}] to [{}]", firstLine, bagitPath);
61-
Files.write(bagitPath, firstLine.getBytes(StandardCharsets.UTF_8),
62-
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
63-
64-
final String secondLine = "Tag-File-Character-Encoding : " + encoding + System.lineSeparator();
65-
logger.debug("Writing line [{}] to [{}]", secondLine, bagitPath);
66-
Files.write(bagitPath, secondLine.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
60+
sb.append("BagIt-Version : ").append(version).append(System.lineSeparator())
61+
.append("Tag-File-Character-Encoding : ").append(encoding).append(System.lineSeparator());
6762

63+
logger.debug("Writing [{}] to [{}]", sb.toString(), bagitPath);
6864
if(version.compareTo(ONE_DOT_ZERO) >= 0 && payloadByteCount != null && payloadFileCount != null){ //if it is 1.0 or greater
69-
final String thirdLine = "Payload-Byte-Count : " + payloadByteCount + System.lineSeparator();
70-
Files.write(bagitPath, thirdLine.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
71-
72-
final String fourthLine = "Payload-File-Count : " + payloadFileCount + System.lineSeparator();
73-
Files.write(bagitPath, fourthLine.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
65+
sb.append("Payload-Byte-Count : ").append(payloadByteCount).append(System.lineSeparator())
66+
.append("Payload-File-Count : ").append(payloadFileCount).append(System.lineSeparator());
7467
}
68+
69+
Files.write(bagitPath, sb.toString().getBytes(StandardCharsets.UTF_8),
70+
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE);
7571
}
7672
}

0 commit comments

Comments
 (0)