1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.javadpkg.impl;
20
21 import java.io.BufferedWriter;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.security.MessageDigest;
25 import java.security.NoSuchAlgorithmException;
26 import java.util.List;
27
28 import net.sourceforge.javadpkg.BuildException;
29 import net.sourceforge.javadpkg.GlobalConstants;
30 import net.sourceforge.javadpkg.MD5SumsBuilder;
31 import net.sourceforge.javadpkg.io.DataTarget;
32 import net.sourceforge.javadpkg.store.DataStore;
33 import net.sourceforge.javadpkg.store.FileHash;
34
35
36
37
38
39
40
41
42
43
44
45 public class MD5SumsBuilderImpl implements MD5SumsBuilder, GlobalConstants {
46
47
48
49
50
51
52
53 public MD5SumsBuilderImpl() {
54 super();
55 }
56
57
58 @Override
59 public void buildMD5Sums(DataStore store, DataTarget target) throws IOException, BuildException {
60 MessageDigest digest;
61 List<FileHash> fileHashes;
62 String path;
63
64
65 if (store == null)
66 throw new IllegalArgumentException("Argument store is null.");
67 if (target == null)
68 throw new IllegalArgumentException("Argument target is null.");
69
70
71 try {
72 digest = MessageDigest.getInstance("MD5");
73 } catch (NoSuchAlgorithmException e) {
74 throw new BuildException("Couldn't build MD5 sums: MD5 is not supported: " + e.getMessage(), e);
75 }
76
77
78 try {
79 fileHashes = store.createFileHashes(digest);
80 } catch (IOException e) {
81 throw new IOException("Couldn't build MD5 sums: Couldn't create hashes: " + e.getMessage(), e);
82 }
83
84
85 try {
86 try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(target.getOutputStream(), UTF_8_CHARSET))) {
87 for (FileHash fileHash : fileHashes) {
88 out.write(fileHash.getHashAsHex());
89 out.write(" ");
90
91 path = fileHash.getPath();
92 if (path.startsWith("/")) {
93 path = path.substring(1);
94 }
95 out.write(path);
96 out.write("\n");
97 }
98 }
99 } catch (IOException e) {
100 throw new IOException("Couldn't build MD5 sums: Couldn't write MD5 sums: " + e.getMessage(), e);
101 }
102 }
103
104
105 }