-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchFileVisitor.java
76 lines (59 loc) · 2.19 KB
/
SearchFileVisitor.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
package com.javarush.task.task31.task3111;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class SearchFileVisitor extends SimpleFileVisitor<Path> {
private int minSize;
private int maxSize;
private String partOfName;
private String partOfContent;
private List<Path> foundFiles = new ArrayList<>();
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
byte[] content = Files.readAllBytes(file); // размер файла: content.length
if (this.isCorrespondingFile(file, content)) {
this.foundFiles.add(file);
}
return super.visitFile(file, attrs);
}
private boolean isCorrespondingFile(Path file, byte[] content) {
return isValidMinSize(content)
&& isValidMaxSize(content)
&& isValidPartOfName(file)
&& isValidPartOfContent(content);
}
private boolean isValidMinSize(byte[] content) {
return this.minSize == 0 || content.length > this.minSize;
}
private boolean isValidMaxSize(byte[] content) {
return this.maxSize == 0 || content.length < this.maxSize;
}
private boolean isValidPartOfName(Path file) {
String fileName = file.getFileName().toString();
return this.partOfName == null || fileName.contains(this.partOfName);
}
private boolean isValidPartOfContent(byte[] content) {
String stringContent = new String(content);
return this.partOfContent == null || stringContent.contains(this.partOfContent);
}
public void setPartOfName(String partOfName) {
this.partOfName = partOfName;
}
public void setPartOfContent(String partOfContent) {
this.partOfContent = partOfContent;
}
public void setMinSize(int minSize) {
this.minSize = minSize;
}
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
public List<Path> getFoundFiles() {
return foundFiles;
}
}