1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.niofs;
20
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.nio.file.FileSystem;
24 import java.nio.file.FileVisitResult;
25 import java.nio.file.Files;
26 import java.nio.file.LinkOption;
27 import java.nio.file.NoSuchFileException;
28 import java.nio.file.Path;
29 import java.nio.file.SimpleFileVisitor;
30 import java.nio.file.attribute.BasicFileAttributes;
31 import java.util.Arrays;
32 import java.util.concurrent.atomic.AtomicLong;
33
34 import org.apache.logging.log4j.LogManager;
35
36
37
38
39
40 public abstract class MCRPathUtils {
41
42 private MCRPathUtils() {
43
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public static <A extends BasicFileAttributes> A getAttributes(Path path, Class<A> type, LinkOption... options) {
62 try {
63 return Files.readAttributes(path, type, options);
64 } catch (NoSuchFileException | FileNotFoundException e) {
65
66 } catch (IOException e) {
67
68 LogManager.getLogger(MCRPathUtils.class).info("Error while retrieving attributes of file: {}", path, e);
69 }
70 return null;
71 }
72
73 public static Path getPath(FileSystem targetFS, String fileName) {
74 String[] nameComps = fileName.replace('\\', '/').split("/");
75 if (nameComps.length == 1) {
76 return targetFS.getPath(nameComps[0]);
77 } else {
78 return targetFS.getPath(nameComps[0], Arrays.copyOfRange(nameComps, 1, nameComps.length));
79 }
80
81 }
82
83
84
85
86
87
88
89
90
91
92 public static long getSize(Path p) throws IOException {
93 AtomicLong size = new AtomicLong();
94 Files.walkFileTree(p, new SimpleFileVisitor<Path>() {
95 @Override
96 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
97 size.addAndGet(attrs.size());
98 return super.visitFile(file, attrs);
99 }
100 });
101 return size.get();
102 }
103
104 }