SHARE
TWEET

Untitled

a guest Feb 18th, 2020 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ublic class Walk {
  2.  
  3.     private final Path filePathIn;
  4.     private final Path filePathOut;
  5.  
  6.     private static final int SIZE = 1024;
  7.     private static final int INIT = 0x811c9dc5;
  8.     private static final int PRIME = 0x01000193;
  9.  
  10.     private Walk(final String pathIn, final String pathOut) throws WalkException {
  11.         try {
  12.             filePathIn = Paths.get(pathIn);
  13.         } catch (InvalidPathException error) {
  14.             throw new WalkException("Wrong input file path: " + error.getMessage());
  15.         }
  16.         try {
  17.             filePathOut = Paths.get(pathOut);
  18.         } catch (InvalidPathException error) {
  19.             throw new WalkException("Wrong output file path: " + error.getMessage());
  20.         }
  21.         try {
  22.             if (Files.notExists(filePathOut) && filePathOut.getParent() != null) {
  23.                 Files.createDirectory(filePathOut.getParent());
  24.             }
  25.         } catch (IOException error) {
  26.             System.err.println("Can't create directory: " + filePathOut.getParent().toString());
  27.         }
  28.     }
  29.  
  30.     private static int calculateHash(final Path inPath) {
  31.         try {
  32.             int blockSize;
  33.             int hash = INIT;
  34.             InputStream reader = Files.newInputStream(inPath);
  35.             byte[] bytes = new byte[SIZE];
  36.             while ((blockSize = reader.read(bytes)) >= 0) {
  37.                 for (int i = 0; i < blockSize; i++) {
  38.                     hash = (hash * PRIME) ^ (bytes[i] & 0xff);
  39.                 }
  40.             }
  41.             return hash;
  42.         } catch (IOException error) {
  43.             return INIT;
  44.         }
  45.     }
  46.  
  47.     public static void main(String[] args) {
  48.         try {
  49.             if (args != null && args.length == 2 && args[0] != null && args[1] != null) {
  50.                 Walk walker = new Walk(args[0], args[1]);
  51.                 try (BufferedReader reader = Files.newBufferedReader(walker.filePathIn, StandardCharsets.UTF_8)) {
  52.                     try (BufferedWriter writer = Files.newBufferedWriter(walker.filePathOut, StandardCharsets.UTF_8)) {
  53.                         String filePath;
  54.                         while ((filePath = reader.readLine()) != null) {
  55.                             writer.write(String.format("%08x", calculateHash(Paths.get(filePath))) + ' ' + filePath + '\n');
  56.                         }
  57.                     } catch (FileNotFoundException error) {
  58.                         throw new WalkException("Can't find a file: " + error.getMessage());
  59.                     } catch (IOException error) {
  60.                         throw new WalkException("Something went wrong: " + error.getMessage());
  61.                     }
  62.                 } catch (FileNotFoundException error) {
  63.                     throw new WalkException("Can't find a file:: " + error.getMessage());
  64.                 } catch (IOException error) {
  65.                     throw new WalkException("Something went wrong: " + error.getMessage());
  66.                 }
  67.             } else {
  68.                 throw new WalkException("Wrong arguments: \"input file\" \"output file\"");
  69.             }
  70.         } catch (WalkException error) {
  71.             System.err.println(error.getMessage());
  72.         }
  73.     }
  74.  
  75. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top