Article ID: 178561
Article Last Modified on 6/14/2006
import java.io.*;
import java.util.*;
import com.ms.com.*;
// SafeArrayOutputStream
public class SafeArrayOutputStream extends ByteArrayOutputStream
{
public SafeArray getSafeArray()
{
// Allocate an array the size of the vector
byte[] byteArray = toByteArray();
// Create the safearray
SafeArray array = new SafeArray(Variant.VariantByte,
byteArray.length);
// Fill in safearray
array.setBytes(0, byteArray.length, byteArray, 0);
return array;
}
}
The following class called SafeArrayInputStream converts data from a
SafeArray into an InputStream:
import java.io.*;
import com.ms.com.*;
// SafeArrayInputStream
public class SafeArrayInputStream extends ByteArrayInputStream
{
public SafeArrayInputStream(SafeArray array)
{
super(array.toByteArray());
}
}
Notice that both of these classes extend classes that are in the package
Java.io. The ByteArrayOutputStream and the ByteArrayInputStream already
provide most of the functionality necessary for the conversion.
interface IShortStorage
{
public void setAppData(Variant v);
public Variant getAppData();
}
You would be able to use them in the following manner:
// This is our variable that contains the COM interface
IShortStorage istor = (IShortStorage) storageCOMObject;
// This is our Object that we want to store
Color color = new Color(255,0,128);
// Create our SafeArrayOutputStream
SafeArrayOutputStream safeOutStream = new SafeArrayOutputStream();
// Declare a ObjectOutputStream for Serialization
ObjectOutputStream objOutStream = null;
try
{
// Create the ObjectOutputStream passing in our SafeArrayOutputStream
objOutStream = new ObjectOutputStream(safeOutStream);
// Write our color object to the stream
objOutStream.writeObject(color);
// Flush the stream
objOutStream.flush();
}
catch(Exception e)
{
// ObjectOutputStream threw an exception
}
// Close the stream
safeOutStream.close();
// Get the SafeArray from our SafeArrayOutputStream
SafeArray bytes = safeOutStream.getSafeArray();
// Create a new Variant to hold the SafeArray
Variant data = new Variant();
// Put the SafeArray inside the Variant
data.putSafeArray ( bytes );
// Store the Variant inside the Automation Object
istor.setAppData(data);
When you are ready to retieve the Object, use this code to deserialize your
Object from the SafeArrayInputStream:
// Get the Variant back from the Automation Object
Variant data = istor.getAppData();
// Get the SafeArray out of the Variant
SafeArray bytes = body.toSafeArray();
// Create a new SafeArrayInputStream based on the SafeArray
SafeArrayInputStream safeInStream = new SafeArrayInputStream(bytes);
// Create a ObjectInputStream based on the SafeArrayInputStream
ObjectInputStream objInStream = new ObjectInputStream(safeInStream);
// Get the serialized object out of the ObjectInputStream
Object obj = objInStream.readObject();
// Check to see if the object is a color
if(obj instanceof Color)
{
Color col = (Color)obj;
// Use the color
}
Additional query words: SafeArray serialization
Keywords: kbhowto kbfaq kbcode kbjavafaq KB178561