Skip to content

Commit bcb9896

Browse files
committed
refs #38 replace File with Path
1 parent 2792e18 commit bcb9896

14 files changed

Lines changed: 249 additions & 262 deletions

File tree

src/main/java/gov/loc/repository/bagit/creator/AddPayloadToBagManifestVistor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs)throws IOE
5656
InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ);
5757
String hash = Hasher.hash(inputStream, messageDigest);
5858
logger.debug("Adding [{}] to manifest with hash [{}]", path, hash);
59-
manifest.getFileToChecksumMap().put(path.toFile(), hash);
59+
manifest.getFileToChecksumMap().put(path, hash);
6060
}
6161

6262
return FileVisitResult.CONTINUE;

src/main/java/gov/loc/repository/bagit/creator/BagCreator.java

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package gov.loc.repository.bagit.creator;
22

3-
import java.io.File;
43
import java.io.IOException;
4+
import java.nio.file.DirectoryStream;
55
import java.nio.file.Files;
66
import java.nio.file.Path;
7-
import java.nio.file.Paths;
87
import java.security.MessageDigest;
98
import java.security.NoSuchAlgorithmException;
109

@@ -37,24 +36,26 @@ public class BagCreator {
3736
* @throws IOException if there is a problem writing or moving file(s)
3837
* @return a {@link Bag} object representing the newly created bagit bag
3938
*/
40-
public static Bag bagInPlace(File root, SupportedAlgorithm algorithm, boolean includeHidden) throws NoSuchAlgorithmException, IOException{
39+
public static Bag bagInPlace(Path root, SupportedAlgorithm algorithm, boolean includeHidden) throws NoSuchAlgorithmException, IOException{
4140
Bag bag = new Bag(new Version(0, 97));
4241
bag.setRootDir(root);
4342
logger.info("Creating a bag with version: [{}] in directory: [{}]", bag.getVersion(), root);
4443

45-
File[] files = root.listFiles();
46-
File dataDir = new File(root, "data");
47-
if(!dataDir.exists() && !dataDir.mkdir()){
48-
throw new IOException("Unable to make " + dataDir);
49-
}
5044

51-
moveFilesToDataDir(files, dataDir);
45+
Path dataDir = root.resolve("data");
46+
Files.createDirectory(dataDir);
47+
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(root);
48+
for(Path path : directoryStream){
49+
if(!path.equals(dataDir) && !Files.isHidden(path) || includeHidden){
50+
Files.move(path, dataDir.resolve(path.getFileName()));
51+
}
52+
}
5253

5354
logger.info("Creating payload manifest");
5455
Manifest manifest = new Manifest(algorithm);
5556
MessageDigest messageDigest = MessageDigest.getInstance(algorithm.getMessageDigestName());
5657
AddPayloadToBagManifestVistor visitor = new AddPayloadToBagManifestVistor(manifest, messageDigest, includeHidden);
57-
Files.walkFileTree(Paths.get(dataDir.toURI()), visitor);
58+
Files.walkFileTree(dataDir, visitor);
5859

5960
bag.getPayLoadManifests().add(manifest);
6061
BagWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), root);
@@ -64,17 +65,6 @@ public static Bag bagInPlace(File root, SupportedAlgorithm algorithm, boolean in
6465
return bag;
6566
}
6667

67-
protected static void moveFilesToDataDir(File[] files, File dataDir) throws IOException{
68-
if(files != null){
69-
logger.info("Moving files to [{}]", dataDir);
70-
for(File file : files){
71-
Path dest = Paths.get(dataDir.getPath(), file.getName());
72-
logger.debug("Moving [{}] to [{}]", file, dest);
73-
Files.move(Paths.get(file.toURI()), dest);
74-
}
75-
}
76-
}
77-
7868
/**
7969
* Creates a basic(only required elements) .bagit bag in place.
8070
* This creates files and directories, thus if an error is thrown during operation it may leave the filesystem
@@ -87,21 +77,19 @@ protected static void moveFilesToDataDir(File[] files, File dataDir) throws IOEx
8777
* @throws IOException if there is a problem writing files or .bagit directory
8878
*/
8979
@Incubating
90-
public static Bag createDotBagit(File root, SupportedAlgorithm algorithm, boolean includeHidden) throws NoSuchAlgorithmException, IOException{
80+
public static Bag createDotBagit(Path root, SupportedAlgorithm algorithm, boolean includeHidden) throws NoSuchAlgorithmException, IOException{
9181
Bag bag = new Bag(new Version(0, 98));
9282
bag.setRootDir(root);
9383
logger.info("Creating a bag with version: [{}] in directory: [{}]", bag.getVersion(), root);
9484

95-
File dotbagitDir = new File(root, ".bagit");
96-
if(!dotbagitDir.mkdir()){
97-
throw new IOException("Was unable to create " + dotbagitDir);
98-
}
85+
Path dotbagitDir = root.resolve(".bagit");
86+
Files.createDirectories(dotbagitDir);
9987

10088
logger.info("Creating payload manifest");
10189
Manifest manifest = new Manifest(algorithm);
10290
MessageDigest messageDigest = MessageDigest.getInstance(algorithm.getMessageDigestName());
10391
AddPayloadToBagManifestVistor visitor = new AddPayloadToBagManifestVistor(manifest, messageDigest, includeHidden);
104-
Files.walkFileTree(Paths.get(root.toURI()), visitor);
92+
Files.walkFileTree(root, visitor);
10593

10694
bag.getPayLoadManifests().add(manifest);
10795
BagWriter.writeBagitFile(bag.getVersion(), bag.getFileEncoding(), dotbagitDir);

src/main/java/gov/loc/repository/bagit/domain/Bag.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package gov.loc.repository.bagit.domain;
22

3-
import java.io.File;
43
import java.nio.charset.StandardCharsets;
4+
import java.nio.file.Path;
55
import java.util.ArrayList;
66
import java.util.HashSet;
77
import java.util.List;
@@ -33,7 +33,7 @@ public class Bag {
3333
private List<Pair<String, String>> metadata = new ArrayList<>();
3434

3535
//the current location of the bag on the filesystem
36-
private File rootDir;
36+
private Path rootDir;
3737

3838
public Bag(){
3939
}
@@ -142,11 +142,11 @@ public boolean equals(Object obj) {
142142
Objects.equals(this.metadata, other.getMetadata());
143143
}
144144

145-
public File getRootDir() {
145+
public Path getRootDir() {
146146
return rootDir;
147147
}
148148

149-
public void setRootDir(File rootDir) {
149+
public void setRootDir(Path rootDir) {
150150
this.rootDir = rootDir;
151151
}
152152

src/main/java/gov/loc/repository/bagit/domain/Manifest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
package gov.loc.repository.bagit.domain;
33

4-
import java.io.File;
4+
import java.nio.file.Path;
55
import java.util.HashMap;
66

77
import gov.loc.repository.bagit.hash.SupportedAlgorithm;
@@ -11,17 +11,17 @@
1111
*/
1212
public class Manifest {
1313
private final SupportedAlgorithm algorithm;
14-
private HashMap<File, String> fileToChecksumMap = new HashMap<>();
14+
private HashMap<Path, String> fileToChecksumMap = new HashMap<>();
1515

1616
public Manifest(SupportedAlgorithm algorithm){
1717
this.algorithm = algorithm;
1818
}
1919

20-
public HashMap<File, String> getFileToChecksumMap() {
20+
public HashMap<Path, String> getFileToChecksumMap() {
2121
return fileToChecksumMap;
2222
}
2323

24-
public void setFileToChecksumMap(HashMap<File, String> fileToChecksumMap) {
24+
public void setFileToChecksumMap(HashMap<Path, String> fileToChecksumMap) {
2525
this.fileToChecksumMap = fileToChecksumMap;
2626
}
2727

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

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package gov.loc.repository.bagit.reader;
22

33
import java.io.BufferedReader;
4-
import java.io.File;
5-
import java.io.FilenameFilter;
64
import java.io.IOException;
75
import java.net.URL;
6+
import java.nio.file.DirectoryStream;
87
import java.nio.file.Files;
9-
import java.nio.file.Paths;
8+
import java.nio.file.Path;
109
import java.util.ArrayList;
1110
import java.util.HashMap;
1211
import java.util.List;
@@ -48,22 +47,22 @@ public BagReader(BagitAlgorithmNameToSupportedAlgorithmMapping nameMapping){
4847
* @return a {@link Bag} object representing a bag on the filesystem
4948
* @throws UnparsableVersionException If there is a problem parsing the bagit version
5049
*/
51-
public Bag read(File rootDir) throws IOException, UnparsableVersionException{
52-
File bagitDir = new File(rootDir, ".bagit");
53-
if(!bagitDir.exists()){
50+
public Bag read(Path rootDir) throws IOException, UnparsableVersionException{
51+
Path bagitDir = rootDir.resolve(".bagit");
52+
if(!Files.exists(bagitDir)){
5453
bagitDir = rootDir;
5554
}
5655

57-
File bagitFile = new File(bagitDir, "bagit.txt");
56+
Path bagitFile = bagitDir.resolve("bagit.txt");
5857
Bag bag = readBagitTextFile(bagitFile, new Bag());
5958
bag.setRootDir(rootDir);
6059

6160
bag = readAllManifests(bagitDir, bag);
6261

6362
bag = readBagMetadata(bagitDir, bag);
6463

65-
File fetchFile = new File(bagitDir, "fetch.txt");
66-
if(fetchFile.exists()){
64+
Path fetchFile = bagitDir.resolve("fetch.txt");
65+
if(Files.exists(fetchFile)){
6766
bag = readFetch(fetchFile, bag);
6867
}
6968

@@ -79,7 +78,7 @@ public Bag read(File rootDir) throws IOException, UnparsableVersionException{
7978
* @throws IOException if there is a problem reading a file
8079
* @throws UnparsableVersionException if there is a problem parsing the bagit version number
8180
*/
82-
public Bag readBagitTextFile(File bagitFile, Bag bag) throws IOException, UnparsableVersionException{
81+
public Bag readBagitTextFile(Path bagitFile, Bag bag) throws IOException, UnparsableVersionException{
8382
logger.debug("Reading bagit.txt file");
8483
List<Pair<String, String>> pairs = readKeyValuesFromFile(bagitFile, ":");
8584

@@ -123,34 +122,33 @@ protected Version parseVersion(String version) throws UnparsableVersionException
123122
* @return a new bag that contains all the manifest(s) information
124123
* @throws IOException if there is a problem reading a file
125124
*/
126-
public Bag readAllManifests(File rootDir, Bag bag) throws IOException{
125+
public Bag readAllManifests(Path rootDir, Bag bag) throws IOException{
127126
logger.info("Attempting to find and read manifests");
128127
Bag newBag = new Bag(bag);
129-
File[] files = getAllManifestFiles(rootDir);
128+
DirectoryStream<Path> manifests = getAllManifestFiles(rootDir);
130129

131-
for(File file : files){
132-
if(file.getName().startsWith("tag")){
133-
logger.debug("Found tag manifest [{}]", file);
134-
newBag.getTagManifests().add(readManifest(file, bag.getRootDir()));
130+
for (Path path : manifests){
131+
if(path.getFileName().toString().startsWith("tagmanifest-")){
132+
logger.debug("Found tag manifest [{}]", path);
133+
newBag.getTagManifests().add(readManifest(path, bag.getRootDir()));
135134
}
136-
else if(file.getName().startsWith("manifest")){
137-
logger.debug("Found payload manifest [{}]", file);
138-
newBag.getPayLoadManifests().add(readManifest(file, bag.getRootDir()));
135+
else if(path.getFileName().toString().startsWith("manifest-")){
136+
logger.debug("Found payload manifest [{}]", path);
137+
newBag.getPayLoadManifests().add(readManifest(path, bag.getRootDir()));
139138
}
140139
}
141140

142141
return newBag;
143142
}
144143

145-
protected File[] getAllManifestFiles(File rootDir){
146-
File[] files = rootDir.listFiles(new FilenameFilter() {
147-
@Override
148-
public boolean accept(File dir, String name) {
149-
return name.matches("(tag)?manifest\\-.*\\.txt");
144+
protected DirectoryStream<Path> getAllManifestFiles(Path rootDir) throws IOException{
145+
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
146+
public boolean accept(Path file) throws IOException {
147+
return file.getFileName().toString().startsWith("tagmanifest-") || file.getFileName().toString().startsWith("manifest-");
150148
}
151-
});
149+
};
152150

153-
return files == null? new File[]{} : files;
151+
return Files.newDirectoryStream(rootDir, filter);
154152
}
155153

156154
/**
@@ -160,27 +158,27 @@ public boolean accept(File dir, String name) {
160158
* @return the converted manifest object from the file
161159
* @throws IOException if there is a problem reading a file
162160
*/
163-
public Manifest readManifest(File manifestFile, File bagRootDir) throws IOException{
161+
public Manifest readManifest(Path manifestFile, Path bagRootDir) throws IOException{
164162
logger.debug("Reading manifest [{}]", manifestFile);
165-
String alg = manifestFile.getName().split("[-\\.]")[1];
163+
String alg = manifestFile.getFileName().toString().split("[-\\.]")[1];
166164
SupportedAlgorithm algorithm = nameMapping.getMessageDigestName(alg);
167165

168166
Manifest manifest = new Manifest(algorithm);
169167

170-
HashMap<File, String> filetToChecksumMap = readChecksumFileMap(manifestFile, bagRootDir);
168+
HashMap<Path, String> filetToChecksumMap = readChecksumFileMap(manifestFile, bagRootDir);
171169
manifest.setFileToChecksumMap(filetToChecksumMap);
172170

173171
return manifest;
174172
}
175173

176-
protected HashMap<File, String> readChecksumFileMap(File manifestFile, File bagRootDir) throws IOException{
177-
HashMap<File, String> map = new HashMap<>();
178-
BufferedReader br = Files.newBufferedReader(Paths.get(manifestFile.toURI()));
174+
protected HashMap<Path, String> readChecksumFileMap(Path manifestFile, Path bagRootDir) throws IOException{
175+
HashMap<Path, String> map = new HashMap<>();
176+
BufferedReader br = Files.newBufferedReader(manifestFile);
179177

180178
String line = br.readLine();
181179
while(line != null){
182180
String[] parts = line.split("\\s+", 2);
183-
File file = new File(bagRootDir, parts[1]);
181+
Path file = bagRootDir.resolve(parts[1]);
184182
logger.debug("Read checksum [{}] and file [{}] from manifest [{}]", parts[0], file, manifestFile);
185183
map.put(file, parts[0]);
186184
line = br.readLine();
@@ -197,18 +195,18 @@ protected HashMap<File, String> readChecksumFileMap(File manifestFile, File bagR
197195
* @return a new bag that contains the bag-info.txt (metadata) information
198196
* @throws IOException if there is a problem reading a file
199197
*/
200-
public Bag readBagMetadata(File rootDir, Bag bag) throws IOException{
198+
public Bag readBagMetadata(Path rootDir, Bag bag) throws IOException{
201199
logger.info("Attempting to read bag metadata file");
202200
Bag newBag = new Bag(bag);
203201
List<Pair<String, String>> metadata = new ArrayList<>();
204202

205-
File bagInfoFile = new File(rootDir, "bag-info.txt");
206-
if(bagInfoFile.exists()){
203+
Path bagInfoFile = rootDir.resolve("bag-info.txt");
204+
if(Files.exists(bagInfoFile)){
207205
logger.debug("Found [{}] file", bagInfoFile);
208206
metadata = readKeyValuesFromFile(bagInfoFile, ":");
209207
}
210-
File packageInfoFile = new File(rootDir, "package-info.txt"); //onlu exists in versions 0.93 - 0.95
211-
if(packageInfoFile.exists()){
208+
Path packageInfoFile = rootDir.resolve("package-info.txt"); //onlu exists in versions 0.93 - 0.95
209+
if(Files.exists(packageInfoFile)){
212210
logger.debug("Found [{}] file", packageInfoFile);
213211
metadata = readKeyValuesFromFile(packageInfoFile, ":");
214212
}
@@ -226,10 +224,10 @@ public Bag readBagMetadata(File rootDir, Bag bag) throws IOException{
226224
* @return a new bag that contains a list of items to fetch
227225
* @throws IOException if there is a problem reading a file
228226
*/
229-
public Bag readFetch(File fetchFile, Bag bag) throws IOException{
227+
public Bag readFetch(Path fetchFile, Bag bag) throws IOException{
230228
logger.info("Attempting to read [{}]", fetchFile);
231229
Bag newBag = new Bag(bag);
232-
BufferedReader br = Files.newBufferedReader(Paths.get(fetchFile.toURI()));
230+
BufferedReader br = Files.newBufferedReader(fetchFile);
233231

234232
String line = br.readLine();
235233
while(line != null){
@@ -247,9 +245,9 @@ public Bag readFetch(File fetchFile, Bag bag) throws IOException{
247245
return newBag;
248246
}
249247

250-
protected static List<Pair<String, String>> readKeyValuesFromFile(File file, String splitRegex) throws IOException{
248+
protected static List<Pair<String, String>> readKeyValuesFromFile(Path file, String splitRegex) throws IOException{
251249
List<Pair<String, String>> keyValues = new ArrayList<>();
252-
BufferedReader br = Files.newBufferedReader(Paths.get(file.toURI()));
250+
BufferedReader br = Files.newBufferedReader(file);
253251

254252
String line = br.readLine();
255253
while(line != null){

src/main/java/gov/loc/repository/bagit/tasks/CheckIfFileExistsTask.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
package gov.loc.repository.bagit.tasks;
22

3-
import java.io.File;
3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
45
import java.util.concurrent.CountDownLatch;
56

67
/**
78
* A simple task to check if a file exists on the filesystem. This is thread safe, so many can be called at once.
89
*/
910
public class CheckIfFileExistsTask implements Runnable {
10-
private final File file;
11+
private final Path file;
1112
private final StringBuilder messageBuilder;
1213
private final CountDownLatch latch;
1314

14-
public CheckIfFileExistsTask(File file, StringBuilder messageBuilder, CountDownLatch latch) {
15+
public CheckIfFileExistsTask(Path file, StringBuilder messageBuilder, CountDownLatch latch) {
1516
this.file = file;
1617
this.messageBuilder = messageBuilder;
1718
this.latch = latch;
1819
}
1920

2021
@Override
2122
public void run() {
22-
if(!file.exists()){
23+
if(!Files.exists(file)){
2324
messageBuilder.append("Manifest lists file [").append(file).append("] but it does not exist").append(System.lineSeparator());
2425
}
2526
latch.countDown();

0 commit comments

Comments
 (0)