View Javadoc
1   package org.djutils.serialization.serializers;
2   
3   import org.djutils.serialization.EndianUtil;
4   import org.djutils.serialization.SerializationException;
5   
6   /**
7    * Serializer for simple classes.
8    * <p>
9    * Copyright (c) 2019-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
11   * <p>
12   * @version $Revision$, $LastChangedDate$, by $Author$, <br>
13   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
14   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
15   * @param <T> class
16   */
17  public abstract class ObjectSerializer<T extends Object> extends BasicSerializer<T>
18  {
19      /**
20       * Construct a new ObjectSerializer.
21       * @param type byte; the field type (returned by the <code>fieldType</code> method)
22       * @param dataClassName String; returned by the dataClassName method
23       */
24      public ObjectSerializer(final byte type, final String dataClassName)
25      {
26          super(type, dataClassName);
27      }
28  
29      @Override
30      public final int sizeWithPrefix(final T object) throws SerializationException
31      {
32          return 1 + size(object);
33      }
34  
35      @Override
36      public final void serializeWithPrefix(final T object, final byte[] buffer, final Pointer pointer,
37              final EndianUtil endianUtil) throws SerializationException
38      {
39          buffer[pointer.getAndIncrement(1)] = fieldType();
40          serialize(object, buffer, pointer, endianUtil);
41      }
42  
43      @Override
44      public final int getNumberOfDimensions()
45      {
46          return 0;
47      }
48  
49  }