001: /*
002: * $RCSfile: DataBufferState.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.awt.image.DataBuffer;
016: import java.awt.image.DataBufferByte;
017: import java.awt.image.DataBufferInt;
018: import java.awt.image.DataBufferShort;
019: import java.awt.image.DataBufferUShort;
020: import java.io.IOException;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectOutputStream;
023: import java.io.Serializable;
024: import com.sun.media.jai.util.DataBufferUtils;
025:
026: /**
027: * This class is a serializable proxy for a DataBuffer from which the
028: * DataBuffer may be reconstituted.
029: *
030: *
031: * @since 1.1
032: */
033: public class DataBufferState extends SerializableStateImpl {
034:
035: /** DataBufferFloat and DataBufferDouble core classes or null. */
036: private static Class[] J2DDataBufferClasses = null;
037:
038: /** The DataBuffer. */
039: private transient DataBuffer dataBuffer;
040:
041: // Initialize J2DDataBufferClasses.
042: static {
043: try {
044: Class dbfClass = Class
045: .forName("java.awt.image.DataBufferFloat");
046: Class dbdClass = Class
047: .forName("java.awt.image.DataBufferDouble");
048: J2DDataBufferClasses = new Class[] { dbfClass, dbdClass };
049: } catch (ClassNotFoundException e) {
050: // Ignore the exception.
051: }
052: }
053:
054: public static Class[] getSupportedClasses() {
055: Class[] supportedClasses = null;
056: if (J2DDataBufferClasses != null) {
057: // Java 2 1.4.0 and higher.
058: supportedClasses = new Class[] {
059: DataBufferByte.class,
060: DataBufferShort.class,
061: DataBufferUShort.class,
062: DataBufferInt.class,
063: J2DDataBufferClasses[0],
064: J2DDataBufferClasses[1],
065: javax.media.jai.DataBufferFloat.class,
066: javax.media.jai.DataBufferDouble.class,
067: com.sun.media.jai.codecimpl.util.DataBufferFloat.class,
068: com.sun.media.jai.codecimpl.util.DataBufferDouble.class };
069: } else {
070: // Java 2 pre-1.4.0.
071: supportedClasses = new Class[] {
072: DataBufferByte.class,
073: DataBufferShort.class,
074: DataBufferUShort.class,
075: DataBufferInt.class,
076: javax.media.jai.DataBufferFloat.class,
077: javax.media.jai.DataBufferDouble.class,
078: com.sun.media.jai.codecimpl.util.DataBufferFloat.class,
079: com.sun.media.jai.codecimpl.util.DataBufferDouble.class };
080: }
081:
082: return supportedClasses;
083: }
084:
085: /**
086: * Constructs a <code>DataBufferState</code> from a
087: * <code>DataBuffer</code>.
088: *
089: * @param source The <code>DataBuffer</code> to be serialized.
090: * @param o The <code>SampleModel</code> to be serialized.
091: * @param h The <code>RenderingHints</code> (ignored).
092: */
093: public DataBufferState(Class c, Object o, RenderingHints h) {
094: super (c, o, h);
095: }
096:
097: // XXX Note that there is potential for some form of data compression in
098: // the readObject() and writeObject() methods.
099:
100: /**
101: * Serialize the <code>DataBufferState</code>.
102: *
103: * @param out The <code>ObjectOutputStream</code>.
104: */
105: private void writeObject(ObjectOutputStream out) throws IOException {
106: DataBuffer dataBuffer = (DataBuffer) theObject;
107:
108: // Write serialized form to the stream.
109: int dataType = dataBuffer.getDataType();
110: out.writeInt(dataType);
111: out.writeObject(dataBuffer.getOffsets());
112: out.writeInt(dataBuffer.getSize());
113: Object dataArray = null;
114: switch (dataType) {
115: case DataBuffer.TYPE_BYTE:
116: dataArray = ((DataBufferByte) dataBuffer).getBankData();
117: break;
118: case DataBuffer.TYPE_SHORT:
119: dataArray = ((DataBufferShort) dataBuffer).getBankData();
120: break;
121: case DataBuffer.TYPE_USHORT:
122: dataArray = ((DataBufferUShort) dataBuffer).getBankData();
123: break;
124: case DataBuffer.TYPE_INT:
125: dataArray = ((DataBufferInt) dataBuffer).getBankData();
126: break;
127: case DataBuffer.TYPE_FLOAT:
128: dataArray = DataBufferUtils.getBankDataFloat(dataBuffer);
129: break;
130: case DataBuffer.TYPE_DOUBLE:
131: dataArray = DataBufferUtils.getBankDataDouble(dataBuffer);
132: break;
133: default:
134: throw new RuntimeException(JaiI18N
135: .getString("DataBufferState0"));
136: }
137: out.writeObject(dataArray);
138: }
139:
140: /**
141: * Deserialize the <code>DataBufferState</code>.
142: *
143: * @param out The <code>ObjectInputStream</code>.
144: */
145: private void readObject(ObjectInputStream in) throws IOException,
146: ClassNotFoundException {
147: DataBuffer dataBuffer = null;
148:
149: // Read serialized form from the stream.
150: int dataType = -1;
151: int[] offsets = null;
152: int size = -1;
153: Object dataArray = null;
154: dataType = in.readInt();
155: offsets = (int[]) in.readObject();
156: size = in.readInt();
157: dataArray = in.readObject();
158:
159: // Restore the transient DataBuffer.
160: switch (dataType) {
161: case DataBuffer.TYPE_BYTE:
162: dataBuffer = new DataBufferByte((byte[][]) dataArray, size,
163: offsets);
164: break;
165: case DataBuffer.TYPE_SHORT:
166: dataBuffer = new DataBufferShort((short[][]) dataArray,
167: size, offsets);
168: break;
169: case DataBuffer.TYPE_USHORT:
170: dataBuffer = new DataBufferUShort((short[][]) dataArray,
171: size, offsets);
172: break;
173: case DataBuffer.TYPE_INT:
174: dataBuffer = new DataBufferInt((int[][]) dataArray, size,
175: offsets);
176: break;
177: case DataBuffer.TYPE_FLOAT:
178: dataBuffer = DataBufferUtils.createDataBufferFloat(
179: (float[][]) dataArray, size, offsets);
180: break;
181: case DataBuffer.TYPE_DOUBLE:
182: dataBuffer = DataBufferUtils.createDataBufferDouble(
183: (double[][]) dataArray, size, offsets);
184: break;
185: default:
186: throw new RuntimeException(JaiI18N
187: .getString("DataBufferState0"));
188: }
189:
190: theObject = dataBuffer;
191: }
192: }
|