/* (non-Javadoc)
  * @see java.lang.Object#equals(java.lang.Object)
  */
 @Override
 public boolean equals(Object obj) {
   if (this == obj) return true;
   if (obj == null) return false;
   if (getClass() != obj.getClass()) return false;
   Externalizable other = (Externalizable) obj;
   try {
     return toKey().equals(other.toKey());
   } catch (UndefinedKeyException e) {
     return false;
   }
 }
Example #2
0
 private static Object readRealObject(byte type, InputStream is, ClassLoader loader)
     throws IOException {
   try {
     if (type == 0) {
       return PDataStream.readUTF(is);
     } else if (type == 1) {
       return new Integer(PDataStream.readInt(is));
     } else if (type == 2) {
       return new Long(PDataStream.readLong(is));
     } else if (type == 3) {
       return new Byte((byte) is.read());
     } else if (type == 4) {
       return PDataStream.readBoolean(is) ? Boolean.TRUE : Boolean.FALSE;
     } else if (type == 5) {
       return new Character(PDataStream.readChar(is));
     } else if (type == 6) {
       return new Short(PDataStream.readShort(is));
     } else if (type == 7) {
       return new Float(PDataStream.readFloat(is));
     } else if (type == 8) {
       return new Double(PDataStream.readDouble(is));
     } else if (type == 11) {
       String name = PDataStream.readUTF(is);
       Class c = loader == null ? Class.forName(name) : loader.loadClass(name);
       if (Externalizable.class.isAssignableFrom(c)) {
         Externalizable obj = (Externalizable) c.newInstance();
         obj.readObject(is);
         return obj;
       }
       throw new IOException("Could not read object " + name);
     } else if (type == 12) {
       ObjectInputStream in =
           loader == null
               ? new ObjectInputStream(is)
               : (ObjectInputStream) new XObjectInputStream(loader, is);
       return in.readObject();
     }
   } catch (ClassNotFoundException cnfe) {
     throw new IOException("Could not find class " + cnfe.toString());
   } catch (Exception exc) {
     throw exc instanceof IOException
         ? (IOException) exc
         : new IOException("Could not read object " + exc.toString());
   }
   throw new IllegalArgumentException("Unsupported Typed Object: " + type);
 }
Example #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());
   }
 }
 public void defaultWriteObject(Object toWrite, FSTClazzInfo serializationInfo)
     throws IOException {
   if (serializationInfo.isExternalizable()) {
     codec.ensureFree(writeExternalWriteAhead);
     ((Externalizable) toWrite).writeExternal(this);
   } else {
     FSTClazzInfo.FSTFieldInfo[] fieldInfo = serializationInfo.getFieldInfo();
     writeObjectFields(toWrite, serializationInfo, fieldInfo, 0, 0);
   }
 }
Example #5
0
 /**
  * Serializes an object into a byte array.
  *
  * @param object the object
  * @param serializeDirectly true if the given object is either Serializable or Externalizable and
  *     if the object should not be instantiated using the default constructor when the object is
  *     deserialized
  * @return the serialized data
  * @throws IOException when serialization fails
  * @see #deserialize(byte[])
  * @see #deserialize(byte[], Externalizable)
  */
 public static byte[] serialize(Object object, boolean serializeDirectly) throws IOException {
   ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
   DataOutputStream out = new DataOutputStream(byteOut);
   if (serializeDirectly) {
     ((Externalizable) object).write(out);
   } else {
     serialize(object, out);
   }
   byte[] data = byteOut.toByteArray();
   return data;
 }
  private void writeExternalData(Externalizable obj) throws IOException {
    Object oldObj = curObj;
    ObjectStreamClass oldDesc = curDesc;
    PutFieldImpl oldPut = curPut;
    curObj = obj;
    curDesc = null;
    curPut = null;

    obj.writeExternal(this);
    dos.writeMarker();

    curObj = oldObj;
    curDesc = oldDesc;
    curPut = oldPut;
  }
