Source Code Cross Referenced for SerializableUtils.java in  » Database-JDBC-Connection-Pool » c3p0 » com » mchange » v2 » ser » 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 » Database JDBC Connection Pool » c3p0 » com.mchange.v2.ser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Distributed as part of c3p0 v.0.9.1.2
003:         *
004:         * Copyright (C) 2005 Machinery For Change, Inc.
005:         *
006:         * Author: Steve Waldman <swaldman@mchange.com>
007:         *
008:         * This library is free software; you can redistribute it and/or modify
009:         * it under the terms of the GNU Lesser General Public License version 2.1, as 
010:         * published by the Free Software Foundation.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015:         * GNU Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public License
018:         * along with this software; see the file LICENSE.  If not, write to the
019:         * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020:         * Boston, MA 02111-1307, USA.
021:         */
022:
023:        package com.mchange.v2.ser;
024:
025:        import java.io.*;
026:        import com.mchange.v1.io.*;
027:        import com.mchange.v2.log.*;
028:
029:        public final class SerializableUtils {
030:            final static MLogger logger = MLog
031:                    .getLogger(SerializableUtils.class);
032:
033:            private SerializableUtils() {
034:            }
035:
036:            public static byte[] toByteArray(Object obj)
037:                    throws NotSerializableException {
038:                return serializeToByteArray(obj);
039:            }
040:
041:            public static byte[] toByteArray(Object obj, Indirector indirector,
042:                    IndirectPolicy policy) throws NotSerializableException {
043:                try {
044:                    if (policy == IndirectPolicy.DEFINITELY_INDIRECT) {
045:                        if (indirector == null)
046:                            throw new IllegalArgumentException(
047:                                    "null indirector is not consistent with "
048:                                            + policy);
049:
050:                        IndirectlySerialized indirect = indirector
051:                                .indirectForm(obj);
052:                        return toByteArray(indirect);
053:                    } else if (policy == IndirectPolicy.INDIRECT_ON_EXCEPTION) {
054:                        if (indirector == null)
055:                            throw new IllegalArgumentException(
056:                                    "null indirector is not consistent with "
057:                                            + policy);
058:
059:                        try {
060:                            return toByteArray(obj);
061:                        } catch (NotSerializableException e) {
062:                            return toByteArray(obj, indirector,
063:                                    IndirectPolicy.DEFINITELY_INDIRECT);
064:                        }
065:                    } else if (policy == IndirectPolicy.DEFINITELY_DIRECT)
066:                        return toByteArray(obj);
067:                    else
068:                        throw new InternalError("unknown indirecting policy: "
069:                                + policy);
070:                } catch (NotSerializableException e) {
071:                    throw e;
072:                } catch (Exception e) {
073:                    //e.printStackTrace();
074:                    if (logger.isLoggable(MLevel.WARNING))
075:                        logger
076:                                .log(
077:                                        MLevel.WARNING,
078:                                        "An Exception occurred while serializing an Object to a byte[] with an Indirector.",
079:                                        e);
080:                    throw new NotSerializableException(e.toString());
081:                }
082:            }
083:
084:            /**
085:             * @deprecated use SerialializableUtils.toByteArray() [shorter name is better!]
086:             */
087:            public static byte[] serializeToByteArray(Object obj)
088:                    throws NotSerializableException {
089:                try {
090:                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
091:                    ObjectOutputStream out = new ObjectOutputStream(baos);
092:                    out.writeObject(obj);
093:                    return baos.toByteArray();
094:                } catch (NotSerializableException e) {
095:                    //this is the only IOException that 
096:                    //shouldn't signal a bizarre error...
097:                    e.fillInStackTrace();
098:                    throw e;
099:                } catch (IOException e) {
100:                    //e.printStackTrace();
101:                    if (logger.isLoggable(MLevel.SEVERE))
102:                        logger
103:                                .log(
104:                                        MLevel.SEVERE,
105:                                        "An IOException occurred while writing into a ByteArrayOutputStream?!?",
106:                                        e);
107:                    throw new Error("IOException writing to a byte array!");
108:                }
109:            }
110:
111:            /**
112:             * By default, unwraps IndirectlySerialized objects, returning the original
113:             */
114:            public static Object fromByteArray(byte[] bytes)
115:                    throws IOException, ClassNotFoundException {
116:                Object out = deserializeFromByteArray(bytes);
117:                if (out instanceof  IndirectlySerialized)
118:                    return ((IndirectlySerialized) out).getObject();
119:                else
120:                    return out;
121:            }
122:
123:            public static Object fromByteArray(byte[] bytes,
124:                    boolean ignore_indirects) throws IOException,
125:                    ClassNotFoundException {
126:                if (ignore_indirects)
127:                    return deserializeFromByteArray(bytes);
128:                else
129:                    return fromByteArray(bytes);
130:            }
131:
132:            /**
133:             * @deprecated use SerialializableUtils.fromByteArray() [shorter name is better!]
134:             */
135:            public static Object deserializeFromByteArray(byte[] bytes)
136:                    throws IOException, ClassNotFoundException {
137:                ObjectInputStream in = new ObjectInputStream(
138:                        new ByteArrayInputStream(bytes));
139:                return in.readObject();
140:            }
141:
142:            public static Object testSerializeDeserialize(Object o)
143:                    throws IOException, ClassNotFoundException {
144:                return deepCopy(o);
145:            }
146:
147:            public static Object deepCopy(Object o) throws IOException,
148:                    ClassNotFoundException {
149:                byte[] bytes = serializeToByteArray(o);
150:                return deserializeFromByteArray(bytes);
151:            }
152:
153:            public final static Object unmarshallObjectFromFile(File file)
154:                    throws IOException, ClassNotFoundException {
155:                ObjectInputStream in = null;
156:                try {
157:                    in = new ObjectInputStream(new BufferedInputStream(
158:                            new FileInputStream(file)));
159:                    return in.readObject();
160:                } finally {
161:                    InputStreamUtils.attemptClose(in);
162:                }
163:            }
164:
165:            public final static void marshallObjectToFile(Object o, File file)
166:                    throws IOException {
167:                ObjectOutputStream out = null;
168:                try {
169:                    out = new ObjectOutputStream(new BufferedOutputStream(
170:                            new FileOutputStream(file)));
171:                    out.writeObject(o);
172:                } finally {
173:                    OutputStreamUtils.attemptClose(out);
174:                }
175:            }
176:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.