001: /*
002: * $RCSfile: HashtableState.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:56:50 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.rmi;
013:
014: import java.awt.RenderingHints;
015: import java.io.IOException;
016: import java.io.ObjectInputStream;
017: import java.io.ObjectOutputStream;
018: import java.io.Serializable;
019: import java.util.Enumeration;
020: import java.util.Hashtable;
021: import java.util.Iterator;
022: import java.util.Set;
023: import javax.media.jai.JAI;
024: import javax.media.jai.remote.SerializerFactory;
025: import javax.media.jai.remote.SerializableState;
026:
027: /**
028: * This class is a serializable proxy for a Hashtable object.
029: * <br>(entries which are neither <code>Serializable</code> nor supported by
030: * <code>SerializerFactory</code> are omitted);
031: *
032: *
033: * @since 1.1
034: */
035: public class HashtableState extends SerializableStateImpl {
036: /**
037: * Returns the classes supported by this SerializableState.
038: */
039: public static Class[] getSupportedClasses() {
040: return new Class[] { Hashtable.class };
041: }
042:
043: /**
044: * Constructs a <code>HashtableState</code> from a
045: * <code>Hashtable</code> object.
046: *
047: * @param c The <code>Class</code> of the object to be serialized.
048: * @param o The <code>Hashtable</code> object to be serialized.
049: * @param h The <code>RenderingHints</code> for this serialization.
050: */
051: public HashtableState(Class c, Object o, RenderingHints h) {
052: super (c, o, h);
053: }
054:
055: /**
056: * Serialize the HashtableState.
057: */
058: private void writeObject(ObjectOutputStream out) throws IOException {
059: // -- Create a serializable form of the Hashtable object. --
060: Hashtable table = (Hashtable) theObject;
061:
062: Hashtable serializableTable = new Hashtable();
063:
064: // If there are hints, add them to the table.
065: if (table != null && !table.isEmpty()) {
066: // Get a view of the hints' keys.
067: Set keySet = table.keySet();
068:
069: // Proceed if the key set is non-empty.
070: if (!keySet.isEmpty()) {
071: // Get an iterator for the key set.
072: Iterator keyIterator = keySet.iterator();
073:
074: // Loop over the keys.
075: while (keyIterator.hasNext()) {
076: // Get the next key.
077: Object key = keyIterator.next();
078: Object serializableKey = getSerializableForm(key);
079:
080: if (serializableKey == null)
081: continue;
082:
083: // Get the next value.
084: Object value = table.get(key);
085:
086: Object serializableValue = getSerializableForm(value);
087:
088: if (serializableValue == null)
089: continue;
090:
091: serializableTable.put(serializableKey,
092: serializableValue);
093: }
094: }
095: }
096:
097: // Write serialized form to the stream.
098: out.writeObject(serializableTable);
099: }
100:
101: /**
102: * Deserialize the HashtableState.
103: */
104: private void readObject(ObjectInputStream in) throws IOException,
105: ClassNotFoundException {
106: // Read serialized form from the stream.
107: Hashtable serializableTable = (Hashtable) in.readObject();
108:
109: // Create an empty Hashtable object.
110: Hashtable table = new Hashtable();
111: theObject = table;
112:
113: // If the table is empty just return.
114: if (serializableTable.isEmpty()) {
115: return;
116: }
117:
118: // Get an enumeration of the table keys.
119: Enumeration keys = serializableTable.keys();
120:
121: // Loop over the table keys.
122: while (keys.hasMoreElements()) {
123: // Get the next key element.
124: Object serializableKey = keys.nextElement();
125: Object key = getDeserializedFrom(serializableKey);
126:
127: // Get the value element.
128: Object serializableValue = serializableTable
129: .get(serializableKey);
130: Object value = getDeserializedFrom(serializableValue);
131:
132: // Add an entry to the table.
133: table.put(key, value);
134: }
135: }
136: }
|