Source Code Cross Referenced for SerializationUtils.java in  » Library » Apache-common-lang » org » apache » commons » lang » 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 » Library » Apache common lang » org.apache.commons.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.lang;
018:
019:        import java.io.ByteArrayInputStream;
020:        import java.io.ByteArrayOutputStream;
021:        import java.io.IOException;
022:        import java.io.InputStream;
023:        import java.io.ObjectInputStream;
024:        import java.io.ObjectOutputStream;
025:        import java.io.OutputStream;
026:        import java.io.Serializable;
027:
028:        /**
029:         * <p>Assists with the serialization process and performs additional functionality based 
030:         * on serialization.</p>
031:         * <p>
032:         * <ul>
033:         * <li>Deep clone using serialization
034:         * <li>Serialize managing finally and IOException
035:         * <li>Deserialize managing finally and IOException
036:         * </ul>
037:         *
038:         * <p>This class throws exceptions for invalid <code>null</code> inputs.
039:         * Each method documents its behaviour in more detail.</p>
040:         *
041:         * @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
042:         * @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
043:         * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
044:         * @author Stephen Colebourne
045:         * @author Jeff Varszegi
046:         * @author Gary Gregory
047:         * @since 1.0
048:         * @version $Id: SerializationUtils.java 437554 2006-08-28 06:21:41Z bayard $
049:         */
050:        public class SerializationUtils {
051:
052:            /**
053:             * <p>SerializationUtils instances should NOT be constructed in standard programming.
054:             * Instead, the class should be used as <code>SerializationUtils.clone(object)</code>.</p>
055:             *
056:             * <p>This constructor is public to permit tools that require a JavaBean instance
057:             * to operate.</p>
058:             * @since 2.0
059:             */
060:            public SerializationUtils() {
061:                super ();
062:            }
063:
064:            // Clone
065:            //-----------------------------------------------------------------------
066:            /**
067:             * <p>Deep clone an <code>Object</code> using serialization.</p>
068:             *
069:             * <p>This is many times slower than writing clone methods by hand
070:             * on all objects in your object graph. However, for complex object
071:             * graphs, or for those that don't support deep cloning this can
072:             * be a simple alternative implementation. Of course all the objects
073:             * must be <code>Serializable</code>.</p>
074:             * 
075:             * @param object  the <code>Serializable</code> object to clone
076:             * @return the cloned object
077:             * @throws SerializationException (runtime) if the serialization fails
078:             */
079:            public static Object clone(Serializable object) {
080:                return deserialize(serialize(object));
081:            }
082:
083:            // Serialize
084:            //-----------------------------------------------------------------------
085:            /**
086:             * <p>Serializes an <code>Object</code> to the specified stream.</p>
087:             *
088:             * <p>The stream will be closed once the object is written.
089:             * This avoids the need for a finally clause, and maybe also exception
090:             * handling, in the application code.</p>
091:             * 
092:             * <p>The stream passed in is not buffered internally within this method.
093:             * This is the responsibility of your application if desired.</p>
094:             *
095:             * @param obj  the object to serialize to bytes, may be null
096:             * @param outputStream  the stream to write to, must not be null
097:             * @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
098:             * @throws SerializationException (runtime) if the serialization fails
099:             */
100:            public static void serialize(Serializable obj,
101:                    OutputStream outputStream) {
102:                if (outputStream == null) {
103:                    throw new IllegalArgumentException(
104:                            "The OutputStream must not be null");
105:                }
106:                ObjectOutputStream out = null;
107:                try {
108:                    // stream closed in the finally
109:                    out = new ObjectOutputStream(outputStream);
110:                    out.writeObject(obj);
111:
112:                } catch (IOException ex) {
113:                    throw new SerializationException(ex);
114:                } finally {
115:                    try {
116:                        if (out != null) {
117:                            out.close();
118:                        }
119:                    } catch (IOException ex) {
120:                        // ignore close exception
121:                    }
122:                }
123:            }
124:
125:            /**
126:             * <p>Serializes an <code>Object</code> to a byte array for
127:             * storage/serialization.</p>
128:             *
129:             * @param obj  the object to serialize to bytes
130:             * @return a byte[] with the converted Serializable
131:             * @throws SerializationException (runtime) if the serialization fails
132:             */
133:            public static byte[] serialize(Serializable obj) {
134:                ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
135:                serialize(obj, baos);
136:                return baos.toByteArray();
137:            }
138:
139:            // Deserialize
140:            //-----------------------------------------------------------------------
141:            /**
142:             * <p>Deserializes an <code>Object</code> from the specified stream.</p>
143:             *
144:             * <p>The stream will be closed once the object is written. This
145:             * avoids the need for a finally clause, and maybe also exception
146:             * handling, in the application code.</p>
147:             * 
148:             * <p>The stream passed in is not buffered internally within this method.
149:             * This is the responsibility of your application if desired.</p>
150:             *
151:             * @param inputStream  the serialized object input stream, must not be null
152:             * @return the deserialized object
153:             * @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
154:             * @throws SerializationException (runtime) if the serialization fails
155:             */
156:            public static Object deserialize(InputStream inputStream) {
157:                if (inputStream == null) {
158:                    throw new IllegalArgumentException(
159:                            "The InputStream must not be null");
160:                }
161:                ObjectInputStream in = null;
162:                try {
163:                    // stream closed in the finally
164:                    in = new ObjectInputStream(inputStream);
165:                    return in.readObject();
166:
167:                } catch (ClassNotFoundException ex) {
168:                    throw new SerializationException(ex);
169:                } catch (IOException ex) {
170:                    throw new SerializationException(ex);
171:                } finally {
172:                    try {
173:                        if (in != null) {
174:                            in.close();
175:                        }
176:                    } catch (IOException ex) {
177:                        // ignore close exception
178:                    }
179:                }
180:            }
181:
182:            /**
183:             * <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
184:             *
185:             * @param objectData  the serialized object, must not be null
186:             * @return the deserialized object
187:             * @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
188:             * @throws SerializationException (runtime) if the serialization fails
189:             */
190:            public static Object deserialize(byte[] objectData) {
191:                if (objectData == null) {
192:                    throw new IllegalArgumentException(
193:                            "The byte[] must not be null");
194:                }
195:                ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
196:                return deserialize(bais);
197:            }
198:
199:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.