Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ublic class Walk {
- private final Path filePathIn;
- private final Path filePathOut;
- private static final int SIZE = 1024;
- private static final int INIT = 0x811c9dc5;
- private static final int PRIME = 0x01000193;
- private Walk(final String pathIn, final String pathOut) throws WalkException {
- try {
- filePathIn = Paths.get(pathIn);
- } catch (InvalidPathException error) {
- throw new WalkException("Wrong input file path: " + error.getMessage());
- }
- try {
- filePathOut = Paths.get(pathOut);
- } catch (InvalidPathException error) {
- throw new WalkException("Wrong output file path: " + error.getMessage());
- }
- try {
- if (Files.notExists(filePathOut) && filePathOut.getParent() != null) {
- Files.createDirectory(filePathOut.getParent());
- }
- } catch (IOException error) {
- System.err.println("Can't create directory: " + filePathOut.getParent().toString());
- }
- }
- private static int calculateHash(final Path inPath) {
- try {
- int blockSize;
- int hash = INIT;
- InputStream reader = Files.newInputStream(inPath);
- byte[] bytes = new byte[SIZE];
- while ((blockSize = reader.read(bytes)) >= 0) {
- for (int i = 0; i < blockSize; i++) {
- hash = (hash * PRIME) ^ (bytes[i] & 0xff);
- }
- }
- return hash;
- } catch (IOException error) {
- return INIT;
- }
- }
- public static void main(String[] args) {
- try {
- if (args != null && args.length == 2 && args[0] != null && args[1] != null) {
- Walk walker = new Walk(args[0], args[1]);
- try (BufferedReader reader = Files.newBufferedReader(walker.filePathIn, StandardCharsets.UTF_8)) {
- try (BufferedWriter writer = Files.newBufferedWriter(walker.filePathOut, StandardCharsets.UTF_8)) {
- String filePath;
- while ((filePath = reader.readLine()) != null) {
- writer.write(String.format("%08x", calculateHash(Paths.get(filePath))) + ' ' + filePath + '\n');
- }
- } catch (FileNotFoundException error) {
- throw new WalkException("Can't find a file: " + error.getMessage());
- } catch (IOException error) {
- throw new WalkException("Something went wrong: " + error.getMessage());
- }
- } catch (FileNotFoundException error) {
- throw new WalkException("Can't find a file:: " + error.getMessage());
- } catch (IOException error) {
- throw new WalkException("Something went wrong: " + error.getMessage());
- }
- } else {
- throw new WalkException("Wrong arguments: \"input file\" \"output file\"");
- }
- } catch (WalkException error) {
- System.err.println(error.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement