📄 variant.java
字号:
package com.develop.jawin;
import com.develop.jawin.constants.*;
import com.develop.jawin.marshal.*;
import com.develop.io.*;
import java.io.*;
import java.util.Date;
public class Variant implements VarTypes, WellKnownGUIDs {
public static final int SIZEOF = 16;
public static void marshalIn(Object var, LittleEndianOutputStream leos)
throws COMException {
try {
Class cls = var.getClass();
if (cls.isArray()) {
marshalInArray(var, leos);
return;
}
if (cls == Integer.class) {
leos.writeShort(VT_I4);
leos.writeInt(((Integer) var).intValue());
} else if (cls == Short.class) {
leos.writeShort(VT_I2);
leos.writeShort(((Short) var).shortValue());
} else if (cls == String.class) {
leos.writeShort(VT_BSTR);
leos.writeStringUnicode((String) var);
} else if (cls == Boolean.class) {
leos.writeShort(VT_BOOL);
leos.writeByte(((Boolean) var).booleanValue() ? 1 : 0);
} else if (cls == Float.class) {
leos.writeShort(VT_R4);
leos.writeFloat(((Float) var).floatValue());
} else if (cls == Double.class) {
leos.writeShort(VT_R8);
leos.writeDouble(((Double) var).doubleValue());
} else if (var instanceof DispatchPtr) {
leos.writeShort(VT_DISPATCH);
DispatchPtr dp = (DispatchPtr) var;
leos.writeInt(DispatchPtr.iidToken);
leos.writeInt(dp.getPeer());
leos.writeInt(dp.getUnknown());
} else if (var instanceof java.util.Date) {
leos.writeShort(VT_DATE);
leos.writeDouble(DateUtil.getWin32Date((java.util.Date) var));
} else {
throw new Error("Type cannot be marshalled to variant: " + cls);
}
} catch (IOException ioe) {
throw new COMException(ioe);
}
}
private static void marshalInArray(Object array, LittleEndianOutputStream leos)
throws COMException, IOException {
Class cls = array.getClass().getComponentType();
if (cls == Integer.TYPE) {
leos.writeShort(VT_ARRAY + VT_I4);
int[] values = (int[]) array;
leos.writeInt(values.length);
for (int n = 0; n < values.length; n++) {
leos.writeInt(values[n]);
}
} else if (cls == Object.class) {
leos.writeShort(VT_ARRAY + VT_VARIANT);
Object[] values = (Object[]) array;
leos.writeInt(values.length);
for (int n = 0; n < values.length; n++) {
marshalIn(values[n], leos);
}
} else if (cls == Short.TYPE) {
leos.writeShort(VT_ARRAY + VT_I2);
short[] values = (short[]) array;
leos.writeInt(values.length);
for (int n = 0; n < values.length; n++) {
leos.writeShort(values[n]);
}
} else if (cls == String.class) {
leos.writeShort(VT_ARRAY + VT_BSTR);
String[] values = (String[]) array;
leos.writeInt(values.length);
for (int n = 0; n < values.length; n++) {
leos.writeStringUnicode(values[n]);
}
} else if (cls == Boolean.TYPE) {
leos.writeShort(VT_ARRAY + VT_BOOL);
boolean[] values = (boolean[]) array;
leos.writeInt(values.length);
for (int n = 0; n < values.length; n++) {
leos.writeByte(values[n] ? 1 : 0);
}
} else if (cls == Float.TYPE) {
leos.writeShort(VT_ARRAY + VT_R4);
float[] values = (float[]) array;
leos.writeInt(values.length);
for (int n = 0; n < values.length; n++) {
leos.writeFloat(values[n]);
}
} else if (DispatchPtr.class.isAssignableFrom(cls)) {
leos.writeShort(VT_ARRAY + VT_DISPATCH);
leos.writeInt(DispatchPtr.iidToken);
DispatchPtr[] values = (DispatchPtr[]) array;
leos.writeInt(values.length);
for (int n = 0; n < values.length; n++) {
leos.writeInt(values[n].getPeer());
leos.writeInt(values[n].getUnknown());
}
} else {
throw new Error("Type cannot be marshalled to variant: " + cls);
}
}
public static Object marshalOutArray(short vt, LittleEndianInputStream leis) throws COMException, IOException {
int length = leis.readInt();
switch (vt) {
case VT_I1:
case VT_UI1:
byte[] bytes = new byte[length];
for (int n = 0; n < length; n++) {
bytes[n] = leis.readByte();
}
return bytes;
case VT_I2:
case VT_UI2:
short[] shorts = new short[length];
for (int n = 0; n < length; n++) {
shorts[n] = leis.readShort();
}
return shorts;
case VT_I4:
case VT_UI4:
case VT_INT:
case VT_UINT:
int[] ints = new int[length];
for (int n = 0; n < length; n++) {
ints[n] = leis.readInt();
}
return ints;
case VT_I8:
case VT_UI8:
long[] longs = new long[length];
for (int n = 0; n < length; n++) {
longs[n] = leis.readLong();
}
return longs;
case VT_R4:
float[] floats = new float[length];
for (int n = 0; n < length; n++) {
floats[n] = leis.readFloat();
}
return floats;
case VT_R8:
double[] doubles = new double[length];
for (int n = 0; n < length; n++) {
doubles[n] = leis.readDouble();
}
return doubles;
case VT_DATE:
Date[] dates = new Date[length];
for (int n = 0; n < length; n++) {
dates[n] = new java.util.Date(DateUtil.getJavaDate(leis.readDouble()));
}
return dates;
case VT_BSTR:
String[] strings = new String[length];
for (int n = 0; n < length; n++) {
strings[n] = leis.readStringUnicode();
}
return strings;
case VT_DISPATCH:
DispatchPtr[] dispatches = new DispatchPtr[length];
for (int n = 0; n < length; n++) {
dispatches[n] = (DispatchPtr) IdentityManager.getCOMPtr(leis, IID_IDispatch);
}
return dispatches;
case VT_BOOL:
boolean[] booleans = new boolean[length];
for (int n = 0; n < length; n++) {
booleans[n] = (leis.readByte() != 0);
}
return booleans;
case VT_UNKNOWN:
COMPtr[] comptrs = new COMPtr[length];
for (int n = 0; n < length; n++) {
comptrs[n] = (COMPtr) IdentityManager.getCOMPtr(leis, IID_IUnknown);
}
return comptrs;
case VT_VARIANT:
Object[] objects = new Object[length];
for (int n = 0; n < length; n++) {
objects[n] = marshalOut(leis);
}
return objects;
}
throw new COMException("Bad vartype in array: " + vt);
}
public static Object marshalOut(LittleEndianInputStream leis)
throws COMException {
try {
short vt = leis.readShort();
if (0 != (vt & VT_ARRAY)) {
return marshalOutArray((short) (vt & VT_TYPEMASK), leis);
}
int temp = 0;
switch (vt) {
case VT_EMPTY:
return null;
case VT_NULL:
return null;
case VT_I2:
return new Short(leis.readShort());
case VT_I4:
return new Integer(leis.readInt());
case VT_R4:
return new Float(leis.readFloat());
case VT_R8:
return new Double(leis.readDouble());
//case VT_CY:
case VT_DATE:
return new java.util.Date(DateUtil.getJavaDate(leis.readDouble()));
case VT_BSTR:
return leis.readStringUnicode();
case VT_DISPATCH:
return IdentityManager.getCOMPtr(leis, IID_IDispatch);
//case VT_ERROR:
case VT_BOOL:
return new Boolean(leis.readByte() != 0);
//case VT_VARIANT:
case VT_UNKNOWN:
return (IdentityManager.getCOMPtr(leis, IID_IUnknown));
//case VT_DECIMAL:
case VT_I1:
case VT_UI1:
return new Byte(leis.readByte());
case VT_UI2:
return new Short(leis.readShort());
case VT_UI4:
return new Integer(leis.readInt());
case VT_I8:
return new Long(leis.readLong());
case VT_UI8:
return new Long(leis.readLong());
case VT_INT:
return new Integer(leis.readInt());
case VT_UINT:
return new Integer(leis.readInt());
case VT_VOID:
return null;
case VT_HRESULT:
temp = leis.readInt();
if (0 != (temp & 0x80000000))
throw new COMException(leis.readInt(), "Bad HR in Variant");
return null;
//case VT_PTR:
//case VT_SAFEARRAY:
//case VT_CARRAY:
//case VT_USERDEFINED:
//case VT_LPSTR:
//case VT_LPWSTR:
//case VT_FILETIME:
//case VT_BLOB:
//case VT_STREAM:
//case VT_STORAGE:
//case VT_STREAMED_OBJECT:
//case VT_STORED_OBJECT:
//case VT_BLOB_OBJECT:
//case VT_CF:
//case VT_CLSID:
//case VT_VECTOR:
//case VT_ARRAY:
//case VT_BYREF:
//case VT_RESERVED:
//case VT_ILLEGAL:
//case VT_ILLEGALMASKED:
//case VT_TYPEMASK:
}
return null;
} catch (IOException ioe) {
throw new COMException(ioe);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -