Source Code Cross Referenced for ChecksumUtils.java in  » Content-Management-System » dspace » org » purl » sword » base » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » dspace » org.purl.sword.base 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright (c) 2007, Aberystwyth University
003:         *
004:         * All rights reserved.
005:         * 
006:         * Redistribution and use in source and binary forms, with or without 
007:         * modification, are permitted provided that the following conditions 
008:         * are met:
009:         * 
010:         *  - Redistributions of source code must retain the above 
011:         *    copyright notice, this list of conditions and the 
012:         *    following disclaimer.
013:         *  
014:         *  - Redistributions in binary form must reproduce the above copyright 
015:         *    notice, this list of conditions and the following disclaimer in 
016:         *    the documentation and/or other materials provided with the 
017:         *    distribution.
018:         *    
019:         *  - Neither the name of the Centre for Advanced Software and 
020:         *    Intelligent Systems (CASIS) nor the names of its 
021:         *    contributors may be used to endorse or promote products derived 
022:         *    from this software without specific prior written permission.
023:         * 
024:         * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
025:         * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
026:         * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
027:         * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
028:         * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
029:         * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
030:         * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
031:         * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 
032:         * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
033:         * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
034:         * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
035:         * SUCH DAMAGE.
036:         */package org.purl.sword.base;
037:
038:        import java.io.FileInputStream;
039:        import java.io.IOException;
040:        import java.io.InputStream;
041:        import java.security.MessageDigest;
042:        import java.security.NoSuchAlgorithmException;
043:
044:        /**
045:         * Utility class that holds Checksum related methods. 
046:         * 
047:         * @author Neil Taylor
048:         */
049:        public class ChecksumUtils {
050:            /**
051:             * Generate an MD5 hash for the file that is specified in the 
052:             * filepath. The hash is returned as a String representation. 
053:             * 
054:             * @param filepath The path to the file to load. 
055:             * @return A string hash of the file. 
056:             * @throws NoSuchAlgorithmException If the MD5 algorithm is 
057:             * not supported by the installed virtual machine. 
058:             * 
059:             * @throws IOException If there is an error accessing the file. 
060:             */
061:            public static String generateMD5(String filepath)
062:                    throws NoSuchAlgorithmException, IOException {
063:                return generateMD5(new FileInputStream(filepath));
064:            }
065:
066:            /**
067:             * Generate an MD5 hash for the file that is specified in the 
068:             * filepath. The hash is returned as a String representation. 
069:             * 
070:             * @param md5Stream The InputStream to checksum. 
071:             * @return A string hash of the file. 
072:             * @throws NoSuchAlgorithmException If the MD5 algorithm is 
073:             * not supported by the installed virtual machine. 
074:             * 
075:             * @throws IOException If there is an error accessing the file. 
076:             */
077:            public static String generateMD5(InputStream md5Stream)
078:                    throws NoSuchAlgorithmException, IOException {
079:                String md5 = null;
080:
081:                try {
082:                    MessageDigest md = MessageDigest.getInstance("MD5");
083:                    md.reset();
084:
085:                    byte[] bytes = new byte[1024];
086:                    int count = 0;
087:                    while ((count = md5Stream.read(bytes)) != -1) {
088:                        md.update(bytes, 0, count);
089:                    }
090:
091:                    byte[] md5Digest = md.digest();
092:
093:                    StringBuffer buffer = new StringBuffer();
094:                    for (byte b : md5Digest) {
095:                        // 0xFF is used to handle the issue of negative numbers in the bytes
096:                        String hex = Integer.toHexString(b & 0xFF);
097:                        if (hex.length() == 1) {
098:                            buffer.append("0");
099:                        }
100:                        buffer.append(hex);
101:                    }
102:
103:                    md5 = buffer.toString();
104:                } catch (NoSuchAlgorithmException ex) {
105:                    InfoLogger.getLogger().writeInfo("Error accessing string");
106:                    throw ex; // rethrow 
107:                } finally {
108:                    if (md5Stream != null) {
109:                        md5Stream.close();
110:                    }
111:                }
112:
113:                return md5;
114:            }
115:
116:            /**
117:             * Generate an MD5 hash for the file that is specified in the 
118:             * filepath. The hash is returned as a String representation. 
119:             * 
120:             * @param bytes The byte array to checksum. 
121:             * @return A string hash of the file. 
122:             * @throws NoSuchAlgorithmException If the MD5 algorithm is 
123:             * not supported by the installed virtual machine. 
124:             * 
125:             * @throws IOException If there is an error accessing the file. 
126:             */
127:            public static String generateMD5(byte[] bytes)
128:                    throws NoSuchAlgorithmException, IOException {
129:                String md5 = null;
130:
131:                try {
132:                    MessageDigest md = MessageDigest.getInstance("MD5");
133:                    md.reset();
134:
135:                    md.update(bytes);
136:
137:                    byte[] md5Digest = md.digest();
138:
139:                    StringBuffer buffer = new StringBuffer();
140:                    for (byte b : md5Digest) {
141:                        // 0xFF is used to handle the issue of negative numbers in the bytes
142:                        String hex = Integer.toHexString(b & 0xFF);
143:                        if (hex.length() == 1) {
144:                            buffer.append("0");
145:                        }
146:                        buffer.append(hex);
147:                    }
148:
149:                    md5 = buffer.toString();
150:                } catch (NoSuchAlgorithmException ex) {
151:                    InfoLogger.getLogger().writeInfo("Error accessing string");
152:                    throw ex; // rethrow 
153:                }
154:
155:                return md5;
156:            }
157:
158:            public static void main(String[] args)
159:                    throws NoSuchAlgorithmException, IOException {
160:                System.out.println(ChecksumUtils.generateMD5(args[0]));
161:            }
162:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.