25
25
import org .culturegraph .mf .exceptions .MetafactureException ;
26
26
import org .culturegraph .mf .framework .DefaultObjectPipe ;
27
27
import org .culturegraph .mf .framework .ObjectReceiver ;
28
+ import org .culturegraph .mf .framework .annotations .Description ;
29
+ import org .culturegraph .mf .framework .annotations .In ;
30
+ import org .culturegraph .mf .framework .annotations .Out ;
31
+ import org .culturegraph .mf .types .Triple ;
28
32
29
33
/**
34
+ * Uses the input string as a file name and computes a cryptographic hash the file.
35
+ *
30
36
* @author Christoph Böhme
31
37
*
32
38
*/
39
+ @ Description ("Uses the input string as a file name and computes a cryptographic hash the file" )
40
+ @ In (String .class )
41
+ @ Out (Triple .class )
33
42
public final class FileDigestCalculator extends
34
- DefaultObjectPipe <String , ObjectReceiver <String >> {
43
+ DefaultObjectPipe <String , ObjectReceiver <Triple >> {
35
44
36
45
private static final int BUFFER_SIZE = 1024 ;
37
46
@@ -40,27 +49,29 @@ public final class FileDigestCalculator extends
40
49
private static final char [] NIBBLE_TO_HEX =
41
50
{ '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' };
42
51
52
+ private final DigestAlgorithm algorithm ;
43
53
private final MessageDigest messageDigest ;
44
54
45
55
46
56
public FileDigestCalculator (final DigestAlgorithm algorithm ) {
47
- this .messageDigest = algorithm .getInstance ();
57
+ this .algorithm = algorithm ;
58
+ this .messageDigest = this .algorithm .getInstance ();
48
59
}
49
60
50
61
public FileDigestCalculator (final String algorithm ) {
51
- this .messageDigest = DigestAlgorithm .valueOf (algorithm .toUpperCase ()).getInstance ();
62
+ this .algorithm = DigestAlgorithm .valueOf (algorithm .toUpperCase ());
63
+ this .messageDigest = this .algorithm .getInstance ();
52
64
}
53
65
54
66
@ Override
55
67
public void process (final String file ) {
56
68
final String digest ;
57
69
try (final InputStream stream = new FileInputStream (file )) {
58
70
digest = bytesToHex (getDigest (stream , messageDigest ));
59
-
60
71
} catch (IOException e ) {
61
72
throw new MetafactureException (e );
62
73
}
63
- getReceiver ().process (digest );
74
+ getReceiver ().process (new Triple ( file , algorithm . name (), digest ) );
64
75
}
65
76
66
77
private static byte [] getDigest (final InputStream stream , final MessageDigest messageDigest ) throws IOException {
0 commit comments