public Object readObject(final ObjectDataInput in) {
   try {
     final boolean isNull = in.readBoolean();
     if (isNull) {
       return null;
     }
     final int typeId = in.readInt();
     final SerializerAdapter serializer = serializerFor(typeId);
     if (serializer == null) {
       if (active) {
         throw new HazelcastSerializationException(
             "There is no suitable de-serializer for type " + typeId);
       }
       throw new HazelcastInstanceNotActiveException();
     }
     if (typeId == SerializationConstants.CONSTANT_TYPE_PORTABLE
         && in instanceof PortableContextAwareInputStream) {
       ClassDefinition classDefinition = new ClassDefinitionImpl();
       classDefinition.readData(in);
       classDefinition = serializationContext.registerClassDefinition(classDefinition);
       PortableContextAwareInputStream ctxIn = (PortableContextAwareInputStream) in;
       ctxIn.setClassDefinition(classDefinition);
     }
     Object obj = serializer.read(in);
     if (managedContext != null) {
       obj = managedContext.initialize(obj);
     }
     return obj;
   } catch (Throwable e) {
     handleException(e);
   }
   return null;
 }
 public void writeObject(final ObjectDataOutput out, final Object obj) {
   final boolean isNull = obj == null;
   try {
     out.writeBoolean(isNull);
     if (isNull) {
       return;
     }
     final SerializerAdapter serializer = serializerFor(obj.getClass());
     if (serializer == null) {
       if (active) {
         throw new HazelcastSerializationException(
             "There is no suitable serializer for " + obj.getClass());
       }
       throw new HazelcastInstanceNotActiveException();
     }
     out.writeInt(serializer.getTypeId());
     if (obj instanceof Portable) {
       final Portable portable = (Portable) obj;
       ClassDefinition classDefinition =
           serializationContext.lookupOrRegisterClassDefinition(portable);
       classDefinition.writeData(out);
     }
     serializer.write(out, obj);
   } catch (Throwable e) {
     handleException(e);
   }
 }
  public <T> T toObject(final Object object) {
    if (!(object instanceof Data)) {
      return (T) object;
    }

    Data data = (Data) object;

    if (data.bufferSize() == 0 && data.isDataSerializable()) {
      return null;
    }
    try {
      final int typeId = data.type;
      final SerializerAdapter serializer = serializerFor(typeId);
      if (serializer == null) {
        if (active) {
          throw new HazelcastSerializationException(
              "There is no suitable de-serializer for type " + typeId);
        }
        throw new HazelcastInstanceNotActiveException();
      }
      if (typeId == SerializationConstants.CONSTANT_TYPE_PORTABLE) {
        serializationContext.registerClassDefinition(data.classDefinition);
      }
      Object obj = serializer.read(data);
      if (managedContext != null) {
        obj = managedContext.initialize(obj);
      }
      return (T) obj;
    } catch (Throwable e) {
      handleException(e);
    }
    return null;
  }
 private void registerClassDefinition(
     ClassDefinition cd, Map<Integer, ClassDefinition> classDefMap, boolean checkClassDefErrors) {
   for (int i = 0; i < cd.getFieldCount(); i++) {
     FieldDefinition fd = cd.get(i);
     if (fd.getType() == FieldType.PORTABLE || fd.getType() == FieldType.PORTABLE_ARRAY) {
       int classId = fd.getClassId();
       ClassDefinition nestedCd = classDefMap.get(classId);
       if (nestedCd != null) {
         ((ClassDefinitionImpl) cd).addClassDef(nestedCd);
         registerClassDefinition(nestedCd, classDefMap, checkClassDefErrors);
         serializationContext.registerClassDefinition(nestedCd);
       } else if (checkClassDefErrors) {
         throw new HazelcastSerializationException(
             "Could not find registered ClassDefinition for class-id: " + classId);
       }
     }
   }
   serializationContext.registerClassDefinition(cd);
 }
 @SuppressWarnings("unchecked")
 public Data toData(Object obj, PartitioningStrategy strategy) {
   if (obj == null) {
     return null;
   }
   if (obj instanceof Data) {
     return (Data) obj;
   }
   try {
     final SerializerAdapter serializer = serializerFor(obj.getClass());
     if (serializer == null) {
       if (active) {
         throw new HazelcastSerializationException(
             "There is no suitable serializer for " + obj.getClass());
       }
       throw new HazelcastInstanceNotActiveException();
     }
     final byte[] bytes = serializer.write(obj);
     final Data data = new Data(serializer.getTypeId(), bytes);
     if (obj instanceof Portable) {
       final Portable portable = (Portable) obj;
       data.classDefinition =
           serializationContext.lookup(portable.getFactoryId(), portable.getClassId());
     }
     if (strategy == null) {
       strategy = globalPartitioningStrategy;
     }
     if (strategy != null) {
       Object pk = strategy.getPartitionKey(obj);
       if (pk != null && pk != obj) {
         final Data partitionKey = toData(pk, EMPTY_PARTITIONING_STRATEGY);
         data.partitionHash = (partitionKey == null) ? -1 : partitionKey.getPartitionHash();
       }
     }
     return data;
   } catch (Throwable e) {
     handleException(e);
   }
   return null;
 }
  SerializationServiceImpl(
      InputOutputFactory inputOutputFactory,
      int version,
      ClassLoader classLoader,
      Map<Integer, ? extends DataSerializableFactory> dataSerializableFactories,
      Map<Integer, ? extends PortableFactory> portableFactories,
      Collection<ClassDefinition> classDefinitions,
      boolean checkClassDefErrors,
      ManagedContext managedContext,
      PartitioningStrategy partitionStrategy,
      int initialOutputBufferSize,
      boolean enableCompression,
      boolean enableSharedObject) {

    this.inputOutputFactory = inputOutputFactory;
    this.classLoader = classLoader;
    this.managedContext = managedContext;
    this.globalPartitioningStrategy = partitionStrategy;
    this.outputBufferSize = initialOutputBufferSize;

    PortableHookLoader loader = new PortableHookLoader(portableFactories, classLoader);
    serializationContext =
        new SerializationContextImpl(this, loader.getFactories().keySet(), version);
    for (ClassDefinition cd : loader.getDefinitions()) {
      serializationContext.registerClassDefinition(cd);
    }

    dataSerializerAdapter =
        new StreamSerializerAdapter(
            this, new DataSerializer(dataSerializableFactories, classLoader));
    portableSerializer = new PortableSerializer(serializationContext, loader.getFactories());
    portableSerializerAdapter = new StreamSerializerAdapter(this, portableSerializer);

    registerConstant(DataSerializable.class, dataSerializerAdapter);
    registerConstant(Portable.class, portableSerializerAdapter);
    registerConstant(Byte.class, new ByteSerializer());
    registerConstant(Boolean.class, new BooleanSerializer());
    registerConstant(Character.class, new CharSerializer());
    registerConstant(Short.class, new ShortSerializer());
    registerConstant(Integer.class, new IntegerSerializer());
    registerConstant(Long.class, new LongSerializer());
    registerConstant(Float.class, new FloatSerializer());
    registerConstant(Double.class, new DoubleSerializer());
    registerConstant(byte[].class, new TheByteArraySerializer());
    registerConstant(char[].class, new CharArraySerializer());
    registerConstant(short[].class, new ShortArraySerializer());
    registerConstant(int[].class, new IntegerArraySerializer());
    registerConstant(long[].class, new LongArraySerializer());
    registerConstant(float[].class, new FloatArraySerializer());
    registerConstant(double[].class, new DoubleArraySerializer());
    registerConstant(String.class, new StringSerializer());

    safeRegister(Date.class, new DateSerializer());
    safeRegister(BigInteger.class, new BigIntegerSerializer());
    safeRegister(BigDecimal.class, new BigDecimalSerializer());
    safeRegister(Externalizable.class, new Externalizer(enableCompression));
    safeRegister(Serializable.class, new ObjectSerializer(enableSharedObject, enableCompression));
    safeRegister(Class.class, new ClassSerializer());
    safeRegister(Enum.class, new EnumSerializer());

    registerClassDefinitions(classDefinitions, checkClassDefErrors);
  }