コード例 #1
0
  /**
   * Creates a new instance.
   *
   * @param data the data to be serialized
   * @return a new instance
   * @throws BatchRuntimeException if a failure to serialize the data occurs
   */
  public static SerializableData of(final Serializable data) {
    if (data instanceof SerializableData) {
      return (SerializableData) data;
    }
    if (data instanceof byte[]) {
      return new SerializableData((byte[]) data, null);
    }
    if (data == null) {
      return new SerializableData(null, null);
    }

    Class<?> c = data.getClass();
    if (c.isArray()) {
      c = c.getComponentType();
    }

    if (requiresSerialization(c)) {
      try {
        return new SerializableData(BatchUtil.objectToBytes(data), null);
      } catch (IOException e) {
        throw BatchMessages.MESSAGES.failedToSerialize(e, data);
      }
    }
    return new SerializableData(null, data);
  }