Source Code Cross Referenced for EncodingUtil.java in  » Web-Framework » aranea-mvc-1.1.1 » org » araneaframework » http » util » 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 » Web Framework » aranea mvc 1.1.1 » org.araneaframework.http.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Copyright 2006 Webmedia Group Ltd.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *  http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         **/package org.araneaframework.http.util;
016:
017:        import java.io.ByteArrayInputStream;
018:        import java.io.ByteArrayOutputStream;
019:        import java.io.InputStream;
020:        import java.io.ObjectInputStream;
021:        import java.io.ObjectOutputStream;
022:        import java.io.OutputStream;
023:        import java.io.Serializable;
024:        import java.security.MessageDigest;
025:        import java.util.zip.GZIPInputStream;
026:        import java.util.zip.GZIPOutputStream;
027:        import net.iharder.base64.Base64;
028:
029:        /**
030:         * Provides base64 encoding, decoding methods, generating a digest and checking the digest methods.
031:         *  
032:         * @author "Toomas Römer" <toomas@webmedia.ee>
033:         */
034:        public abstract class EncodingUtil {
035:            /**
036:             * The method serializes the object, optionally compresses it and base64 encodes. 
037:             * GZip is used for the optional compression.
038:             * 
039:             * The method uses streams for the different operations and the underlying base64
040:             * algorithm is from iHarder.net base64 utilities..
041:             * 
042:             * @param object that will be encoded.
043:             * @param compress if set, the serialized object will be compressed.
044:             * @return A String represtation of the object in base64 encoded format.
045:             * @throws Exception
046:             */
047:            public static String encodeObjectBase64(Serializable object,
048:                    boolean compress) throws Exception {
049:                ByteArrayOutputStream baos = null;
050:                Base64.OutputStream b64os = null;
051:                ObjectOutputStream oos = null;
052:                try {
053:                    baos = new ByteArrayOutputStream();
054:                    b64os = new Base64.OutputStream(baos, Base64.ENCODE
055:                            | Base64.DONT_BREAK_LINES);
056:                    OutputStream os = compress ? ((OutputStream) new GZIPOutputStream(
057:                            b64os))
058:                            : b64os;
059:                    oos = new ObjectOutputStream(os);
060:
061:                    oos.writeObject(object);
062:
063:                    oos.flush();
064:                } finally {
065:                    oos.close();
066:                }
067:
068:                return new String(baos.toByteArray(), "UTF-8");
069:            }
070:
071:            /**
072:             * Base64 decodes the value, optionally decompresses it and then deserializes it. For
073:             * the decompression GZip is expected.
074:             * 
075:             * @param value is the base64 encoded data of the optionally compressed serialized object. 
076:             * @param compress determines if after the decoding gzip decompression should be applied.
077:             * @return an Object that is deserialized object of the decoded optionally decompressed value. 
078:             * @throws Exception
079:             */
080:            public static Object decodeObjectBase64(String value,
081:                    boolean compress) throws Exception {
082:                ByteArrayInputStream bais = new ByteArrayInputStream(value
083:                        .getBytes("UTF-8"));
084:                Base64.InputStream b64is = new Base64.InputStream(bais,
085:                        Base64.DECODE | Base64.DONT_BREAK_LINES);
086:                InputStream is = compress ? ((InputStream) new GZIPInputStream(
087:                        b64is)) : b64is;
088:                ObjectInputStream ois = new ObjectInputStream(is);
089:
090:                return ois.readObject();
091:            }
092:
093:            /**
094:             * Builds a digest of the byte array, using the SHA hash function.
095:             * 
096:             * @param data the byte array that's digest we are building.
097:             * @return A byte[] array representing the SHA hash.
098:             * @throws Exception
099:             */
100:            public static byte[] buildDigest(byte[] data) throws Exception {
101:                MessageDigest dgst = MessageDigest.getInstance("SHA");
102:                dgst.update(data);
103:                return dgst.digest();
104:            }
105:
106:            /**
107:             * Returns true, if the digest of the value equals to the given digest.
108:             * 
109:             * @param value byte array that's digest we are comparing. 
110:             * @param digest the allready generated digest we are comparing against.
111:             * @return true if the digest of the value equals digest, otherwise false.
112:             * @throws Exception
113:             */
114:            public static boolean checkDigest(byte[] value, byte[] digest)
115:                    throws Exception {
116:                return MessageDigest.isEqual(buildDigest(value), digest);
117:            }
118:
119:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.