|
| 1 | +/* |
| 2 | + * Copyright 2013 Christoph Böhme |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.stream.pipe; |
| 17 | + |
| 18 | +import java.io.FileInputStream; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.security.MessageDigest; |
| 22 | + |
| 23 | + |
| 24 | +import org.culturegraph.mf.exceptions.MetafactureException; |
| 25 | +import org.culturegraph.mf.framework.DefaultObjectPipe; |
| 26 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 27 | +import org.culturegraph.mf.framework.annotations.Description; |
| 28 | +import org.culturegraph.mf.framework.annotations.In; |
| 29 | +import org.culturegraph.mf.framework.annotations.Out; |
| 30 | +import org.culturegraph.mf.stream.util.DigestAlgorithm; |
| 31 | +import org.culturegraph.mf.types.Triple; |
| 32 | + |
| 33 | +/** |
| 34 | + * Uses the input string as a file name and computes a cryptographic hash the file. |
| 35 | + * |
| 36 | + * @author Christoph Böhme |
| 37 | + * |
| 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) |
| 42 | +public final class FileDigestCalculator extends |
| 43 | + DefaultObjectPipe<String, ObjectReceiver<Triple>> { |
| 44 | + |
| 45 | + private static final int BUFFER_SIZE = 1024; |
| 46 | + |
| 47 | + private static final int HIGH_NIBBLE = 0xf0; |
| 48 | + private static final int LOW_NIBBLE = 0x0f; |
| 49 | + private static final char[] NIBBLE_TO_HEX = |
| 50 | + { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; |
| 51 | + |
| 52 | + private final DigestAlgorithm algorithm; |
| 53 | + private final MessageDigest messageDigest; |
| 54 | + |
| 55 | + |
| 56 | + public FileDigestCalculator(final DigestAlgorithm algorithm) { |
| 57 | + this.algorithm = algorithm; |
| 58 | + this.messageDigest = this.algorithm.getInstance(); |
| 59 | + } |
| 60 | + |
| 61 | + public FileDigestCalculator(final String algorithm) { |
| 62 | + this.algorithm = DigestAlgorithm.valueOf(algorithm.toUpperCase()); |
| 63 | + this.messageDigest = this.algorithm.getInstance(); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void process(final String file) { |
| 68 | + final String digest; |
| 69 | + InputStream stream = null; |
| 70 | + try { |
| 71 | + stream = new FileInputStream(file); |
| 72 | + digest = bytesToHex(getDigest(stream, messageDigest)); |
| 73 | + } catch (IOException e) { |
| 74 | + throw new MetafactureException(e); |
| 75 | + } finally { |
| 76 | + if (stream != null) { |
| 77 | + try { stream.close(); } |
| 78 | + catch (final IOException e) { } |
| 79 | + } |
| 80 | + } |
| 81 | + getReceiver().process(new Triple(file, algorithm.name(), digest)); |
| 82 | + } |
| 83 | + |
| 84 | + private static byte[] getDigest(final InputStream stream, final MessageDigest messageDigest) throws IOException { |
| 85 | + final byte[] buffer = new byte[BUFFER_SIZE]; |
| 86 | + |
| 87 | + int read = stream.read(buffer, 0, BUFFER_SIZE); |
| 88 | + while (read > -1) { |
| 89 | + messageDigest.update(buffer, 0, read); |
| 90 | + read = stream.read(buffer, 0, BUFFER_SIZE); |
| 91 | + } |
| 92 | + return messageDigest.digest(); |
| 93 | + } |
| 94 | + |
| 95 | + private static String bytesToHex(final byte[] bytes) { |
| 96 | + final char[] hex = new char[bytes.length * 2]; |
| 97 | + for (int i=0; i < bytes.length; ++i) { |
| 98 | + // NO CHECKSTYLE MagicNumber FOR 1 LINE: |
| 99 | + hex[i * 2] = NIBBLE_TO_HEX[(bytes[i] & HIGH_NIBBLE) >>> 4]; |
| 100 | + hex[i * 2 + 1] = NIBBLE_TO_HEX[bytes[i] & LOW_NIBBLE]; |
| 101 | + } |
| 102 | + return new String(hex); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments