Пример #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
  private static void writeValue(Object value, OutputStream os) throws IOException {

    if (value == null) {
      os.write(-1);
    } else {
      Class valueClass = value.getClass();
      // write 0 for a single value, 1 for array, and 2 for Vector
      if (valueClass.isArray()) {
        os.write(1);
        int length = Array.getLength(value);
        PDataStream.writeInt(length, os);
        Class componentType = valueClass.getComponentType();
        if (componentType.isPrimitive()) {
          // primitive
          PDataStream.writeBoolean(true, os);
          // 1: int;2: long; 3: byte; 4: boolean; 5: character; 6:
          // short; 7: float; 8: double
          writePrimitiveArray(componentType, value, length, os);
        } else {
          PDataStream.writeBoolean(false, os);
          PDataStream.writeUTF(componentType.getName(), os);
          Object[] oArr = (Object[]) value;
          for (int i = 0; i < length; i++) {
            writeValue(oArr[i], os);
          }
        }
      } else if (valueClass.equals(Vector.class)) {
        os.write(2);
        int size = ((Vector) value).size();
        PDataStream.writeInt(size, os);
        for (int i = 0; i < size; i++) {
          writeValue(((Vector) value).elementAt(i), os);
        }
      } else {
        os.write(0);
        writeRealObject(value, valueClass, os);
      }
    }
  }
Пример #3
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());
   }
 }