示例#1
0
  // 1: int;2: long; 3: byte; 4: boolean; 5: character;
  // 6: short; 7: float; 8: double
  private static void writePrimitiveArray(
      Class componentType, Object array, int length, OutputStream os) throws IOException {

    if (componentType.equals(Integer.TYPE)) {
      int[] ints = (int[]) array;
      os.write(1);
      for (int i = 0; i < length; i++) {
        PDataStream.writeInt(ints[i], os);
      }
    } else if (componentType.equals(Long.TYPE)) {
      os.write(2);
      long[] longs = (long[]) array;
      for (int i = 0; i < length; i++) {
        PDataStream.writeLong(longs[i], os);
      }
    } else if (componentType.equals(Byte.TYPE)) {
      os.write(3);
      os.write((byte[]) array);
    } else if (componentType.equals(Boolean.TYPE)) {
      os.write(4);
      boolean[] booleans = (boolean[]) array;
      for (int i = 0; i < length; i++) {
        PDataStream.writeBoolean(booleans[i], os);
      }
    } else if (componentType.equals(Character.TYPE)) {
      os.write(5);
      char[] chars = (char[]) array;
      for (int i = 0; i < length; i++) {
        PDataStream.writeChar(chars[i], os);
      }
    } else if (componentType.equals(Short.TYPE)) {
      os.write(6);
      short[] shorts = (short[]) array;
      for (int i = 0; i < length; i++) {
        PDataStream.writeShort(shorts[i], os);
      }
    } else if (componentType.equals(Float.TYPE)) {
      os.write(7);
      float[] floats = (float[]) array;
      for (int i = 0; i < length; i++) {
        PDataStream.writeFloat(floats[i], os);
      }
    } else if (componentType.equals(Double.TYPE)) {
      os.write(8);
      double[] doubles = (double[]) array;
      for (int i = 0; i < length; i++) {
        PDataStream.writeDouble(doubles[i], os);
      }
    } else {
      throw new IllegalArgumentException("Unsupported Primitive Type: " + componentType);
    }
  }
示例#2
0
 // only if this is an object (not primitive) and non null!
 private static void writeRealObject(Object value, Class vClass, OutputStream os)
     throws IOException {
   try {
     if (vClass.equals(String.class)) {
       os.write(0);
       PDataStream.writeUTF((String) value, os);
     } else if (vClass.equals(Integer.class)) {
       os.write(1);
       PDataStream.writeInt(((Integer) value).intValue(), os);
     } else if (vClass.equals(Long.class)) {
       os.write(2);
       PDataStream.writeLong(((Long) value).longValue(), os);
     } else if (vClass.equals(Byte.class)) {
       os.write(3);
       os.write(((Byte) value).byteValue());
     } else if (vClass.equals(Boolean.class)) {
       os.write(4);
       PDataStream.writeBoolean(((Boolean) value).booleanValue(), os);
     } else if (vClass.equals(Character.class)) {
       os.write(5);
       PDataStream.writeChar(((Character) value).charValue(), os);
     } else if (vClass.equals(Short.class)) {
       os.write(6);
       PDataStream.writeShort(((Short) value).shortValue(), os);
     } else if (vClass.equals(Float.class)) {
       os.write(7);
       PDataStream.writeFloat(((Float) value).floatValue(), os);
     } else if (vClass.equals(Double.class)) {
       os.write(8);
       PDataStream.writeDouble(((Double) value).doubleValue(), os);
     } else if (Externalizable.class.isAssignableFrom(vClass)) {
       os.write(11);
       String name = vClass.getName();
       PDataStream.writeUTF(name, os);
       Externalizable tmp = (Externalizable) value;
       tmp.writeObject(os);
     } else {
       os.write(12);
       ObjectOutputStream out = new ObjectOutputStream(os);
       out.writeObject(value);
     }
   } catch (Exception exc) {
     throw new IOException(exc.toString());
   }
 }