This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
FileUtil.java
96 lines (87 loc) · 2.93 KB
/
FileUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.cryptomator.frontend.dokany;
import com.dokany.java.constants.FileAttribute;
import com.dokany.java.structure.EnumIntegerSet;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.file.attribute.DosFileAttributeView;
import java.nio.file.attribute.DosFileAttributes;
import java.util.Set;
import java.util.stream.IntStream;
public class FileUtil {
private static final Logger LOG = LoggerFactory.getLogger(FileUtil.class);
private static final Set<Integer> globOperatorsToEscapeCodePoints;
static {
char[] globOperatorsToEscape = new char[]{'[', ']', '{', '}'};
globOperatorsToEscapeCodePoints = Sets.newHashSet();
for (int i = 0; i < globOperatorsToEscape.length; i++) {
globOperatorsToEscapeCodePoints.add(Character.codePointAt(globOperatorsToEscape, i));
}
}
/**
* TODO: support for other attributes ?
*
* @param attr the DOS file attributes of the java standard API
* @return
*/
public static EnumIntegerSet<FileAttribute> dosAttributesToEnumIntegerSet(DosFileAttributes attr) {
EnumIntegerSet<FileAttribute> set = new EnumIntegerSet<>(FileAttribute.class);
if (attr.isArchive()) {
set.add(FileAttribute.ARCHIVE);
}
if (attr.isHidden()) {
set.add(FileAttribute.HIDDEN);
}
if (attr.isReadOnly()) {
set.add(FileAttribute.READONLY);
}
if (attr.isSystem()) {
set.add(FileAttribute.SYSTEM);
}
if (attr.isDirectory()) {
set.add(FileAttribute.DIRECTORY);
}
if (attr.isRegularFile()) {
set.add(FileAttribute.NORMAL);
}
if (attr.isSymbolicLink()) {
set.add(FileAttribute.REPARSE_POINT);
}
return set;
}
public static void setAttribute(DosFileAttributeView attrView, FileAttribute attr) throws IOException {
switch (attr) {
case ARCHIVE:
attrView.setArchive(true);
break;
case HIDDEN:
attrView.setHidden(true);
break;
case READONLY:
attrView.setReadOnly(true);
break;
case SYSTEM:
attrView.setSystem(true);
break;
default:
LOG.debug("Windows file attribute {} is currently not supported and thus will be ignored", attr.name());
}
}
/**
* Method for preprocessing a string containing glob patterns for a {@link java.nio.file.PathMatcher}. These characters must be escaped to not cause a different matching expression.
* This method escapes the characters defined in {@link FileUtil#globOperatorsToEscapeCodePoints}.
*
* @param rawPattern a string possibly containing unwanted glob operators
* @return a String where some glob operators are escaped
*/
public static String addEscapeSequencesForPathPattern(String rawPattern) {
return rawPattern.codePoints().flatMap(c -> {
if (Character.isBmpCodePoint(c) && globOperatorsToEscapeCodePoints.contains(c)) {
return IntStream.of((int) '\\', c);
} else {
return IntStream.of(c);
}
}).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
}
}