001: /*
002: * $RCSfile: DataBufferProxy.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:49 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.rmi;
013:
014: import java.awt.image.DataBuffer;
015: import java.awt.image.DataBufferByte;
016: import java.awt.image.DataBufferInt;
017: import java.awt.image.DataBufferShort;
018: import java.awt.image.DataBufferUShort;
019: import java.io.IOException;
020: import java.io.ObjectInputStream;
021: import java.io.ObjectOutputStream;
022: import java.io.Serializable;
023: import com.sun.media.jai.util.DataBufferUtils;
024:
025: /**
026: * This class is a serializable proxy for a DataBuffer from which the
027: * DataBuffer may be reconstituted.
028: *
029: *
030: * @since EA3
031: */
032: public class DataBufferProxy implements Serializable {
033: /** The DataBuffer. */
034: private transient DataBuffer dataBuffer;
035:
036: /**
037: * Constructs a <code>DataBufferProxy</code> from a
038: * <code>DataBuffer</code>.
039: *
040: * @param source The <code>DataBuffer</code> to be serialized.
041: */
042: public DataBufferProxy(DataBuffer source) {
043: dataBuffer = source;
044: }
045:
046: /**
047: * Retrieves the associated <code>DataBuffer</code>.
048: * @return The (perhaps reconstructed) <code>DataBuffer</code>.
049: */
050: public DataBuffer getDataBuffer() {
051: return dataBuffer;
052: }
053:
054: // XXX Note that there is potential for some form of data compression in
055: // the readObject() and writeObject() methods.
056:
057: /**
058: * Serialize the <code>DataBufferProxy</code>.
059: *
060: * @param out The <code>ObjectOutputStream</code>.
061: */
062: private void writeObject(ObjectOutputStream out) throws IOException {
063: // Write serialized form to the stream.
064: int dataType = dataBuffer.getDataType();
065: out.writeInt(dataType);
066: out.writeObject(dataBuffer.getOffsets());
067: out.writeInt(dataBuffer.getSize());
068: Object dataArray = null;
069: switch (dataType) {
070: case DataBuffer.TYPE_BYTE:
071: dataArray = ((DataBufferByte) dataBuffer).getBankData();
072: break;
073: case DataBuffer.TYPE_SHORT:
074: dataArray = ((DataBufferShort) dataBuffer).getBankData();
075: break;
076: case DataBuffer.TYPE_USHORT:
077: dataArray = ((DataBufferUShort) dataBuffer).getBankData();
078: break;
079: case DataBuffer.TYPE_INT:
080: dataArray = ((DataBufferInt) dataBuffer).getBankData();
081: break;
082: case DataBuffer.TYPE_FLOAT:
083: dataArray = DataBufferUtils.getBankDataFloat(dataBuffer);
084: break;
085: case DataBuffer.TYPE_DOUBLE:
086: dataArray = DataBufferUtils.getBankDataDouble(dataBuffer);
087: break;
088: default:
089: throw new RuntimeException(JaiI18N
090: .getString("DataBufferProxy0"));
091: }
092: out.writeObject(dataArray);
093: }
094:
095: /**
096: * Deserialize the <code>DataBufferProxy</code>.
097: *
098: * @param out The <code>ObjectInputStream</code>.
099: */
100: private void readObject(ObjectInputStream in) throws IOException,
101: ClassNotFoundException {
102: // Read serialized form from the stream.
103: int dataType = -1;
104: int[] offsets = null;
105: int size = -1;
106: Object dataArray = null;
107: dataType = in.readInt();
108: offsets = (int[]) in.readObject();
109: size = in.readInt();
110: dataArray = in.readObject();
111:
112: // Restore the transient DataBuffer.
113: switch (dataType) {
114: case DataBuffer.TYPE_BYTE:
115: dataBuffer = new DataBufferByte((byte[][]) dataArray, size,
116: offsets);
117: break;
118: case DataBuffer.TYPE_SHORT:
119: dataBuffer = new DataBufferShort((short[][]) dataArray,
120: size, offsets);
121: break;
122: case DataBuffer.TYPE_USHORT:
123: dataBuffer = new DataBufferUShort((short[][]) dataArray,
124: size, offsets);
125: break;
126: case DataBuffer.TYPE_INT:
127: dataBuffer = new DataBufferInt((int[][]) dataArray, size,
128: offsets);
129: break;
130: case DataBuffer.TYPE_FLOAT:
131: dataBuffer = DataBufferUtils.createDataBufferFloat(
132: (float[][]) dataArray, size, offsets);
133: break;
134: case DataBuffer.TYPE_DOUBLE:
135: dataBuffer = DataBufferUtils.createDataBufferDouble(
136: (double[][]) dataArray, size, offsets);
137: break;
138: default:
139: throw new RuntimeException(JaiI18N
140: .getString("DataBufferProxy0"));
141: }
142: }
143: }
|