Skip to content

Commit cfef405

Browse files
committed
metafacture-files/ (main): Fix Checkstyle violations.
1 parent 713c413 commit cfef405

File tree

2 files changed

+44
-31
lines changed

2 files changed

+44
-31
lines changed

metafacture-files/src/main/java/org/metafacture/files/DirReader.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.metafacture.files;
1716

18-
import java.io.File;
19-
import java.io.FilenameFilter;
20-
import java.util.Arrays;
17+
package org.metafacture.files;
2118

2219
import org.metafacture.framework.FluxCommand;
2320
import org.metafacture.framework.ObjectReceiver;
@@ -26,6 +23,10 @@
2623
import org.metafacture.framework.annotations.Out;
2724
import org.metafacture.framework.helpers.DefaultObjectPipe;
2825

26+
import java.io.File;
27+
import java.io.FilenameFilter;
28+
import java.util.Arrays;
29+
2930
/**
3031
* Reads a directory and emits all filenames found.
3132
*
@@ -40,42 +41,47 @@ public final class DirReader extends DefaultObjectPipe<String, ObjectReceiver<St
4041

4142
private boolean recursive;
4243

43-
private String filenameFilterPattern = null;
44+
private String filenameFilterPattern;
45+
46+
public DirReader() {
47+
}
4448

4549
public void setRecursive(final boolean recursive) {
4650
this.recursive = recursive;
4751
}
4852

49-
public void setFilenamePattern(final String filenameFilterPattern) {
50-
this.filenameFilterPattern = filenameFilterPattern;
53+
public void setFilenamePattern(final String newFilenameFilterPattern) {
54+
filenameFilterPattern = newFilenameFilterPattern;
5155
}
5256

5357
@Override
5458
public void process(final String dir) {
5559
final File file = new File(dir);
5660
if (file.isDirectory()) {
5761
dir(file);
58-
} else {
62+
}
63+
else {
5964
getReceiver().process(dir);
6065
}
6166
}
6267

6368
private void dir(final File dir) {
6469
final ObjectReceiver<String> receiver = getReceiver();
65-
final File[] files = filenameFilterPattern == null ? dir.listFiles()
66-
: dir.listFiles(new FilenameFilter() {
70+
final File[] files = filenameFilterPattern == null ? dir.listFiles() :
71+
dir.listFiles(new FilenameFilter() {
6772
@Override
6873
public boolean accept(final File dir, final String name) {
6974
return name.matches(filenameFilterPattern);
7075
}
7176
});
7277
Arrays.sort(files);
73-
for (File file : files) {
78+
for (final File file : files) {
7479
if (file.isDirectory()) {
7580
if (recursive) {
7681
dir(file);
7782
}
78-
} else {
83+
}
84+
else {
7985
receiver.process(file.getAbsolutePath());
8086
}
8187
}

metafacture-files/src/main/java/org/metafacture/files/FileDigestCalculator.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.metafacture.files;
1716

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;
2318

2419
import org.metafacture.framework.FluxCommand;
2520
import org.metafacture.framework.MetafactureException;
@@ -30,6 +25,12 @@
3025
import org.metafacture.framework.helpers.DefaultObjectPipe;
3126
import org.metafacture.framework.objects.Triple;
3227

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+
3334
/**
3435
* Interprets the input string as a file name and computes a cryptographic hash
3536
* for the file.
@@ -48,13 +49,13 @@ public final class FileDigestCalculator extends
4849

4950
private static final int HIGH_NIBBLE = 0xf0;
5051
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;
5355

5456
private final DigestAlgorithm algorithm;
5557
private final MessageDigest messageDigest;
5658

57-
5859
public FileDigestCalculator(final DigestAlgorithm algorithm) {
5960
this.algorithm = algorithm;
6061
this.messageDigest = this.algorithm.getInstance();
@@ -72,12 +73,17 @@ public void process(final String file) {
7273
try {
7374
stream = new FileInputStream(file);
7475
digest = bytesToHex(getDigest(stream, messageDigest));
75-
} catch (IOException e) {
76+
}
77+
catch (final IOException e) {
7678
throw new MetafactureException(e);
77-
} finally {
79+
}
80+
finally {
7881
if (stream != null) {
79-
try { stream.close(); }
80-
catch (final IOException e) { }
82+
try {
83+
stream.close();
84+
}
85+
catch (final IOException e) {
86+
}
8187
}
8288
}
8389
getReceiver().process(new Triple(file, algorithm.name(), digest));
@@ -96,8 +102,8 @@ private static byte[] getDigest(final InputStream stream, final MessageDigest me
96102

97103
private static String bytesToHex(final byte[] bytes) {
98104
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];
101107
hex[i * 2 + 1] = NIBBLE_TO_HEX[bytes[i] & LOW_NIBBLE];
102108
}
103109
return new String(hex);
@@ -115,19 +121,20 @@ public enum DigestAlgorithm {
115121
SHA1("SHA-1"),
116122
SHA256("SHA-256"),
117123
SHA384("SHA-384"),
118-
SHA512 ("SHA-512");
124+
SHA512("SHA-512");
119125

120126
private final String identifier;
121127

122-
private DigestAlgorithm(final String identifier) {
128+
DigestAlgorithm(final String identifier) {
123129
this.identifier = identifier;
124130
}
125131

126132
public MessageDigest getInstance() {
127133
try {
128134
return MessageDigest.getInstance(identifier);
129-
} catch (NoSuchAlgorithmException e) {
130-
throw new MetafactureException (e);
135+
}
136+
catch (final NoSuchAlgorithmException e) {
137+
throw new MetafactureException(e);
131138
}
132139
}
133140

0 commit comments

Comments
 (0)