0001: /*
0002: * Copyright (c) 1998 - 2005 Versant Corporation
0003: * All rights reserved. This program and the accompanying materials
0004: * are made available under the terms of the Eclipse Public License v1.0
0005: * which accompanies this distribution, and is available at
0006: * http://www.eclipse.org/legal/epl-v10.html
0007: *
0008: * Contributors:
0009: * Versant Corporation - initial API and implementation
0010: */
0011: package com.versant.core.common;
0012:
0013: import com.versant.core.metadata.MDStatics;
0014: import com.versant.core.metadata.FieldMetaData;
0015: import com.versant.core.util.OIDObjectInput;
0016: import com.versant.core.util.OIDObjectOutput;
0017:
0018: import java.io.*;
0019: import java.util.Date;
0020: import java.util.Locale;
0021: import java.math.BigDecimal;
0022: import java.math.BigInteger;
0023:
0024: /**
0025: * Utility methods to help with Serialization.
0026: */
0027: public final class SerUtils {
0028:
0029: public static Object readArrayField(FieldMetaData fmd,
0030: OIDObjectInput is) throws IOException,
0031: ClassNotFoundException {
0032: if (fmd.elementTypeMetaData != null) {
0033: return SerUtils.readOIDArray(is);
0034: } else {
0035: return SerUtils.readArrayField(fmd.componentTypeCode, is);
0036: }
0037: }
0038:
0039: public static void writeArrayField(FieldMetaData fmd,
0040: OIDObjectOutput os, Object data) throws IOException {
0041: if (fmd.elementTypeMetaData != null) {
0042: SerUtils.writeOIDArray((OID[]) data, os);
0043: } else {
0044: SerUtils.writeArrayField(fmd.componentTypeCode, os, data);
0045: }
0046: }
0047:
0048: public static final void writeOIDArray(OID[] oids,
0049: OIDObjectOutput out) throws IOException {
0050: if (oids == null) {
0051: out.writeInt(0);
0052: } else {
0053: int length = oids.length;
0054: out.writeInt(length);
0055: for (int i = 0; i < length; i++) {
0056: out.write(oids[i]);
0057: }
0058: }
0059: }
0060:
0061: public static final OID[] readOIDArray(OIDObjectInput in)
0062: throws IOException, ClassNotFoundException {
0063: int size = in.readInt();
0064: if (size > 0) {
0065: OID[] oids = new OID[size];
0066: for (int i = 0; i < size; i++) {
0067: oids[i] = in.readOID();
0068: }
0069: return oids;
0070: } else {
0071: return null;
0072: }
0073: }
0074:
0075: public static Object readArrayField(int type, OIDObjectInput is)
0076: throws IOException, ClassNotFoundException {
0077: switch (type) {
0078: case MDStatics.INTW:
0079: return SerUtils.readIntegerArray(is);
0080: case MDStatics.INT:
0081: return SerUtils.readIntArray(is);
0082: case MDStatics.SHORTW:
0083: return SerUtils.readShortWArray(is);
0084: case MDStatics.SHORT:
0085: return SerUtils.readShortArray(is);
0086: case MDStatics.CHARW:
0087: return SerUtils.readCharWArray(is);
0088: case MDStatics.CHAR:
0089: return SerUtils.readCharArray(is);
0090: case MDStatics.BOOLEANW:
0091: return SerUtils.readBooleanWArray(is);
0092: case MDStatics.BOOLEAN:
0093: return SerUtils.readBooleanArray(is);
0094: case MDStatics.BYTEW:
0095: return SerUtils.readByteWArray(is);
0096: case MDStatics.BYTE:
0097: return SerUtils.readByteArray(is);
0098: case MDStatics.DOUBLEW:
0099: return SerUtils.readDoubleWArray(is);
0100: case MDStatics.DOUBLE:
0101: return SerUtils.readDoubleArray(is);
0102: case MDStatics.FLOATW:
0103: return SerUtils.readFloatWArray(is);
0104: case MDStatics.FLOAT:
0105: return SerUtils.readFloatArray(is);
0106: case MDStatics.LONGW:
0107: return SerUtils.readLongWArray(is);
0108: case MDStatics.LONG:
0109: return SerUtils.readLongArray(is);
0110: case MDStatics.STRING:
0111: return SerUtils.readStringArray(is);
0112: case MDStatics.LOCALE:
0113: return SerUtils.readLocaleArray(is);
0114: case MDStatics.DATE:
0115: return SerUtils.readDateArray(is);
0116: case MDStatics.OID:
0117: return SerUtils.readOIDArray(is);
0118: default:
0119: return SerUtils.readObjectArray(is);
0120: }
0121: }
0122:
0123: public static void writeArrayField(int type, OIDObjectOutput os,
0124: Object toWrite) throws IOException {
0125: switch (type) {
0126: case MDStatics.INTW:
0127: SerUtils.writeIntegerArray((Integer[]) toWrite, os);
0128: break;
0129: case MDStatics.INT:
0130: SerUtils.writeIntArray((int[]) toWrite, os);
0131: break;
0132: case MDStatics.SHORTW:
0133: SerUtils.writeShortWArray((Short[]) toWrite, os);
0134: break;
0135: case MDStatics.SHORT:
0136: SerUtils.writeShortArray((short[]) toWrite, os);
0137: break;
0138: case MDStatics.CHARW:
0139: SerUtils.writeCharWArray((Character[]) toWrite, os);
0140: break;
0141: case MDStatics.CHAR:
0142: SerUtils.writeCharArray((char[]) toWrite, os);
0143: break;
0144: case MDStatics.BOOLEANW:
0145: SerUtils.writeBooleanWArray((Boolean[]) toWrite, os);
0146: break;
0147: case MDStatics.BOOLEAN:
0148: SerUtils.writeBooleanArray((boolean[]) toWrite, os);
0149: break;
0150: case MDStatics.BYTEW:
0151: SerUtils.writeByteWArray((Byte[]) toWrite, os);
0152: break;
0153: case MDStatics.BYTE:
0154: SerUtils.writeByteArray((byte[]) toWrite, os);
0155: break;
0156: case MDStatics.DOUBLEW:
0157: SerUtils.writeDoubleWArray((Double[]) toWrite, os);
0158: break;
0159: case MDStatics.DOUBLE:
0160: SerUtils.writeDoubleArray((double[]) toWrite, os);
0161: break;
0162: case MDStatics.FLOATW:
0163: SerUtils.writeFloatWArray((Float[]) toWrite, os);
0164: break;
0165: case MDStatics.FLOAT:
0166: SerUtils.writeFloatArray((float[]) toWrite, os);
0167: break;
0168: case MDStatics.LONGW:
0169: SerUtils.writeLongWArray((Long[]) toWrite, os);
0170: break;
0171: case MDStatics.LONG:
0172: SerUtils.writeLongArray((long[]) toWrite, os);
0173: break;
0174: case MDStatics.STRING:
0175: SerUtils.writeStringArray((String[]) toWrite, os);
0176: break;
0177: case MDStatics.LOCALE:
0178: SerUtils.writeLocaleArray((Locale[]) toWrite, os);
0179: break;
0180: case MDStatics.DATE:
0181: SerUtils.writeDateArray((Date[]) toWrite, os);
0182: break;
0183: case MDStatics.OID:
0184: SerUtils.writeOIDArray((OID[]) toWrite, os);
0185: break;
0186: default:
0187: writeObjectArray((Object[]) toWrite, os);
0188: }
0189: }
0190:
0191: public static void writeLongWArray(Long[] array, ObjectOutput out)
0192: throws IOException {
0193: if (array == null) {
0194: out.writeBoolean(true);
0195: } else {
0196: out.writeBoolean(false);
0197: int n = array.length;
0198: out.writeInt(n);
0199: for (int i = 0; i < n; i++) {
0200: if (array[i] == null) {
0201: out.writeByte(0);
0202: } else {
0203: out.writeByte(0);
0204: out.writeLong(array[i].longValue());
0205: }
0206: }
0207: }
0208: }
0209:
0210: public static Long[] readLongWArray(ObjectInput in)
0211: throws IOException {
0212: if (in.readBoolean())
0213: return null;
0214: final int n = in.readInt();
0215: final Long[] array = new Long[n];
0216: for (int i = 0; i < n; i++) {
0217: if (in.readByte() == 1)
0218: array[i] = new Long(in.readLong());
0219: }
0220: return array;
0221: }
0222:
0223: public static void writeLongArray(long[] array, ObjectOutput out)
0224: throws IOException {
0225: if (array == null) {
0226: out.writeBoolean(true);
0227: return;
0228: } else {
0229: out.writeBoolean(false);
0230: }
0231: final int n = array.length;
0232: out.writeInt(n);
0233: for (int i = 0; i < n; i++) {
0234: out.writeLong(array[i]);
0235: }
0236: }
0237:
0238: public static long[] readLongArray(ObjectInput in)
0239: throws IOException {
0240: if (in.readBoolean())
0241: return null;
0242: final int n = in.readInt();
0243: final long[] array = new long[n];
0244: for (int i = 0; i < n; i++) {
0245: array[i] = in.readLong();
0246: }
0247: return array;
0248: }
0249:
0250: public static void writeBooleanWArray(Boolean[] array,
0251: ObjectOutput out) throws IOException {
0252: if (array == null) {
0253: out.writeBoolean(true);
0254: return;
0255: } else {
0256: out.writeBoolean(false);
0257: }
0258: final int n = array.length;
0259: out.writeInt(n);
0260: for (int i = 0; i < n; i++) {
0261: if (array[i] == null) {
0262: out.writeByte(0);
0263: } else {
0264: out.writeByte(0);
0265: out.writeByte(array[i].booleanValue() == true ? 1 : 0);
0266: }
0267: }
0268: }
0269:
0270: public static Boolean[] readBooleanWArray(ObjectInput in)
0271: throws IOException {
0272: if (in.readBoolean())
0273: return null;
0274: final int n = in.readInt();
0275: final Boolean[] array = new Boolean[n];
0276: for (int i = 0; i < n; i++) {
0277: if (in.readByte() == 1)
0278: array[i] = new Boolean(in.readByte() == 1 ? true
0279: : false);
0280: }
0281: return array;
0282: }
0283:
0284: public static void writeBooleanArray(boolean[] array,
0285: ObjectOutput out) throws IOException {
0286: if (array == null) {
0287: out.writeBoolean(true);
0288: return;
0289: } else {
0290: out.writeBoolean(false);
0291: }
0292: final int n = array.length;
0293: out.writeInt(n);
0294: for (int i = 0; i < n; i++) {
0295: out.writeBoolean(array[i]);
0296: }
0297: }
0298:
0299: public static boolean[] readBooleanArray(ObjectInput in)
0300: throws IOException {
0301: if (in.readBoolean())
0302: return null;
0303: final int n = in.readInt();
0304: final boolean[] array = new boolean[n];
0305: for (int i = 0; i < n; i++) {
0306: array[i] = in.readBoolean();
0307: }
0308: return array;
0309: }
0310:
0311: public static void writeIntegerArray(Integer[] array,
0312: ObjectOutput out) throws IOException {
0313: if (array == null) {
0314: out.writeBoolean(true);
0315: return;
0316: } else {
0317: out.writeBoolean(false);
0318: }
0319: final int n = array.length;
0320: out.writeInt(n);
0321: for (int i = 0; i < n; i++) {
0322: if (array[i] == null) {
0323: out.writeByte(0);
0324: } else {
0325: out.writeByte(1);
0326: out.writeInt(array[i].intValue());
0327: }
0328: }
0329: }
0330:
0331: public static Integer[] readIntegerArray(ObjectInput in)
0332: throws IOException {
0333: if (in.readBoolean())
0334: return null;
0335: final int n = in.readInt();
0336: final Integer[] array = new Integer[n];
0337: for (int i = 0; i < n; i++) {
0338: if (in.readByte() == 1) {
0339: array[i] = new Integer(in.readInt());
0340: }
0341: }
0342: return array;
0343: }
0344:
0345: public static void writeIntArray(int[] array, ObjectOutput out)
0346: throws IOException {
0347: if (array == null) {
0348: out.writeBoolean(true);
0349: return;
0350: } else {
0351: out.writeBoolean(false);
0352: }
0353: final int n = array.length;
0354: out.writeInt(n);
0355: for (int i = 0; i < n; i++) {
0356: out.writeInt(array[i]);
0357: }
0358: }
0359:
0360: public static int[] readIntArray(ObjectInput in) throws IOException {
0361: if (in.readBoolean()) {
0362: return null;
0363: }
0364: int n = in.readInt();
0365: int[] array = new int[n];
0366: for (int i = 0; i < n; i++) {
0367: array[i] = in.readInt();
0368: }
0369: return array;
0370: }
0371:
0372: public static void writeByteWArray(Byte[] array, ObjectOutput out)
0373: throws IOException {
0374: if (array == null) {
0375: out.writeBoolean(true);
0376: return;
0377: } else {
0378: out.writeBoolean(false);
0379: }
0380: final int n = array.length;
0381: out.writeInt(n);
0382: for (int i = 0; i < n; i++) {
0383: if (array[i] == null) {
0384: out.writeByte(0);
0385: } else {
0386: out.writeByte(1);
0387: out.writeByte(array[i].byteValue());
0388: }
0389: }
0390: }
0391:
0392: public static Byte[] readByteWArray(ObjectInput in)
0393: throws IOException {
0394: if (in.readBoolean())
0395: return null;
0396: final int n = in.readInt();
0397: final Byte[] array = new Byte[n];
0398: for (int i = 0; i < n; i++) {
0399: if (in.readByte() == 1)
0400: array[i] = new Byte(in.readByte());
0401: }
0402: return array;
0403: }
0404:
0405: public static void writeByteArray(byte[] array, ObjectOutput out)
0406: throws IOException {
0407: if (array == null) {
0408: out.writeBoolean(true);
0409: return;
0410: } else {
0411: out.writeBoolean(false);
0412: }
0413: final int n = array.length;
0414: out.writeInt(n);
0415: for (int i = 0; i < n; i++) {
0416: out.writeByte(array[i]);
0417: }
0418: }
0419:
0420: public static byte[] readByteArray(ObjectInput in)
0421: throws IOException {
0422: if (in.readBoolean())
0423: return null;
0424: final int n = in.readInt();
0425: final byte[] array = new byte[n];
0426: for (int i = 0; i < n; i++) {
0427: array[i] = in.readByte();
0428: }
0429: return array;
0430: }
0431:
0432: public static void writeShortWArray(Short[] array, ObjectOutput out)
0433: throws IOException {
0434: if (array == null) {
0435: out.writeBoolean(true);
0436: return;
0437: } else {
0438: out.writeBoolean(false);
0439: }
0440: final int n = array.length;
0441: out.writeInt(n);
0442: for (int i = 0; i < n; i++) {
0443: if (array[i] == null) {
0444: out.writeByte(0);
0445: } else {
0446: out.writeByte(0);
0447: out.writeShort(array[i].shortValue());
0448: }
0449: }
0450: }
0451:
0452: public static Short[] readShortWArray(ObjectInput in)
0453: throws IOException {
0454: if (in.readBoolean())
0455: return null;
0456: final int n = in.readInt();
0457: final Short[] array = new Short[n];
0458: for (int i = 0; i < n; i++) {
0459: if (in.readByte() == 1)
0460: array[i] = new Short(in.readShort());
0461: }
0462: return array;
0463: }
0464:
0465: public static void writeShortArray(short[] array, ObjectOutput out)
0466: throws IOException {
0467: if (array == null) {
0468: out.writeBoolean(true);
0469: return;
0470: } else {
0471: out.writeBoolean(false);
0472: }
0473: final int n = array.length;
0474: out.writeInt(n);
0475: for (int i = 0; i < n; i++) {
0476: out.writeShort(array[i]);
0477: }
0478: }
0479:
0480: public static short[] readShortArray(ObjectInput in)
0481: throws IOException {
0482: if (in.readBoolean())
0483: return null;
0484: final int n = in.readInt();
0485: final short[] array = new short[n];
0486: for (int i = 0; i < n; i++) {
0487: array[i] = in.readShort();
0488: }
0489: return array;
0490: }
0491:
0492: public static void writeCharWArray(Character[] array,
0493: ObjectOutput out) throws IOException {
0494: if (array == null) {
0495: out.writeBoolean(true);
0496: return;
0497: } else {
0498: out.writeBoolean(false);
0499: }
0500: final int n = array.length;
0501: out.writeInt(n);
0502: for (int i = 0; i < n; i++) {
0503: if (array[i] == null) {
0504: out.writeByte(0);
0505: } else {
0506: out.writeByte(0);
0507: out.writeChar(array[i].charValue());
0508: }
0509: }
0510: }
0511:
0512: public static Character[] readCharWArray(ObjectInput in)
0513: throws IOException {
0514: if (in.readBoolean())
0515: return null;
0516: final int n = in.readInt();
0517: final Character[] array = new Character[n];
0518: for (int i = 0; i < n; i++) {
0519: if (in.readByte() == 1)
0520: array[i] = new Character(in.readChar());
0521: }
0522: return array;
0523: }
0524:
0525: public static void writeCharArray(char[] array, ObjectOutput out)
0526: throws IOException {
0527: if (array == null) {
0528: out.writeBoolean(true);
0529: return;
0530: } else {
0531: out.writeBoolean(false);
0532: }
0533: final int n = array.length;
0534: out.writeInt(n);
0535: for (int i = 0; i < n; i++) {
0536: out.writeChar(array[i]);
0537: }
0538: }
0539:
0540: public static char[] readCharArray(ObjectInput in)
0541: throws IOException {
0542: if (in.readBoolean())
0543: return null;
0544: final int n = in.readInt();
0545: final char[] array = new char[n];
0546: for (int i = 0; i < n; i++) {
0547: array[i] = in.readChar();
0548: }
0549: return array;
0550: }
0551:
0552: public static void writeFloatWArray(Float[] array, ObjectOutput out)
0553: throws IOException {
0554: if (array == null) {
0555: out.writeBoolean(true);
0556: return;
0557: } else {
0558: out.writeBoolean(false);
0559: }
0560: final int n = array.length;
0561: out.writeInt(n);
0562: for (int i = 0; i < n; i++) {
0563: if (array[i] == null) {
0564: out.writeByte(0);
0565: } else {
0566: out.writeByte(0);
0567: out.writeFloat(array[i].floatValue());
0568: }
0569: }
0570: }
0571:
0572: public static Float[] readFloatWArray(ObjectInput in)
0573: throws IOException {
0574: if (in.readBoolean())
0575: return null;
0576: final int n = in.readInt();
0577: final Float[] array = new Float[n];
0578: for (int i = 0; i < n; i++) {
0579: if (in.readByte() == 1)
0580: array[i] = new Float(in.readFloat());
0581: }
0582: return array;
0583: }
0584:
0585: public static void writeFloatArray(float[] array, ObjectOutput out)
0586: throws IOException {
0587: if (array == null) {
0588: out.writeBoolean(true);
0589: return;
0590: } else {
0591: out.writeBoolean(false);
0592: }
0593: final int n = array.length;
0594: out.writeInt(n);
0595: for (int i = 0; i < n; i++) {
0596: out.writeFloat(array[i]);
0597: }
0598: }
0599:
0600: public static float[] readFloatArray(ObjectInput in)
0601: throws IOException {
0602: if (in.readBoolean())
0603: return null;
0604: final int n = in.readInt();
0605: final float[] array = new float[n];
0606: for (int i = 0; i < n; i++) {
0607: array[i] = in.readFloat();
0608: }
0609: return array;
0610: }
0611:
0612: public static void writeDoubleWArray(Double[] array,
0613: ObjectOutput out) throws IOException {
0614: if (array == null) {
0615: out.writeBoolean(true);
0616: return;
0617: } else {
0618: out.writeBoolean(false);
0619: }
0620: final int n = array.length;
0621: out.writeInt(n);
0622: for (int i = 0; i < n; i++) {
0623: if (array[i] == null) {
0624: out.writeByte(0);
0625: } else {
0626: out.writeByte(0);
0627: out.writeDouble(array[i].doubleValue());
0628: }
0629: }
0630: }
0631:
0632: public static Double[] readDoubleWArray(ObjectInput in)
0633: throws IOException {
0634: if (in.readBoolean())
0635: return null;
0636: final int n = in.readInt();
0637: final Double[] array = new Double[n];
0638: for (int i = 0; i < n; i++) {
0639: if (in.readByte() == 1)
0640: array[i] = new Double(in.readDouble());
0641: }
0642: return array;
0643: }
0644:
0645: public static void writeDoubleArray(double[] array, ObjectOutput out)
0646: throws IOException {
0647: if (array == null) {
0648: out.writeBoolean(true);
0649: return;
0650: } else {
0651: out.writeBoolean(false);
0652: }
0653: final int n = array.length;
0654: out.writeInt(n);
0655: for (int i = 0; i < n; i++) {
0656: out.writeDouble(array[i]);
0657: }
0658: }
0659:
0660: public static double[] readDoubleArray(ObjectInput in)
0661: throws IOException {
0662: if (in.readBoolean())
0663: return null;
0664: final int n = in.readInt();
0665: final double[] array = new double[n];
0666: for (int i = 0; i < n; i++) {
0667: array[i] = in.readDouble();
0668: }
0669: return array;
0670: }
0671:
0672: public static void writeStringArray(String[] array, ObjectOutput out)
0673: throws IOException {
0674: if (array == null) {
0675: out.writeBoolean(true);
0676: return;
0677: } else {
0678: out.writeBoolean(false);
0679: }
0680: final int n = array.length;
0681: out.writeInt(n);
0682: for (int i = 0; i < n; i++) {
0683: if (array[i] == null) {
0684: out.writeByte(0);
0685: } else {
0686: out.writeByte(1);
0687: out.writeUTF(array[i]);
0688: }
0689: }
0690: }
0691:
0692: public static String[] readStringArray(ObjectInput in)
0693: throws IOException {
0694: if (in.readBoolean())
0695: return null;
0696: final int n = in.readInt();
0697: final String[] array = new String[n];
0698: for (int i = 0; i < n; i++) {
0699: if (in.readByte() == 1) {
0700: array[i] = in.readUTF();
0701: }
0702: }
0703: return array;
0704: }
0705:
0706: public static void writeDateArray(Date[] array, ObjectOutput out)
0707: throws IOException {
0708: if (array == null) {
0709: out.writeBoolean(true);
0710: return;
0711: } else {
0712: out.writeBoolean(false);
0713: }
0714: final int n = array.length;
0715: out.writeInt(n);
0716: for (int i = 0; i < n; i++) {
0717: if (array[i] == null) {
0718: out.writeByte(0);
0719: } else {
0720: out.writeByte(1);
0721: out.writeLong(array[i].getTime());
0722: }
0723: }
0724: }
0725:
0726: public static Date[] readDateArray(ObjectInput in)
0727: throws IOException {
0728: if (in.readBoolean())
0729: return null;
0730: final int n = in.readInt();
0731: final Date[] array = new Date[n];
0732: for (int i = 0; i < n; i++) {
0733: if (in.readByte() == 1) {
0734: array[i] = new Date(in.readLong());
0735: }
0736: }
0737: return array;
0738: }
0739:
0740: public static void writeObjectArray(Object[] array, ObjectOutput out)
0741: throws IOException {
0742: out.writeObject(array);
0743: }
0744:
0745: public static Object[] readObjectArray(ObjectInput in)
0746: throws IOException, ClassNotFoundException {
0747: return (Object[]) in.readObject();
0748: }
0749:
0750: public static void writeLocaleArray(Locale[] array, ObjectOutput out)
0751: throws IOException {
0752: if (array == null) {
0753: out.writeBoolean(true);
0754: return;
0755: } else {
0756: out.writeBoolean(false);
0757: }
0758: final int n = array.length;
0759: out.writeInt(n);
0760: for (int i = 0; i < n; i++) {
0761: if (array[i] == null) {
0762: out.writeByte(0);
0763: } else {
0764: out.writeByte(1);
0765: Locale l = (Locale) array[i];
0766: out.writeUTF(l.getLanguage());
0767: out.writeUTF(l.getCountry());
0768: out.writeUTF(l.getVariant());
0769: }
0770: }
0771: }
0772:
0773: public static Locale[] readLocaleArray(ObjectInput in)
0774: throws IOException {
0775: if (in.readBoolean())
0776: return null;
0777: final int n = in.readInt();
0778: final Locale[] array = new Locale[n];
0779: for (int i = 0; i < n; i++) {
0780: if (in.readByte() == 1) {
0781: array[i] = new Locale(in.readUTF(), in.readUTF(), in
0782: .readUTF());
0783: }
0784: }
0785: return array;
0786: }
0787:
0788: public static void writeSimpleField(FieldMetaData fmd,
0789: ObjectOutput os, Object toWrite) throws IOException {
0790: writeSimpleField(fmd.typeCode, os, toWrite);
0791: }
0792:
0793: public static void writeSimpleField(int typeCode, ObjectOutput os,
0794: Object toWrite) throws IOException {
0795: if (toWrite == null) {
0796: os.writeByte(0);
0797: return;
0798: } else {
0799: os.writeByte(1);
0800: switch (typeCode) {
0801: case MDStatics.INTW:
0802: case MDStatics.INT:
0803: os.writeInt(((Integer) toWrite).intValue());
0804: break;
0805: case MDStatics.CHARW:
0806: case MDStatics.CHAR:
0807: os.writeChar(((Character) toWrite).charValue());
0808: break;
0809: case MDStatics.SHORTW:
0810: case MDStatics.SHORT:
0811: os.writeShort(((Short) toWrite).shortValue());
0812: break;
0813: case MDStatics.STRING:
0814: Utils.writeLongUTF8((String) toWrite, os);
0815: break;
0816: case MDStatics.BOOLEANW:
0817: case MDStatics.BOOLEAN:
0818: os.writeBoolean(((Boolean) toWrite).booleanValue());
0819: break;
0820: case MDStatics.BYTEW:
0821: case MDStatics.BYTE:
0822: os.writeByte(((Byte) toWrite).byteValue());
0823: break;
0824: case MDStatics.DOUBLEW:
0825: case MDStatics.DOUBLE:
0826: os.writeDouble(((Double) toWrite).doubleValue());
0827: break;
0828: case MDStatics.FLOATW:
0829: case MDStatics.FLOAT:
0830: os.writeFloat(((Float) toWrite).floatValue());
0831: break;
0832: case MDStatics.LONGW:
0833: case MDStatics.LONG:
0834: os.writeLong(((Long) toWrite).longValue());
0835: break;
0836: case MDStatics.DATE:
0837: os.writeLong(((Date) toWrite).getTime());
0838: break;
0839: case MDStatics.LOCALE:
0840: final Locale l = (Locale) toWrite;
0841: os.writeUTF(l.getLanguage());
0842: os.writeUTF(l.getCountry());
0843: os.writeUTF(l.getVariant());
0844: break;
0845: case MDStatics.BIGDECIMAL:
0846: os.writeUTF(toWrite.toString());
0847: break;
0848: case MDStatics.BIGINTEGER:
0849: os.writeUTF(toWrite.toString());
0850: break;
0851: default:
0852: os.writeObject(toWrite);
0853: break;
0854: }
0855: }
0856: }
0857:
0858: public static void writeObject(Object o, ObjectOutput dos)
0859: throws IOException {
0860: dos.writeObject(o);
0861: }
0862:
0863: public static Object readSimpleField(FieldMetaData fmd,
0864: ObjectInput is) throws IOException, ClassNotFoundException {
0865: return readSimpleField(fmd.typeCode, is);
0866: }
0867:
0868: public static Object readSimpleField(int typeCode, ObjectInput is)
0869: throws IOException, ClassNotFoundException {
0870: if (is.readByte() == 0) {
0871: return null;
0872: } else {
0873: switch (typeCode) {
0874: case MDStatics.INTW:
0875: case MDStatics.INT:
0876: return new Integer(is.readInt());
0877: case MDStatics.CHARW:
0878: case MDStatics.CHAR:
0879: return new Character(is.readChar());
0880: case MDStatics.SHORTW:
0881: case MDStatics.SHORT:
0882: return new Short(is.readShort());
0883: case MDStatics.STRING:
0884: return Utils.readLongUTF8(is);
0885: case MDStatics.BOOLEANW:
0886: case MDStatics.BOOLEAN:
0887: return new Boolean(is.readBoolean());
0888: case MDStatics.BYTEW:
0889: case MDStatics.BYTE:
0890: return new Byte(is.readByte());
0891: case MDStatics.DOUBLEW:
0892: case MDStatics.DOUBLE:
0893: return new Double(is.readDouble());
0894: case MDStatics.FLOATW:
0895: case MDStatics.FLOAT:
0896: return new Float(is.readFloat());
0897: case MDStatics.LONGW:
0898: case MDStatics.LONG:
0899: return new Long(is.readLong());
0900: case MDStatics.DATE:
0901: return new Date(is.readLong());
0902: case MDStatics.LOCALE:
0903: return new Locale(is.readUTF(), is.readUTF(), is
0904: .readUTF());
0905: case MDStatics.BIGDECIMAL:
0906: return new BigDecimal(is.readUTF());
0907: case MDStatics.BIGINTEGER:
0908: return new BigInteger(is.readUTF());
0909: default:
0910: return is.readObject();
0911: }
0912: }
0913: }
0914:
0915: public static Object readObject(ObjectInput dis)
0916: throws IOException, ClassNotFoundException {
0917: return dis.readObject();
0918: }
0919:
0920: public static Object[] readCollectionArray(FieldMetaData fmd,
0921: OIDObjectInput in) throws IOException,
0922: ClassNotFoundException {
0923: if (in.readBoolean())
0924: return null;
0925: if (fmd.elementTypeMetaData != null) {
0926: return readOIDObjectArray(in);
0927: } else {
0928: return readColObjectArray(fmd.elementTypeCode, in);
0929: }
0930: }
0931:
0932: public static void writeCollectionArray(FieldMetaData fmd,
0933: Object data, OIDObjectOutput out) throws IOException {
0934: if (data == null) {
0935: out.writeBoolean(true);
0936: return;
0937: } else {
0938: out.writeBoolean(false);
0939: }
0940: if (fmd.elementTypeMetaData != null) {
0941: writeOIDObjectArray((Object[]) data, out);
0942: } else {
0943: writeColObjectArray((Object[]) data, out,
0944: fmd.elementTypeCode);
0945: }
0946: }
0947:
0948: /**
0949: * This writes the Object[] of oids that is send as part of a collection read
0950: * from the store.
0951: */
0952: public static void writeOIDObjectArray(Object[] oids,
0953: OIDObjectOutput out) throws IOException {
0954: int n = oids.length;
0955: out.writeInt(n);
0956: for (int j = 0; j < n; j++) {
0957: out.write((OID) oids[j]);
0958: }
0959: }
0960:
0961: public static Object[] readOIDObjectArray(OIDObjectInput in)
0962: throws IOException, ClassNotFoundException {
0963: OID[] oids = new OID[in.readInt()];
0964: for (int j = 0; j < oids.length; j++) {
0965: oids[j] = in.readOID();
0966: }
0967: return oids;
0968: }
0969:
0970: private static void writeColObjectArray(Object[] ar,
0971: ObjectOutput out, int typeCode) throws IOException {
0972: int n = ar.length;
0973: out.writeInt(n);
0974: switch (typeCode) {
0975: case MDStatics.INTW:
0976: case MDStatics.INT:
0977: for (int i = 0; i < n; i++) {
0978: if (ar[i] == null) {
0979: out.writeByte(0);
0980: continue;
0981: } else {
0982: out.writeByte(1);
0983: }
0984: out.writeInt(((Integer) ar[i]).intValue());
0985: }
0986: break;
0987: case MDStatics.CHARW:
0988: case MDStatics.CHAR:
0989: for (int i = 0; i < n; i++) {
0990: if (ar[i] == null) {
0991: out.writeByte(0);
0992: continue;
0993: } else {
0994: out.writeByte(1);
0995: }
0996: out.writeChar(((Character) ar[i]).charValue());
0997: }
0998: break;
0999: case MDStatics.SHORTW:
1000: case MDStatics.SHORT:
1001: for (int i = 0; i < n; i++) {
1002: if (ar[i] == null) {
1003: out.writeByte(0);
1004: continue;
1005: } else {
1006: out.writeByte(1);
1007: }
1008: out.writeShort(((Short) ar[i]).shortValue());
1009: }
1010: break;
1011: case MDStatics.STRING:
1012: for (int i = 0; i < n; i++) {
1013: if (ar[i] == null) {
1014: out.writeByte(0);
1015: continue;
1016: } else {
1017: out.writeByte(1);
1018: }
1019: out.writeUTF((String) ar[i]);
1020: }
1021: break;
1022: case MDStatics.BOOLEANW:
1023: case MDStatics.BOOLEAN:
1024: for (int i = 0; i < n; i++) {
1025: if (ar[i] == null) {
1026: out.writeByte(0);
1027: continue;
1028: } else {
1029: out.writeByte(1);
1030: }
1031: out.writeBoolean(((Boolean) ar[i]).booleanValue());
1032: }
1033: break;
1034: case MDStatics.BYTEW:
1035: case MDStatics.BYTE:
1036: for (int i = 0; i < n; i++) {
1037: if (ar[i] == null) {
1038: out.writeByte(0);
1039: continue;
1040: } else {
1041: out.writeByte(1);
1042: }
1043: out.writeByte(((Byte) ar[i]).byteValue());
1044: }
1045: break;
1046: case MDStatics.DOUBLEW:
1047: case MDStatics.DOUBLE:
1048: for (int i = 0; i < n; i++) {
1049: if (ar[i] == null) {
1050: out.writeByte(0);
1051: continue;
1052: } else {
1053: out.writeByte(1);
1054: }
1055: out.writeDouble(((Double) ar[i]).doubleValue());
1056: }
1057: break;
1058: case MDStatics.FLOATW:
1059: case MDStatics.FLOAT:
1060: for (int i = 0; i < n; i++) {
1061: if (ar[i] == null) {
1062: out.writeByte(0);
1063: continue;
1064: } else {
1065: out.writeByte(1);
1066: }
1067: out.writeFloat(((Float) ar[i]).floatValue());
1068: }
1069: break;
1070: case MDStatics.LONGW:
1071: case MDStatics.LONG:
1072: for (int i = 0; i < n; i++) {
1073: if (ar[i] == null) {
1074: out.writeByte(0);
1075: continue;
1076: } else {
1077: out.writeByte(1);
1078: }
1079: out.writeLong(((Long) ar[i]).longValue());
1080: }
1081: break;
1082: case MDStatics.DATE:
1083: for (int i = 0; i < n; i++) {
1084: if (ar[i] == null) {
1085: out.writeByte(0);
1086: continue;
1087: } else {
1088: out.writeByte(1);
1089: }
1090: out.writeLong(((Long) ar[i]).longValue());
1091: }
1092: break;
1093: case MDStatics.LOCALE:
1094: break;
1095: case MDStatics.BIGDECIMAL:
1096: break;
1097: case MDStatics.BIGINTEGER:
1098: break;
1099: default:
1100: throw BindingSupportImpl.getInstance().internal(
1101: "writeColObjectArray for " + typeCode
1102: + " is not supported");
1103: }
1104: }
1105:
1106: private static Object[] readColObjectArray(int typeCode,
1107: ObjectInput in) throws IOException {
1108: final int n = in.readInt();
1109: final Object[] ar = new Object[n];
1110: switch (typeCode) {
1111: case MDStatics.INTW:
1112: case MDStatics.INT:
1113: for (int i = 0; i < n; i++) {
1114: if (in.readByte() == 0)
1115: continue;
1116: ar[i] = new Integer(in.readInt());
1117: }
1118: break;
1119: case MDStatics.CHARW:
1120: case MDStatics.CHAR:
1121: for (int i = 0; i < n; i++) {
1122: if (in.readByte() == 0)
1123: continue;
1124: ar[i] = new Character(in.readChar());
1125: }
1126: break;
1127: case MDStatics.SHORTW:
1128: case MDStatics.SHORT:
1129: for (int i = 0; i < n; i++) {
1130: if (in.readByte() == 0)
1131: continue;
1132: ar[i] = new Short(in.readShort());
1133: }
1134: break;
1135: case MDStatics.STRING:
1136: for (int i = 0; i < n; i++) {
1137: if (in.readByte() == 0)
1138: continue;
1139: ar[i] = in.readUTF();
1140: }
1141: return ar;
1142: case MDStatics.BOOLEANW:
1143: case MDStatics.BOOLEAN:
1144: for (int i = 0; i < n; i++) {
1145: if (in.readByte() == 0)
1146: continue;
1147: ar[i] = new Boolean(in.readBoolean());
1148: }
1149: break;
1150: case MDStatics.BYTEW:
1151: case MDStatics.BYTE:
1152: for (int i = 0; i < n; i++) {
1153: if (in.readByte() == 0)
1154: continue;
1155: ar[i] = new Byte(in.readByte());
1156: }
1157: break;
1158: case MDStatics.DOUBLEW:
1159: case MDStatics.DOUBLE:
1160: for (int i = 0; i < n; i++) {
1161: if (in.readByte() == 0)
1162: continue;
1163: ar[i] = new Double(in.readDouble());
1164: }
1165: break;
1166: case MDStatics.FLOATW:
1167: case MDStatics.FLOAT:
1168: for (int i = 0; i < n; i++) {
1169: if (in.readByte() == 0)
1170: continue;
1171: ar[i] = new Float(in.readFloat());
1172: }
1173: break;
1174: case MDStatics.LONGW:
1175: case MDStatics.LONG:
1176: for (int i = 0; i < n; i++) {
1177: if (in.readByte() == 0)
1178: continue;
1179: ar[i] = new Long(in.readLong());
1180: }
1181: break;
1182: case MDStatics.DATE:
1183: for (int i = 0; i < n; i++) {
1184: if (in.readByte() == 0)
1185: continue;
1186: ar[i] = new Date(in.readLong());
1187: }
1188: break;
1189: case MDStatics.LOCALE:
1190: break;
1191: case MDStatics.BIGDECIMAL:
1192: break;
1193: case MDStatics.BIGINTEGER:
1194: break;
1195: default:
1196: throw BindingSupportImpl.getInstance().internal(
1197: "readColObjectArray for " + typeCode
1198: + " is not supported");
1199: }
1200: return ar;
1201: }
1202:
1203: public static void writeMapEntries(FieldMetaData fmd,
1204: OIDObjectOutput out, MapEntries data) throws IOException {
1205: if (data == null) {
1206: out.writeBoolean(true);
1207: return;
1208: } else {
1209: out.writeBoolean(false);
1210: }
1211: if (fmd.keyTypeMetaData != null) {
1212: writeOIDObjectArray(data.keys, out);
1213: } else {
1214: writeColObjectArray(data.keys, out, fmd.keyTypeCode);
1215: }
1216: if (fmd.elementTypeMetaData != null) {
1217: writeOIDObjectArray(data.values, out);
1218: } else {
1219: writeColObjectArray(data.values, out, fmd.elementTypeCode);
1220: }
1221: }
1222:
1223: public static MapEntries readMapEntries(FieldMetaData fmd,
1224: OIDObjectInput in) throws IOException,
1225: ClassNotFoundException {
1226: if (in.readBoolean() == true) {
1227: return null;
1228: }
1229: final MapEntries mapEntries = new MapEntries();
1230: if (fmd.keyTypeMetaData != null) {
1231: mapEntries.keys = readOIDObjectArray(in);
1232: } else {
1233: mapEntries.keys = readColObjectArray(fmd.keyTypeCode, in);
1234: }
1235: if (fmd.elementTypeMetaData != null) {
1236: mapEntries.values = readOIDObjectArray(in);
1237: } else {
1238: mapEntries.values = readColObjectArray(fmd.elementTypeCode,
1239: in);
1240: }
1241: return mapEntries;
1242: }
1243:
1244: private static final int COL_NULL = 0;
1245: private static final int COL_ARRAY = 1;
1246: private static final int COL_MAP_ENTRIES = 2;
1247: private static final int COL_DIFF_ORDERED = 3;
1248: private static final int COL_DIFF_UNORDERED = 4;
1249: private static final int COL_DIFF_MAP = 5;
1250:
1251: /**
1252: * Read the value of a collection or map field from the stream.
1253: * @see #writeCollectionOrMapField
1254: */
1255: public static Object readCollectionOrMapField(OIDObjectInput in,
1256: FieldMetaData fmd) throws IOException,
1257: ClassNotFoundException {
1258: CollectionDiff d;
1259: int code = in.readByte();
1260: switch (code) {
1261: case COL_NULL:
1262: return null;
1263: case COL_ARRAY:
1264: return SerUtils.readCollectionArray(fmd, in);
1265: case COL_MAP_ENTRIES:
1266: return SerUtils.readMapEntries(fmd, in);
1267: case COL_DIFF_UNORDERED:
1268: d = new UnorderedCollectionDiff(fmd);
1269: d.readExternal(in);
1270: return d;
1271: case COL_DIFF_ORDERED:
1272: d = new OrderedCollectionDiff(fmd);
1273: d.readExternal(in);
1274: return d;
1275: case COL_DIFF_MAP:
1276: d = new MapDiff(fmd);
1277: d.readExternal(in);
1278: return d;
1279: }
1280: throw BindingSupportImpl.getInstance().internal(
1281: "Unknown collection field code: " + code);
1282: }
1283:
1284: /**
1285: * Write the value of a collection or map field to the stream. This
1286: * can handle raw collection or map data as well as diffs.
1287: * @see #readCollectionOrMapField
1288: */
1289: public static void writeCollectionOrMapField(OIDObjectOutput out,
1290: FieldMetaData fmd, Object data) throws IOException {
1291: if (data instanceof CollectionDiff) {
1292: int code;
1293: if (data instanceof UnorderedCollectionDiff) {
1294: code = COL_DIFF_UNORDERED;
1295: } else if (data instanceof OrderedCollectionDiff) {
1296: code = COL_DIFF_ORDERED;
1297: } else if (data instanceof MapDiff) {
1298: code = COL_DIFF_MAP;
1299: } else {
1300: throw BindingSupportImpl.getInstance().internal(
1301: "Unknown CollectionDiff class: "
1302: + data.getClass().getName());
1303: }
1304: out.writeByte(code);
1305: ((CollectionDiff) data).writeExternal(out);
1306: } else if (data == null) {
1307: out.writeByte(COL_NULL);
1308: } else if (data instanceof MapEntries) {
1309: out.writeByte(COL_MAP_ENTRIES);
1310: SerUtils.writeMapEntries(fmd, out, (MapEntries) data);
1311: } else {
1312: out.writeByte(COL_ARRAY);
1313: SerUtils.writeCollectionArray(fmd, data, out);
1314: }
1315: }
1316:
1317: }
|