Example #7
0
 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
   in.readByte(); // ignore version for now
   fieldNumber = in.readShort();
   byte fieldType;
   ISOComponent c;
   try {
     while ((fieldType = in.readByte()) != 'E') {
       c = null;
       switch (fieldType) {
         case 'F':
           c = new ISOField();
           break;
         case 'A':
           c = new ISOAmount();
           break;
         case 'B':
           c = new ISOBinaryField();
           break;
         case 'M':
           c = new ISOMsg();
           break;
         case 'H':
           readHeader(in);
           break;
         case 'P':
           readPackager(in);
           break;
         case 'D':
           readDirection(in);
           break;
         default:
           throw new IOException("malformed ISOMsg");
       }
       if (c != null) {
         ((Externalizable) c).readExternal(in);
         set(c);
       }
     }
   } catch (ISOException e) {
     throw new IOException(e.getMessage());
   }
 }
Example #8
0
 private void writeExternal(ObjectOutput out, char b, ISOComponent c) throws IOException {
   out.writeByte(b);
   ((Externalizable) c).writeExternal(out);
 }
Example #9
0
  /**
   * Deserializes an object from the given stream.
   *
   * @param in the data input stream, from which the object is deserialized
   * @return the serializable object
   * @throws IOException when serialization data could not be read or the Serializable class could
   *     not get instantiated
   */
  public static Object deserialize(DataInputStream in) throws IOException {
    byte version = in.readByte();
    // #if polish.debug.warn
    if (version > VERSION) {
      // #debug warn
      System.out.println(
          "Warning: trying to deserialize class that has been serialized with a newer version ("
              + version
              + ">"
              + VERSION
              + ").");
    }
    // #endif
    boolean isNull = in.readBoolean();
    if (isNull) {
      return null;
    }
    byte type = in.readByte();
    switch (type) {
      case TYPE_EXTERNALIZABLE:
        String className = in.readUTF();
        // #if polish.JavaSE
        if (obfuscationDeserializeMap != null) {
          String fullClassName = (String) obfuscationDeserializeMap.get(className);
          if (fullClassName != null) {
            // System.out.println("Serializer.deserialize: translating classname from " + className
            // + " to " +  fullClassName );
            className = fullClassName;
          }
        }
        // #endif
        // #debug debug
        // #= System.out.println("deserialize " + className + "...");
        Externalizable extern = null;
        try {
          extern = (Externalizable) Class.forName(className).newInstance();
        } catch (Exception e) {
          // #debug error
          System.out.println("Unable to instantiate serializable \"" + className + "\"" + e);
          throw new IOException(e.toString());
        }
        extern.read(in);
        return extern;
      case TYPE_EXTERNALIZABLE_ARRAY:
        String cn = in.readUTF();
        // #if polish.JavaSE
        if (obfuscationDeserializeMap != null) {
          String fullClassName = (String) obfuscationDeserializeMap.get(cn);
          if (fullClassName != null) {
            // System.out.println("Serializer.deserialize: translating classname from " + cn + " to
            // " +  fullClassName );
            cn = fullClassName;
          }
        }
        // #endif

        int length = in.readInt();
        Externalizable[] externalizables;

        // #if !polish.JavaSE
        externalizables = new Externalizable[length];
        // #else
        try {
          // #if false
          externalizables = null;
          // #else
          // # externalizables = (Externalizable[]) Array.newInstance(Class.forName( cn ), length);
          // #endif
        } catch (Exception e) {
          // #debug error
          System.out.println("Unable to instantiate Serializable \"" + cn + "\"" + e);
          throw new IOException(e.toString());
        }
        // #endif

        Class[] classes = new Class[Math.min(length, 7)];
        Class currentClass;
        byte idCounter = 0;
        for (int i = 0; i < externalizables.length; i++) {
          int classId = in.readByte();
          if (classId == 0) { // new class name
            className = in.readUTF();
            // #if polish.JavaSE
            if (obfuscationDeserializeMap != null) {
              String fullClassName = (String) obfuscationDeserializeMap.get(className);
              if (fullClassName != null) {
                // System.out.println("Serializer.deserialize: translating classname from " +
                // className + " to " +  fullClassName );
                className = fullClassName;
              }
            }
            // #endif
            try {
              currentClass = Class.forName(className);
            } catch (ClassNotFoundException e) {
              // #debug error
              System.out.println("Unable to load Serializable class \"" + className + "\"" + e);
              throw new IOException(e.toString());
            }
            if (idCounter > classes.length) {
              Class[] newClasses = new Class[classes.length + 7];
              System.arraycopy(classes, 0, newClasses, 0, classes.length);
              classes = newClasses;
            }
            classes[idCounter] = currentClass;
            idCounter++;
          } else {
            currentClass = classes[classId - 1];
          }
          Externalizable externalizable;
          try {
            externalizable = (Externalizable) currentClass.newInstance();
            externalizable.read(in);
            externalizables[i] = externalizable;
          } catch (Exception e) {
            // #debug error
            System.out.println(
                "Unable to instantiate Serializable \"" + currentClass.getName() + "\"" + e);
            throw new IOException(e.toString());
          }
        }
        return externalizables;
      case TYPE_OBJECT_ARRAY:
        length = in.readInt();
        Object[] objects = new Object[length];
        for (int i = 0; i < objects.length; i++) {
          objects[i] = deserialize(in);
        }
        return objects;
      case TYPE_BYTE:
        return new Byte(in.readByte());
      case TYPE_SHORT:
        return new Short(in.readShort());
      case TYPE_INTEGER:
        return new Integer(in.readInt());
      case TYPE_LONG:
        return new Long(in.readLong());
        // #if polish.hasFloatingPoint
      case TYPE_FLOAT:
        return new Float(in.readFloat());
      case TYPE_DOUBLE:
        return new Double(in.readDouble());
        // #endif
      case TYPE_STRING:
        return in.readUTF();
      case TYPE_STRING_BUFFER:
        return new StringBuffer(in.readUTF());
      case TYPE_CHARACTER:
        return new Character(in.readChar());
      case TYPE_BOOLEAN:
        return new Boolean(in.readBoolean());
      case TYPE_DATE:
        return new Date(in.readLong());
      case TYPE_CALENDAR:
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(in.readLong()));
        return calendar;
      case TYPE_RANDOM:
        return new Random();
      case TYPE_HASHTABLE:
        int size = in.readInt();
        Hashtable hashtable = new Hashtable(size);
        for (int i = 0; i < size; i++) {
          Object key = deserialize(in);
          Object value = deserialize(in);
          hashtable.put(key, value);
        }
        return hashtable;
      case TYPE_STACK:
      case TYPE_VECTOR:
        size = in.readInt();
        Vector vector;
        if (type == TYPE_STACK) {
          vector = new Stack();
        } else {
          vector = new Vector(size);
        }
        for (int i = 0; i < size; i++) {
          Object value = deserialize(in);
          vector.addElement(value);
        }
        return vector;
        // #if polish.midp2
      case TYPE_IMAGE:
        byte subType = in.readByte();
        if (subType == TYPE_IMAGE_RGB) {
          int width = in.readInt();
          int height = in.readInt();
          int[] rgb = new int[width * height];
          for (int i = 0; i < rgb.length; i++) {
            rgb[i] = in.readInt();
          }
          return Image.createRGBImage(rgb, width, height, true);
        }

        // this is a bytes based format like png:
        int bytesLength = in.readInt();
        byte[] buffer = new byte[bytesLength];
        in.readFully(buffer);
        return Image.createImage(buffer, 0, bytesLength);
        // #endif
        // #if polish.midp
      case TYPE_FONT:
        int face = in.readInt();
        int style = in.readInt();
        size = in.readInt();
        return Font.getFont(face, style, size);
      case TYPE_COMMAND:
        int cmdType = in.readInt();
        int priority = in.readInt();
        String label = in.readUTF();
        return new Command(label, cmdType, priority);
        // #endif
      case TYPE_BYTE_ARRAY:
        length = in.readInt();
        byte[] byteNumbers = new byte[length];
        in.readFully(byteNumbers);
        return byteNumbers;
      case TYPE_SHORT_ARRAY:
        length = in.readInt();
        short[] shortNumbers = new short[length];
        for (int i = 0; i < length; i++) {
          shortNumbers[i] = in.readShort();
        }
        return shortNumbers;
      case TYPE_INT_ARRAY:
        length = in.readInt();
        int[] intNumbers = new int[length];
        for (int i = 0; i < length; i++) {
          intNumbers[i] = in.readInt();
        }
        return intNumbers;
      case TYPE_LONG_ARRAY:
        length = in.readInt();
        long[] longNumbers = new long[length];
        for (int i = 0; i < length; i++) {
          longNumbers[i] = in.readLong();
        }
        return longNumbers;
        // #if polish.hasFloatingPoint
      case TYPE_FLOAT_ARRAY:
        length = in.readInt();
        float[] floatNumbers = new float[length];
        for (int i = 0; i < length; i++) {
          floatNumbers[i] = in.readFloat();
        }
        return floatNumbers;
      case TYPE_DOUBLE_ARRAY:
        length = in.readInt();
        double[] doubleNumbers = new double[length];
        for (int i = 0; i < length; i++) {
          doubleNumbers[i] = in.readDouble();
        }
        return doubleNumbers;
        // #endif
      case TYPE_CHAR_ARRAY:
        length = in.readInt();
        char[] characters = new char[length];
        for (int i = 0; i < length; i++) {
          characters[i] = in.readChar();
        }
        return characters;
      case TYPE_BOOLEAN_ARRAY:
        length = in.readInt();
        boolean[] bools = new boolean[length];
        for (int i = 0; i < length; i++) {
          bools[i] = in.readBoolean();
        }
        return bools;
      case TYPE_STRING_ARRAY:
        length = in.readInt();
        String[] strings = new String[length];
        for (int i = 0; i < length; i++) {
          strings[i] = in.readUTF();
        }
        return strings;
      default:
        throw new IOException("Unknown type: " + type);
    }
  }
Example #10
0
 /**
  * WARNING: Can only be used in JavaSE environments! Serializes the specified object.
  *
  * @param object the object
  * @param out the stream into which the object should be serialized
  * @param useObfuscation true when classnames are obfuscated
  * @throws IOException when serialization data could not be written or when encountering an object
  *     that cannot be serialized
  */
 public static void serialize(Object object, DataOutputStream out, boolean useObfuscation)
     throws IOException {
   // #endif
   out.writeByte(VERSION);
   boolean isNull = (object == null);
   out.writeBoolean(isNull);
   if (!isNull) {
     if (object instanceof Externalizable) {
       out.writeByte(TYPE_EXTERNALIZABLE);
       String className = object.getClass().getName();
       // #if polish.JavaSE
       if (useObfuscation && obfuscationSerializeMap != null) {
         String obfuscatedClassName = (String) obfuscationSerializeMap.get(className);
         if (obfuscatedClassName != null) {
           // System.out.println("Serializer.serialize: translating classname from " + className +
           // " to " +  obfuscatedClassName +  " useObfuscationIndicator=" +
           // useObfuscationIndicator.get() );
           className = obfuscatedClassName;
         }
       }
       // #endif
       // #debug debug
       // #= System.out.println("serializing " + className + "=" + object);
       out.writeUTF(className);
       ((Externalizable) object).write(out);
     } else if (object instanceof Externalizable[]) {
       out.writeByte(TYPE_EXTERNALIZABLE_ARRAY);
       String cn = object.getClass().getName();
       cn = cn.substring(cn.lastIndexOf('[') + 2, cn.length() - 1);
       // #if polish.JavaSE
       if (useObfuscation && obfuscationSerializeMap != null) {
         String obfuscatedClassName = (String) obfuscationSerializeMap.get(cn);
         if (obfuscatedClassName != null) {
           // System.out.println("Serializer.serialize: translating classname from " + className +
           // " to " +  obfuscatedClassName );
           cn = obfuscatedClassName;
         }
       }
       // #endif
       out.writeUTF(cn);
       Externalizable[] externalizables = (Externalizable[]) object;
       out.writeInt(externalizables.length);
       Hashtable classNames = new Hashtable();
       Class lastClass = null;
       byte lastId = 0;
       byte idCounter = 0;
       for (int i = 0; i < externalizables.length; i++) {
         Externalizable externalizable = externalizables[i];
         Class currentClass = externalizable.getClass();
         if (currentClass == lastClass) {
           out.writeByte(lastId);
         } else {
           Byte knownId = (Byte) classNames.get(currentClass);
           if (knownId != null) {
             out.writeByte(knownId.byteValue());
           } else {
             // this is a class that has not yet been encountered:
             out.writeByte(0);
             idCounter++;
             String className = currentClass.getName();
             // #if polish.JavaSE
             if (useObfuscation && obfuscationSerializeMap != null) {
               String obfuscatedClassName = (String) obfuscationSerializeMap.get(className);
               if (obfuscatedClassName != null) {
                 // System.out.println("Serializer.serialize: translating classname from " +
                 // className + " to " +  obfuscatedClassName );
                 className = obfuscatedClassName;
               }
             }
             // #endif
             // #debug debug
             // #= System.out.println("serializing " + className + "=" + object);
             out.writeUTF(className);
             lastClass = currentClass;
             lastId = idCounter;
             classNames.put(currentClass, new Byte(lastId));
           }
         }
         externalizable.write(out);
       }
     } else if (object instanceof Object[]) {
       out.writeByte(TYPE_OBJECT_ARRAY);
       Object[] objects = (Object[]) object;
       out.writeInt(objects.length);
       for (int i = 0; i < objects.length; i++) {
         Object obj = objects[i];
         serialize(obj, out);
       }
     } else if (object instanceof Byte) {
       out.writeByte(TYPE_BYTE);
       out.writeByte(((Byte) object).byteValue());
     } else if (object instanceof Short) {
       out.writeByte(TYPE_SHORT);
       out.writeShort(((Short) object).shortValue());
     } else if (object instanceof Integer) {
       out.writeByte(TYPE_INTEGER);
       out.writeInt(((Integer) object).intValue());
     } else if (object instanceof Long) {
       out.writeByte(TYPE_LONG);
       out.writeLong(((Long) object).longValue());
       // #if polish.hasFloatingPoint
     } else if (object instanceof Float) {
       out.writeByte(TYPE_FLOAT);
       out.writeFloat(((Float) object).floatValue());
     } else if (object instanceof Double) {
       out.writeByte(TYPE_DOUBLE);
       out.writeDouble(((Double) object).doubleValue());
       // #endif
     } else if (object instanceof String) {
       out.writeByte(TYPE_STRING);
       out.writeUTF((String) object);
     } else if (object instanceof StringBuffer) {
       out.writeByte(TYPE_STRING_BUFFER);
       out.writeUTF(((StringBuffer) object).toString());
     } else if (object instanceof Character) {
       out.writeByte(TYPE_CHARACTER);
       out.writeChar(((Character) object).charValue());
     } else if (object instanceof Boolean) {
       out.writeByte(TYPE_BOOLEAN);
       out.writeBoolean(((Boolean) object).booleanValue());
     } else if (object instanceof Date) {
       out.writeByte(TYPE_DATE);
       out.writeLong(((Date) object).getTime());
     } else if (object instanceof Calendar) {
       out.writeByte(TYPE_CALENDAR);
       out.writeLong(((Calendar) object).getTime().getTime());
     } else if (object instanceof Random) {
       out.writeByte(TYPE_RANDOM);
     } else if (object instanceof Hashtable) {
       out.writeByte(TYPE_HASHTABLE);
       Hashtable table = (Hashtable) object;
       out.writeInt(table.size());
       Enumeration enumeration = table.keys();
       while (enumeration.hasMoreElements()) {
         Object key = enumeration.nextElement();
         serialize(key, out);
         Object value = table.get(key);
         serialize(value, out);
       }
     } else if (object instanceof Vector) { // also serializes stacks
       if (object instanceof Stack) {
         out.writeByte(TYPE_STACK);
       } else {
         out.writeByte(TYPE_VECTOR);
       }
       Vector vector = (Vector) object;
       int size = vector.size();
       out.writeInt(size);
       for (int i = 0; i < size; i++) {
         serialize(vector.elementAt(i), out);
       }
       // #if polish.midp2
     } else if (object instanceof Image) {
       out.writeByte(TYPE_IMAGE);
       // #if polish.JavaSE
       boolean handled = false;
       // we are within a Java SE environment. When the J2ME Polish runtime librarby is used, we
       // can
       // store the image in PNG format instead of in the much more verbose RGB format:
       try {
         // #if false
         Field bufferedImageField = null;
         // #else
         // # Field bufferedImageField = object.getClass().getDeclaredField("bufferedImage");
         // #endif
         bufferedImageField.setAccessible(true);
         BufferedImage bufferedImage = (BufferedImage) bufferedImageField.get(object);
         ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
         ImageIO.write(bufferedImage, "png", byteOut);
         out.writeByte(TYPE_IMAGE_BYTES);
         byte[] data = byteOut.toByteArray();
         out.writeInt(data.length);
         for (int i = 0; i < data.length; i++) {
           out.writeByte(data[i]);
         }
         handled = true;
       } catch (Exception e) {
         e.printStackTrace();
         // #debug warn
         System.out.println(
             "Warning: Unable to retrieve bufferedImage field of javax.microedition.lcdui.Image - probably the enough-polish-runtime library is not used.");
       }
       if (!handled) {
         // #endif
         Image image = (Image) object;
         out.writeByte(TYPE_IMAGE_RGB);
         int width = image.getWidth();
         int height = image.getHeight();
         out.writeInt(width);
         out.writeInt(height);
         int[] rgb = new int[width * height];
         image.getRGB(rgb, 0, width, 0, 0, width, height);
         for (int i = 0; i < rgb.length; i++) {
           out.writeInt(rgb[i]);
         }
         // #if polish.JavaSE
       }
       // #endif
       // #endif
       // #if polish.midp
     } else if (object instanceof Font) {
       out.writeByte(TYPE_FONT);
       Font font = (Font) object;
       out.writeInt(font.getFace());
       out.writeInt(font.getStyle());
       out.writeInt(font.getSize());
     } else if (object instanceof Command) {
       out.writeByte(TYPE_COMMAND);
       Command command = (Command) object;
       out.writeInt(command.getCommandType());
       out.writeInt(command.getPriority());
       out.writeUTF(command.getLabel());
       // #endif
     } else if (object instanceof byte[]) {
       out.writeByte(TYPE_BYTE_ARRAY);
       byte[] numbers = (byte[]) object;
       out.writeInt(numbers.length);
       out.write(numbers, 0, numbers.length);
     } else if (object instanceof short[]) {
       out.writeByte(TYPE_SHORT_ARRAY);
       short[] numbers = (short[]) object;
       out.writeInt(numbers.length);
       for (int i = 0; i < numbers.length; i++) {
         short number = numbers[i];
         out.writeShort(number);
       }
     } else if (object instanceof int[]) {
       out.writeByte(TYPE_INT_ARRAY);
       int[] numbers = (int[]) object;
       out.writeInt(numbers.length);
       for (int i = 0; i < numbers.length; i++) {
         int number = numbers[i];
         out.writeInt(number);
       }
     } else if (object instanceof long[]) {
       out.writeByte(TYPE_LONG_ARRAY);
       long[] numbers = (long[]) object;
       out.writeInt(numbers.length);
       for (int i = 0; i < numbers.length; i++) {
         long number = numbers[i];
         out.writeLong(number);
       }
       // #if polish.hasFloatingPoint
     } else if (object instanceof float[]) {
       out.writeByte(TYPE_FLOAT_ARRAY);
       float[] numbers = (float[]) object;
       out.writeInt(numbers.length);
       for (int i = 0; i < numbers.length; i++) {
         float number = numbers[i];
         out.writeFloat(number);
       }
     } else if (object instanceof double[]) {
       out.writeByte(TYPE_DOUBLE_ARRAY);
       double[] numbers = (double[]) object;
       out.writeInt(numbers.length);
       for (int i = 0; i < numbers.length; i++) {
         double number = numbers[i];
         out.writeDouble(number);
       }
       // #endif
     } else if (object instanceof char[]) {
       out.writeByte(TYPE_CHAR_ARRAY);
       char[] characters = (char[]) object;
       out.writeInt(characters.length);
       for (int i = 0; i < characters.length; i++) {
         char c = characters[i];
         out.writeChar(c);
       }
     } else if (object instanceof boolean[]) {
       out.writeByte(TYPE_BOOLEAN_ARRAY);
       boolean[] bools = (boolean[]) object;
       out.writeInt(bools.length);
       for (int i = 0; i < bools.length; i++) {
         boolean b = bools[i];
         out.writeBoolean(b);
       }
     } else if (object instanceof String[]) {
       out.writeByte(TYPE_STRING_ARRAY);
       String[] strings = (String[]) object;
       out.writeInt(strings.length);
       for (int i = 0; i < strings.length; i++) {
         String s = strings[i];
         out.writeUTF(s);
       }
     } else {
       throw new IOException("Cannot serialize " + object.getClass().getName());
     }
   }
 }
Example #11
0
 /**
  * Reads an object directly from a byte array
  *
  * @param data the serialized data
  * @return the original object
  * @throws IOException when serialization fails
  * @see #serialize(Object, boolean)
  */
 public static void deserialize(byte[] data, Externalizable object) throws IOException {
   ByteArrayInputStream byteIn = new ByteArrayInputStream(data);
   DataInputStream in = new DataInputStream(byteIn);
   object.read(in);
 }