View Javadoc
1   package org.djutils.serialization;
2   
3   import org.djutils.exceptions.Throw;
4   
5   /**
6    * Serializable name for the next item in the stream, data, whatever. This serializer serializes strings that are no longer than
7    * 255 characters and contain only one-byte ASCII characters.
8    * <p>
9    * Copyright (c) 2019-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://sim0mq.org/docs/current/license.html">OpenTrafficSim License</a>.
11   * </p>
12   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
14   */
15  public class ItemName implements Serializer<String>
16  {
17      /** The string (not final; because it can be modified by calling the deSerialize method). */
18      private String string;
19  
20      /**
21       * Construct a new ItemName.
22       * @param string String; name of the item
23       * @throws SerializationException when the <code>string</code> is too long, or contains illegal characters
24       */
25      public ItemName(final String string) throws SerializationException
26      {
27          Throw.when(string.length() > 255, SerializationException.class, "ItemName may not be longer than 255 characters");
28          for (int i = 0; i < string.length(); i++)
29          {
30              Throw.when(string.charAt(i) > 255, SerializationException.class, "Character at position %d is out of range", i);
31          }
32          this.string = string;
33      }
34  
35      /**
36       * Construct a new ItemName from serialized data.
37       * @param buffer byte[]; the data
38       * @param pointer Pointer; position in the data
39       */
40      public ItemName(final byte[] buffer, final Pointer pointer)
41      {
42          deSerialize(buffer, pointer, null);
43      }
44  
45      /** {@inheritDoc} */
46      @Override
47      public int size(final String object) throws SerializationException
48      {
49          return 1 + this.string.length();
50      }
51  
52      /** {@inheritDoc} */
53      @Override
54      public int sizeWithPrefix(final String object) throws SerializationException
55      {
56          return 1 + size(object);
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public byte fieldType()
62      {
63          return 33;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public void serialize(final String object, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
69              throws SerializationException
70      {
71          buffer[pointer.getAndIncrement(1)] = (byte) this.string.length();
72          for (int i = 0; i < this.string.length(); i++)
73          {
74              buffer[pointer.getAndIncrement(1)] = (byte) (this.string.charAt(i) & 0xff);
75          }
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public void serializeWithPrefix(final String object, final byte[] buffer, final Pointer pointer,
81              final EndianUtil endianUtil) throws SerializationException
82      {
83          buffer[pointer.getAndIncrement(1)] = fieldType();
84          serialize(object, buffer, pointer, endianUtil);
85      }
86  
87      /** {@inheritDoc} */
88      @Override
89      public String deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
90      {
91          int length = buffer[pointer.getAndIncrement(1)] & 0xff;
92          char[] chars = new char[length];
93          for (int i = 0; i < length; i++)
94          {
95              chars[i] = (char) (buffer[pointer.getAndIncrement(1)] & 0xff);
96          }
97          this.string = new String(chars);
98          return this.string;
99      }
100 
101     /** {@inheritDoc} */
102     @Override
103     public String dataClassName()
104     {
105         return "Name";
106     }
107 
108     @Override
109     public final int getNumberOfDimensions()
110     {
111         return 0;
112     }
113 
114 }