-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFileHelper.java
94 lines (84 loc) · 3.03 KB
/
FileHelper.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
/*
* Copyright 2018 National Bank of Belgium
*
* Licensed under the EUPL, Version 1.1 or - as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*/
package ec.util.spreadsheet.helpers;
import org.checkerframework.checker.nullness.qual.NonNull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Locale;
/**
*
* @author Philippe Charles
*/
@lombok.experimental.UtilityClass
public class FileHelper {
private boolean hasExtension(String filename, String... exts) {
switch (exts.length) {
case 1:
return filename.endsWith(exts[0]);
default:
for (String o : exts) {
if (filename.endsWith(o)) {
return true;
}
}
return false;
}
}
public boolean hasExtension(@NonNull Path file, @NonNull String... exts) {
String filename = file.getName(file.getNameCount() - 1).toString().toLowerCase(Locale.ROOT);
return hasExtension(filename, exts);
}
public boolean hasExtension(@NonNull File file, @NonNull String... exts) {
String filename = file.getName().toLowerCase(Locale.ROOT);
return hasExtension(filename, exts);
}
private boolean hasMagicNumber(InputStream stream, byte... header) throws IOException {
byte[] actual = new byte[header.length];
return header.length == stream.read(actual) && Arrays.equals(actual, header);
}
public boolean hasMagicNumber(@NonNull Path file, @NonNull byte... header) {
try {
try (InputStream stream = Files.newInputStream(file)) {
return hasMagicNumber(stream, header);
}
} catch (IOException ex) {
return false;
}
}
public boolean hasMagicNumber(@NonNull File file, @NonNull byte... header) {
try {
try (InputStream stream = Files.newInputStream(file.toPath())) {
return hasMagicNumber(stream, header);
}
} catch (IOException ex) {
return false;
}
}
public boolean accept(@NonNull File file, DirectoryStream.@NonNull Filter<Path> delegate) {
try {
return delegate.accept(file.toPath());
} catch (IOException | InvalidPathException ex) {
return false;
}
}
}