Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
.gradle
build/
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: java

jdk:
- oraclejdk8
Empty file removed README.md
Empty file.
15 changes: 15 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
group 'spbau.shevchenko'
version '1.0-SNAPSHOT'

apply plugin: 'java'

//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.8

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#Thu Mar 02 20:22:38 MSK 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip
164 changes: 164 additions & 0 deletions gradlew

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

90 changes: 90 additions & 0 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = 'Lazy'

34 changes: 34 additions & 0 deletions src/main/java/ru/spbau/shevchenko/CLI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.spbau.shevchenko;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;

public class CLI {
public static void main(String[] args) throws IOException {
for (String path : args) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ну хорошо бы всё же вывести что-то, если пользователь не передал аргументов

processSingleThreaded(path);
processMultiThreaded(path);
}
}

private static void processMultiThreaded(String path) throws IOException {
System.out.println("Multithreaded:");
process(path, new ForkJoinCalculator());
}
private static void processSingleThreaded(String path) throws IOException {
System.out.println("Singlethreaded:");
process(path, new SingleThreadedCalculator());
}

private static void process(String path, FileChecksumCalculator calculator) throws IOException {
long startTime = System.currentTimeMillis();
byte[] hash = calculator.calcChecksum(Paths.get(path));
long endTime = System.currentTimeMillis();
Double tookSeconds = (endTime - startTime) / 1000.0;
System.out.println("Calculated result for " + path + " in " + tookSeconds.toString());
System.out.println("Result is: " + Arrays.toString(hash));
System.out.println();
}

}
56 changes: 56 additions & 0 deletions src/main/java/ru/spbau/shevchenko/ChecksumTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.spbau.shevchenko;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RecursiveTask;

class ChecksumTask extends RecursiveTask<byte[]> {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

а где же доки...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

и тесты, да

private static final String MD5 = "MD5";
private static final String NO_SUCH_ALGORITHM_MD5 = "Well, there's no MD5 algorithm in Java...";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

зачем же одни и те же константы по нескольку раз объявлять?

private Path path;

ChecksumTask(Path path) throws FileNotFoundException {
if (Files.notExists(path)) {
throw new FileNotFoundException(path.toString() + " not found!");
}
this.path = path;
}

@Override
protected byte[] compute() {
if (!Files.isDirectory(path)) {
try {
return new SingleFileChecksumCalculator().calcChecksum(path);
} catch (IOException e) {
throw new RuntimeException("Failed to calculate checksum for " + path.toString());
}
}
else {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
MessageDigest messageDigest = MessageDigest.getInstance(MD5);
messageDigest.update(path.getFileName().toString().getBytes());
List<ChecksumTask> subtasks = new ArrayList<>();
for (Path entry: stream) {
ChecksumTask subtask = new ChecksumTask(entry);
subtask.fork();
subtasks.add(subtask);
}
for (ChecksumTask subtask : subtasks) {
messageDigest.update(subtask.join());
}
return messageDigest.digest();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(NO_SUCH_ALGORITHM_MD5);
} catch (IOException e) {
throw new RuntimeException("Failed to calculate checksum for " + path.toString());
}
}
}
}
8 changes: 8 additions & 0 deletions src/main/java/ru/spbau/shevchenko/FileChecksumCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package ru.spbau.shevchenko;

import java.io.IOException;
import java.nio.file.Path;

interface FileChecksumCalculator {
byte[] calcChecksum(Path path) throws IOException;
}
Loading