Пример #1
0
  public byte[] serialize(Object data) throws SharedMemorySerializationException {
    try {
      if (data == null) {
        return new byte[0];
      }
      int type = getType(data);
      switch (type) {
        case JAVA_OBJECT:
          {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(data);
            return byteArrayOutputStream.toByteArray();
          }
        case STRING:
          {
            return ((String) data).getBytes(DBProperties.getCharset());
          }
        case INTEGER:
          {
            byte[] array = new byte[SIZE_OF_INT];
            SerializationUtils.intByteArrayFiller((Integer) data, array, 0);
            return array;
          }
        default:
          throw new SharedMemorySerializationException(
              CommandStatus.unExpectedException, "Fail to serialize object : " + data);
      }

    } catch (IOException e) {
      throw new SharedMemorySerializationException(
          CommandStatus.unExpectedException, "Fail to serialize object : " + data);
    }
  }
Пример #2
0
 public Object deSerialize(byte[] entityData, Integer type)
     throws SharedMemorySerializationException {
   if (entityData == null || type == null || entityData.length == 0) {
     return null;
   }
   switch (type) {
     case JAVA_OBJECT:
       {
         try {
           ByteArrayInputStream inputStream = new ByteArrayInputStream(entityData);
           ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
           return objectInputStream.readObject();
         } catch (Exception e) {
           throw new SharedMemorySerializationException(
               CommandStatus.unExpectedException, "Fail to deSerialize object ");
         }
       }
     case STRING:
       {
         try {
           return new String(entityData, DBProperties.getCharset());
         } catch (Exception e) {
           throw new SharedMemorySerializationException(
               CommandStatus.unExpectedException, "Fail to deSerialize object ");
         }
       }
     case INTEGER:
       {
         try {
           return SerializationUtils.byteArrayToInt(entityData, 0);
         } catch (Exception e) {
           throw new SharedMemorySerializationException(
               CommandStatus.unExpectedException, "Fail to deSerialize object ");
         }
       }
     default:
       throw new SharedMemorySerializationException(
           CommandStatus.unExpectedException, "Fail to deSerialize object ");
   }
 }