-
Notifications
You must be signed in to change notification settings - Fork 0
MD5 #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
MD5 #8
Changes from all commits
729da70
7b666c1
9e158eb
196374a
5a2f2c1
7ab9dd3
c1bafdc
332ca46
d598c8e
19622a0
1636823
912b5fd
0e3a5a0
2e85cf6
b89b207
e198a3b
e71fc9d
2fde044
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| .idea | ||
| .gradle | ||
| build/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| language: java | ||
|
|
||
| jdk: | ||
| - oraclejdk8 |
| 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' | ||
| } |
| 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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| rootProject.name = 'Lazy' | ||
|
|
| 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) { | ||
| 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(); | ||
| } | ||
|
|
||
| } | ||
| 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[]> { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. а где же доки... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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..."; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| 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; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ну хорошо бы всё же вывести что-то, если пользователь не передал аргументов