Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.concurrent.BlockingQueue;
- import java.util.concurrent.CountDownLatch;
- import java.util.concurrent.Executor;
- import java.util.concurrent.Executors;
- import java.util.concurrent.LinkedBlockingQueue;
- import net.sf.jmimemagic.Magic;
- import net.sf.jmimemagic.MagicException;
- import net.sf.jmimemagic.MagicMatch;
- import net.sf.jmimemagic.MagicMatchNotFoundException;
- import net.sf.jmimemagic.MagicParseException;
- public class FileScan implements Runnable {
- private String whatFind;
- private BlockingQueue<File> queue;
- private CountDownLatch latch;
- private File endOfWorkFile;
- public FileScan(String whatFind, BlockingQueue<File> queue,
- CountDownLatch latch, File endOfWorkFile) {
- this.whatFind = whatFind;
- this.queue = queue;
- this.latch = latch;
- this.endOfWorkFile = endOfWorkFile;
- }
- public FileScan() {
- }
- @Override
- public void run() {
- while (true) {
- try {
- File file;
- file = queue.take();
- if (file == endOfWorkFile) {
- break;
- }
- scan(file);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- latch.countDown();
- }
- private void scan(File file) {
- Scanner scanner = null;
- int matches = 0;
- try {
- scanner = new Scanner(file);
- } catch (FileNotFoundException e) {
- System.out.println("File Not Found.");
- e.printStackTrace();
- }
- while (scanner.hasNext())
- if (scanner.next().equals(whatFind)) {
- matches++;
- }
- if (matches > 0) {
- String myStr = String.format(
- "File: %s - and the number of matches " + "is: %d",
- file.getAbsolutePath(), matches);
- System.out.println(myStr);
- }
- }
- public void askUserPathAndWord() {
- try {
- tryToAskUserPathAndWord();
- } catch (IOException | RuntimeException e) {
- System.out.println("Wrong input!");
- e.printStackTrace();
- } catch (InterruptedException e) {
- System.out.println("Interrupted.");
- e.printStackTrace();
- }
- }
- private void tryToAskUserPathAndWord() throws IOException,
- InterruptedException {
- PathAndWord pathAndWord = readPathAndWord();
- if (pathAndWord.isProperlyInitialized()) {
- performScan(pathAndWord, "GameOver.tmp");
- System.out.println("Thank you!");
- } else {
- System.out.println("You did not enter anything");
- }
- }
- private PathAndWord readPathAndWord() throws IOException {
- System.out
- .println("Please, enter a Path and Word (which you want to find):");
- BufferedReader bufferedReader = new BufferedReader(
- new InputStreamReader(System.in));
- String path = readPath(bufferedReader);
- String whatFind = readWord(bufferedReader);
- return new PathAndWord(path, whatFind);
- }
- private String readPath(BufferedReader bufferedReader) throws IOException {
- boolean ok = false;
- File pathInput;
- do {
- System.out.println("Please enter a Path:");
- pathInput = new File(bufferedReader.readLine());
- if (pathInput.exists() && pathInput.isDirectory()) {
- ok = true;
- } else {
- System.out.println("Doesn't exist or isn't folder!");
- }
- } while (!ok);
- return pathInput.getAbsolutePath();
- }
- private String readWord(BufferedReader bufferedReader) throws IOException {
- System.out.println("Please enter a Word:");
- return bufferedReader.readLine();
- }
- private void performScan(PathAndWord pathAndWord, String endOfWorkFileName)
- throws InterruptedException {
- BlockingQueue<File> queue = new LinkedBlockingQueue<File>();
- File endOfWorkFile = new File(endOfWorkFileName);
- CountDownLatch latch = new CountDownLatch(2);
- FolderScan folderScan = new FolderScan(pathAndWord.path, queue, latch,
- endOfWorkFile);
- FileScan fileScan = new FileScan(pathAndWord.whatFind, queue, latch,
- endOfWorkFile);
- Executor executor = Executors.newCachedThreadPool();
- executor.execute(folderScan);
- executor.execute(fileScan);
- latch.await();
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- long startTime = System.currentTimeMillis();
- new FileScan().askUserPathAndWord();
- long stopTime = System.currentTimeMillis();
- long elapsedTime = stopTime - startTime;
- System.out.println("\nRuntime time " + elapsedTime + " milliseconds.");
- }
- }
- class FolderScan implements Runnable {
- FolderScan(String path, BlockingQueue<File> queue, CountDownLatch latch,
- File endOfWorkFile) {
- this.path = path;
- this.queue = queue;
- this.latch = latch;
- this.endOfWorkFile = endOfWorkFile;
- checkers = new ArrayList<Checker>(Arrays.asList(
- new ExtentionCheckers(), /*new ProbeContentTypeCheker(),*/
- new EncodingsCheck() ));
- }
- public FolderScan() {
- }
- @Override
- public void run() {
- findFiles(path);
- queue.add(endOfWorkFile);
- latch.countDown();
- }
- private void findFiles(String path) {
- try {
- File root = new File(path);
- File[] list = root.listFiles();
- for (File currentFile : list) {
- boolean checksFailed = true;
- if (currentFile.isDirectory()) {
- findFiles(currentFile.getAbsolutePath());
- } else {
- // if (currentFile.getAbsolutePath().equals(
- // "E:\\Document\\!Nazar\\BOOKS\\)\\PDF\\readme.txt")) {
- // System.out.println();
- // }
- for (Checker currentChecker : checkers) {
- if (!currentChecker.check(currentFile)) {
- checksFailed = false;
- break;
- }
- }
- if (checksFailed) {
- queue.put(currentFile);
- }
- }
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private String path;
- private BlockingQueue<File> queue;
- private CountDownLatch latch;
- private File endOfWorkFile;
- private List<Checker> checkers;
- }
- class PathAndWord {
- final String path;
- final String whatFind;
- PathAndWord(String path, String whatFind) {
- this.path = path;
- this.whatFind = whatFind;
- }
- boolean isProperlyInitialized() {
- return path != null && whatFind != null;
- }
- }
- class ExtentionCheckers implements Checker {
- private String fileName;
- @Override
- public boolean check(File currentFile) {
- fileName = currentFile.getName().toLowerCase();
- Set<String> extensions = new HashSet<String>(Arrays.asList(".txt",
- ".pdf", ".doc", ".docx", ".html", ".htm", ".xml", ".djvu",
- ".djv", ".rar", ".rtf"));
- if (extensions.contains(fileName.substring(fileName.lastIndexOf(".")))) {
- return true;
- }
- return false;
- }
- }
- class EncodingsCheck implements Checker {
- @Override
- public boolean check(File currentFile) {
- return isUTF8(currentFile);
- }
- public static boolean isUTF8(File file) {
- // validate input
- if (null == file) {
- throw new IllegalArgumentException("input file can't be null");
- }
- if (file.isDirectory()) {
- throw new IllegalArgumentException(
- "input file refers to a directory");
- }
- // read input file
- byte[] buffer;
- try {
- buffer = readUTFHeaderBytes(file);
- } catch (IOException e) {
- throw new IllegalArgumentException(
- "Can't read input file, error = " + e.getLocalizedMessage());
- }
- if (0 == (buffer[0] & 0x80)) {
- return true; // ASCII subset character, fast path
- } else if (0xF0 == (buffer[0] & 0xF8)) { // start of 4-byte sequence
- if (buffer[3] >= buffer.length) {
- return false;
- }
- if ((0x80 == (buffer[1] & 0xC0)) && (0x80 == (buffer[2] & 0xC0))
- && (0x80 == (buffer[3] & 0xC0)))
- return true;
- } else if (0xE0 == (buffer[0] & 0xF0)) { // start of 3-byte sequence
- if (buffer[2] >= buffer.length) {
- return false;
- }
- if ((0x80 == (buffer[1] & 0xC0)) && (0x80 == (buffer[2] & 0xC0))) {
- return true;
- }
- } else if (0xC0 == (buffer[0] & 0xE0)) { // start of 2-byte sequence
- if (buffer[1] >= buffer.length) {
- return false;
- }
- if (0x80 == (buffer[1] & 0xC0)) {
- return true;
- }
- }
- return false;
- }
- private static byte[] readUTFHeaderBytes(File input) throws IOException {
- // read data
- FileInputStream fileInputStream = new FileInputStream(input);
- try{
- byte firstBytes[] = new byte[4];
- int count = fileInputStream.read(firstBytes);
- if(count < 4){
- throw new IOException("Empty file");
- }
- return firstBytes;
- } finally {
- fileInputStream.close();
- }
- }
- }
- class ProbeContentTypeCheker implements Checker {
- @Override
- public boolean check(File currentFile) {
- String mimeType = null;
- try {
- Path path = Paths.get(currentFile.getAbsolutePath());
- byte[] data = Files.readAllBytes(path);
- MagicMatch match = Magic.getMagicMatch(data);
- mimeType = match.getMimeType();
- } catch (MagicParseException | MagicMatchNotFoundException
- | MagicException | IOException e) {
- e.printStackTrace();
- }
- if (null != mimeType) {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement