Skip to content

Commit f8c39cf

Browse files
committed
Handle multiple arguments
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1744721 13f79535-47bb-0310-9956-ffa450edef68
1 parent a7c03cb commit f8c39cf

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

src/main/java/org/apache/commons/codec/cli/Digest.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Digest {
4646
* @param args
4747
* {@code args[0]} is one of {@link MessageDigestAlgorithms} name, {@link MessageDigest} name, {@code ALL}
4848
* , or {@code *}.
49-
* {@code args[1]} is a FILE.
49+
* {@code args[1+]} is a FILE/DIRECTORY/String.
5050
* @throws IOException
5151
*/
5252
public static void main(String[] args) throws IOException {
@@ -55,19 +55,24 @@ public static void main(String[] args) throws IOException {
5555

5656
private final String algorithm;
5757
private final String[] args;
58-
private final String source;
58+
private final String[] inputs;
5959

6060
private Digest(final String[] args) {
6161
if (args == null) {
6262
throw new IllegalArgumentException("args");
6363
}
6464
if (args.length == 0) {
6565
throw new IllegalArgumentException(
66-
String.format("Usage: java %s [algorithm] [FILE|DIRECTORY|string]", Digest.class.getName()));
66+
String.format("Usage: java %s [algorithm] [FILE|DIRECTORY|string] ...", Digest.class.getName()));
6767
}
6868
this.args = args;
6969
algorithm = args[0];
70-
source = args.length == 1 ? null : args[1];
70+
if (args.length <= 1) {
71+
inputs = null;
72+
} else {
73+
inputs = new String[args.length -1];
74+
System.arraycopy(args, 1, inputs, 0, inputs.length);
75+
}
7176
}
7277

7378
private void println(final String prefix, final byte[] digest) {
@@ -105,19 +110,21 @@ private void run(String[] digestAlgorithms) throws IOException {
105110
}
106111

107112
private void run(String prefix, final MessageDigest messageDigest) throws IOException {
108-
if (source == null) {
113+
if (inputs == null) {
109114
println(prefix, DigestUtils.digest(messageDigest, System.in));
110115
return;
111116
}
112-
final File file = new File(source);
113-
if (file.isFile()) {
114-
println(prefix, DigestUtils.digest(messageDigest, file), source);
115-
} else if (file.isDirectory()) {
116-
run(prefix, messageDigest, file.listFiles());
117-
} else {
118-
// use the default charset for the command-line parameter
119-
final byte[] bytes = source.getBytes(Charset.defaultCharset());
120-
println(prefix, DigestUtils.digest(messageDigest, bytes));
117+
for(String source : inputs) {
118+
final File file = new File(source);
119+
if (file.isFile()) {
120+
println(prefix, DigestUtils.digest(messageDigest, file), source);
121+
} else if (file.isDirectory()) {
122+
run(prefix, messageDigest, file.listFiles());
123+
} else {
124+
// use the default charset for the command-line parameter
125+
final byte[] bytes = source.getBytes(Charset.defaultCharset());
126+
println(prefix, DigestUtils.digest(messageDigest, bytes));
127+
}
121128
}
122129
}
123130

0 commit comments

Comments
 (0)