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: }
|