View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.api.util;
20  
21  
22  /**
23   * A class containing static methods used to serialize and deserialize base types
24   * 
25   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
26   */
27  public final class Serialize
28  {
29      /** The serialized value for TRUE */
30      public static final byte TRUE = 0x01;
31  
32      /** The serialized value for FALSE */
33      public static final byte FALSE = 0x00;
34  
35  
36      private Serialize()
37      {
38      }
39  
40  
41      /**
42       * Write an integer into a buffer at a given position
43       * 
44       * @param value The value to serialize
45       * @param buffer The buffer to store the value into
46       * @param pos The position where we serialize the integer
47       * @return The new position in the byte[]
48       */
49      public static int serialize( int value, byte[] buffer, int pos )
50      {
51          if ( buffer.length - pos < 4 )
52          {
53              throw new ArrayIndexOutOfBoundsException();
54          }
55  
56          buffer[pos++] = ( byte ) ( ( value >>> 24 ) & 0xFF );
57          buffer[pos++] = ( byte ) ( ( value >>> 16 ) & 0xFF );
58          buffer[pos++] = ( byte ) ( ( value >>> 8 ) & 0xFF );
59          buffer[pos++] = ( byte ) ( ( value >>> 0 ) & 0xFF );
60  
61          return pos;
62      }
63  
64  
65      /**
66       * Write a byte[] into a buffer at a given position
67       * 
68       * @param value The value to serialize
69       * @param buffer The buffer to store the value into
70       * @param pos The position where we serialize the byte[]
71       * @return The new position in the byte[]
72       */
73      public static int serialize( byte[] value, byte[] buffer, int pos )
74      {
75          if ( buffer.length - pos < 4 + value.length )
76          {
77              throw new ArrayIndexOutOfBoundsException();
78          }
79  
80          buffer[pos++] = ( byte ) ( ( value.length >>> 24 ) & 0xFF );
81          buffer[pos++] = ( byte ) ( ( value.length >>> 16 ) & 0xFF );
82          buffer[pos++] = ( byte ) ( ( value.length >>> 8 ) & 0xFF );
83          buffer[pos++] = ( byte ) ( ( value.length >>> 0 ) & 0xFF );
84  
85          System.arraycopy( value, 0, buffer, pos, value.length );
86  
87          return pos + value.length;
88      }
89  
90  
91      /**
92       * Read an integer from a buffer at a given position
93       * 
94       * @param buffer The buffer containing the serialized integer
95       * @param pos The position from which we will read an integer
96       * @return the deserialized integer
97       */
98      public static int deserializeInt( byte[] buffer, int pos )
99      {
100         if ( buffer.length - pos < 4 )
101         {
102             throw new ArrayIndexOutOfBoundsException();
103         }
104 
105         return ( buffer[pos] << 24 ) + ( buffer[pos + 1] << 16 ) + ( buffer[pos + 2] << 8 ) + ( buffer[pos + 3] << 0 );
106     }
107 
108 
109     /**
110      * Read a byte[] from a buffer at a given position
111      * 
112      * @param buffer The buffer containing the serialized byte[]
113      * @param pos The position from which we will read a byte[]
114      * @return the deserialized byte[]
115      */
116     public static byte[] deserializeBytes( byte[] buffer, int pos )
117     {
118         if ( buffer.length - pos < 4 )
119         {
120             throw new ArrayIndexOutOfBoundsException();
121         }
122 
123         int len = deserializeInt( buffer, pos );
124         pos += 4;
125 
126         if ( len > 0 )
127         {
128             if ( buffer.length - pos < len )
129             {
130                 throw new ArrayIndexOutOfBoundsException();
131             }
132 
133             byte[] result = new byte[len];
134 
135             System.arraycopy( buffer, pos, result, 0, len );
136 
137             return result;
138         }
139         else
140         {
141             return Strings.EMPTY_BYTES;
142         }
143     }
144 
145 
146     /**
147      * Read a boolean from a buffer at a given position
148      * 
149      * @param buffer The buffer containing the serialized boolean
150      * @param pos The position from which we will read a boolean
151      * @return the deserialized boolean
152      */
153     public static boolean deserializeBoolean( byte[] buffer, int pos )
154     {
155         if ( buffer.length - pos < 1 )
156         {
157             throw new ArrayIndexOutOfBoundsException();
158         }
159 
160         byte value = buffer[pos];
161 
162         return ( value != 0x00 );
163     }
164 }