View Javadoc
1   /*
2    * dpkg - Debian Package library and the Debian Package Maven plugin
3    * (c) Copyright 2016 Gerrit Hohl
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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   * <p>
38   * A {@link MD5SumsBuilder} implementation.
39   * </p>
40   *
41   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
42   * @version <b>1.0</b>, 26.04.2016 by Gerrit Hohl
43   * @version <b>1.1</b>, 23.01.2018 by Gerrit Hohl
44   */
45  public class MD5SumsBuilderImpl implements MD5SumsBuilder, GlobalConstants {
46  	
47  	
48  	/**
49  	 * <p>
50  	 * Creates a builder.
51  	 * </p>
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  		// --- Create the digest ---
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  		// --- Create hashes ---
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  		// --- Write hashes ---
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 }