Source Code Cross Referenced for Serializables.java in  » Ajax » zk » org » zkoss » io » 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 » Ajax » zk » org.zkoss.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* Serializables.java
002:
003:        {{IS_NOTE
004:        	Purpose:
005:        		
006:        	Description:
007:        		
008:        	History:
009:        		Sun Jun 25 22:54:45     2006, Created by tomyeh
010:        }}IS_NOTE
011:
012:        Copyright (C) 2006 Potix Corporation. All Rights Reserved.
013:
014:        {{IS_RIGHT
015:        	This program is distributed under GPL Version 2.0 in the hope that
016:        	it will be useful, but WITHOUT ANY WARRANTY.
017:        }}IS_RIGHT
018:         */
019:        package org.zkoss.io;
020:
021:        import java.util.Map;
022:        import java.util.HashMap;
023:        import java.util.Collection;
024:        import java.util.LinkedList;
025:        import java.util.Iterator;
026:        import java.io.Serializable;
027:        import java.io.Externalizable;
028:        import java.io.ObjectOutputStream;
029:        import java.io.ObjectInputStream;
030:        import java.io.IOException;
031:
032:        /**
033:         * Utilities to handle java.io.Serializable.
034:         *
035:         * @author tomyeh
036:         */
037:        public class Serializables {
038:            private Serializables() {
039:            }
040:
041:            /** Writes only serializable entries of the specified map.
042:             * Non-serializable attributes are ignored.
043:             */
044:            public static void smartWrite(ObjectOutputStream s, Map map)
045:                    throws IOException {
046:                if (map != null) {
047:                    for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
048:                        final Map.Entry me = (Map.Entry) it.next();
049:                        final Object nm = me.getKey();
050:                        final Object val = me.getValue();
051:                        if (((nm instanceof  Serializable) || (nm instanceof  Externalizable))
052:                                && (val == null
053:                                        || (val instanceof  Serializable) || (val instanceof  Externalizable))) {
054:                            s.writeObject(nm);
055:                            s.writeObject(val);
056:                        }
057:                    }
058:                }
059:                s.writeObject(null); //denote end-of-map
060:            }
061:
062:            /** Reads serializable entries back (serialized by {@link #smartWrite(ObjectOutputStream,Map)}).
063:             *
064:             * @param map the map to hold the data being read. If null and any data
065:             * is read, a new map (HashMap) is created and returned.
066:             * @return the map being read
067:             */
068:            public static Map smartRead(ObjectInputStream s, Map map)
069:                    throws IOException, ClassNotFoundException {
070:                for (;;) {
071:                    final Object nm = s.readObject();
072:                    if (nm == null)
073:                        break; //no more
074:
075:                    if (map == null)
076:                        map = new HashMap();
077:                    map.put(nm, s.readObject());
078:                }
079:                return map;
080:            }
081:
082:            /** Writes only serializable elements of the specified collection.
083:             */
084:            public static void smartWrite(ObjectOutputStream s, Collection col)
085:                    throws IOException {
086:                if (col != null) {
087:                    for (Iterator it = col.iterator(); it.hasNext();) {
088:                        final Object val = it.next();
089:                        if ((val instanceof  Serializable)
090:                                || (val instanceof  Externalizable)) {
091:                            s.writeObject(val);
092:                        }
093:                    }
094:                }
095:                s.writeObject(null);
096:            }
097:
098:            /** Reads serializable elements back (serialized by {@link #smartWrite(ObjectOutputStream,Collection)})
099:             *
100:             * @param col the collection to hold the data beinig read. If null and
101:             * and data is read, a new collection (LinkedList) is created and returned.
102:             * @return the collection being read
103:             */
104:            public static Collection smartRead(ObjectInputStream s,
105:                    Collection col) throws IOException, ClassNotFoundException {
106:                for (;;) {
107:                    final Object val = s.readObject();
108:                    if (val == null)
109:                        break; //no more
110:
111:                    if (col == null)
112:                        col = new LinkedList();
113:                    col.add(val);
114:                }
115:                return col;
116:            }
117:
118:            /** Writes only serializable elements of the specified array.
119:             * <p>To read back, use {@link #smartRead(ObjectInputStream, Collection)}.
120:             * @since 3.0.0
121:             */
122:            public static void smartWrite(ObjectOutputStream s, Object[] ary)
123:                    throws IOException {
124:                if (ary != null) {
125:                    for (int j = 0; j < ary.length; ++j) {
126:                        final Object val = ary[j];
127:                        if ((val instanceof  Serializable)
128:                                || (val instanceof  Externalizable)) {
129:                            s.writeObject(val);
130:                        }
131:                    }
132:                }
133:                s.writeObject(null);
134:            }
135:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.