13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .metafacture .files ;
17
16
18
- import java .io .FileInputStream ;
19
- import java .io .IOException ;
20
- import java .io .InputStream ;
21
- import java .security .MessageDigest ;
22
- import java .security .NoSuchAlgorithmException ;
17
+ package org .metafacture .files ;
23
18
24
19
import org .metafacture .framework .FluxCommand ;
25
20
import org .metafacture .framework .MetafactureException ;
30
25
import org .metafacture .framework .helpers .DefaultObjectPipe ;
31
26
import org .metafacture .framework .objects .Triple ;
32
27
28
+ import java .io .FileInputStream ;
29
+ import java .io .IOException ;
30
+ import java .io .InputStream ;
31
+ import java .security .MessageDigest ;
32
+ import java .security .NoSuchAlgorithmException ;
33
+
33
34
/**
34
35
* Interprets the input string as a file name and computes a cryptographic hash
35
36
* for the file.
@@ -48,13 +49,13 @@ public final class FileDigestCalculator extends
48
49
49
50
private static final int HIGH_NIBBLE = 0xf0 ;
50
51
private static final int LOW_NIBBLE = 0x0f ;
51
- private static final char [] NIBBLE_TO_HEX =
52
- { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' };
52
+ private static final char [] NIBBLE_TO_HEX = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f' };
53
+
54
+ private static final int NIBBLE_TO_HEX_SHIFT_WIDTH = 4 ;
53
55
54
56
private final DigestAlgorithm algorithm ;
55
57
private final MessageDigest messageDigest ;
56
58
57
-
58
59
public FileDigestCalculator (final DigestAlgorithm algorithm ) {
59
60
this .algorithm = algorithm ;
60
61
this .messageDigest = this .algorithm .getInstance ();
@@ -72,12 +73,17 @@ public void process(final String file) {
72
73
try {
73
74
stream = new FileInputStream (file );
74
75
digest = bytesToHex (getDigest (stream , messageDigest ));
75
- } catch (IOException e ) {
76
+ }
77
+ catch (final IOException e ) {
76
78
throw new MetafactureException (e );
77
- } finally {
79
+ }
80
+ finally {
78
81
if (stream != null ) {
79
- try { stream .close (); }
80
- catch (final IOException e ) { }
82
+ try {
83
+ stream .close ();
84
+ }
85
+ catch (final IOException e ) {
86
+ }
81
87
}
82
88
}
83
89
getReceiver ().process (new Triple (file , algorithm .name (), digest ));
@@ -96,8 +102,8 @@ private static byte[] getDigest(final InputStream stream, final MessageDigest me
96
102
97
103
private static String bytesToHex (final byte [] bytes ) {
98
104
final char [] hex = new char [bytes .length * 2 ];
99
- for (int i = 0 ; i < bytes .length ; ++i ) {
100
- hex [i * 2 ] = NIBBLE_TO_HEX [(bytes [i ] & HIGH_NIBBLE ) >>> 4 ];
105
+ for (int i = 0 ; i < bytes .length ; ++i ) {
106
+ hex [i * 2 ] = NIBBLE_TO_HEX [(bytes [i ] & HIGH_NIBBLE ) >>> NIBBLE_TO_HEX_SHIFT_WIDTH ];
101
107
hex [i * 2 + 1 ] = NIBBLE_TO_HEX [bytes [i ] & LOW_NIBBLE ];
102
108
}
103
109
return new String (hex );
@@ -115,19 +121,20 @@ public enum DigestAlgorithm {
115
121
SHA1 ("SHA-1" ),
116
122
SHA256 ("SHA-256" ),
117
123
SHA384 ("SHA-384" ),
118
- SHA512 ("SHA-512" );
124
+ SHA512 ("SHA-512" );
119
125
120
126
private final String identifier ;
121
127
122
- private DigestAlgorithm (final String identifier ) {
128
+ DigestAlgorithm (final String identifier ) {
123
129
this .identifier = identifier ;
124
130
}
125
131
126
132
public MessageDigest getInstance () {
127
133
try {
128
134
return MessageDigest .getInstance (identifier );
129
- } catch (NoSuchAlgorithmException e ) {
130
- throw new MetafactureException (e );
135
+ }
136
+ catch (final NoSuchAlgorithmException e ) {
137
+ throw new MetafactureException (e );
131
138
}
132
139
}
133
140
0 commit comments