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   *    https://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   */
20  package org.apache.directory.api.asn1.ber;
21  
22  
23  import java.nio.Buffer;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.api.asn1.ber.grammar.Grammar;
27  import org.apache.directory.api.asn1.ber.grammar.States;
28  import org.apache.directory.api.asn1.ber.tlv.TLV;
29  import org.apache.directory.api.asn1.ber.tlv.TLVStateEnum;
30  
31  
32  /**
33   * This class is the abstract container used to store the current state of a PDU
34   * being decoded. It also stores the grammars used to decode the PDU, and all
35   * the informations needed to decode a PDU.
36   *
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
38   */
39  public abstract class AbstractContainer implements Asn1Container
40  {
41      /** All the possible grammars */
42      private Grammar<? extends Asn1Container> grammar;
43  
44      /** The current state of the decoding */
45      private TLVStateEnum state;
46  
47      /** The current transition */
48      private Enum<?> transition;
49  
50      /** The current TLV */
51      private TLV tlv;
52  
53      /** The parent TLV */
54      private TLV parentTLV;
55  
56      /** The grammar end transition flag */
57      private boolean grammarEndAllowed;
58  
59      /** A counter for the decoded bytes */
60      private int decodedBytes;
61  
62      /** The maximum allowed size for a PDU. Default to MAX int value */
63      private int maxPDUSize = Integer.MAX_VALUE;
64  
65      /** The incremental id used to tag TLVs */
66      private int id = 0;
67  
68      /** The Stream being decoded */
69      private ByteBuffer stream;
70  
71      /** A flag telling if the Value should be accumulated before being decoded
72       * for constructed types */
73      private boolean gathering = false;
74  
75  
76      /**
77       * Creates a new instance of AbstractContainer with a starting state.
78       *
79       */
80      protected AbstractContainer()
81      {
82          state = TLVStateEnum.TAG_STATE_START;
83      }
84  
85  
86      /**
87       * Creates a new instance of AbstractContainer with a starting state.
88       *
89       * @param stream the buffer containing the data to decode
90       */
91      protected AbstractContainer( ByteBuffer stream )
92      {
93          state = TLVStateEnum.TAG_STATE_START;
94          this.stream = stream;
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public Grammar<? extends Asn1Container> getGrammar()
103     {
104         return grammar;
105     }
106 
107 
108     /**
109      * {@inheritDoc}
110      */
111     @Override
112     public void setGrammar( Grammar<? extends Asn1Container> grammar )
113     {
114         this.grammar = grammar;
115     }
116 
117 
118     /**
119      * {@inheritDoc}
120      */
121     @Override
122     public TLVStateEnum getState()
123     {
124         return state;
125     }
126 
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public void setState( TLVStateEnum state )
133     {
134         this.state = state;
135     }
136 
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public boolean isGrammarEndAllowed()
143     {
144         return grammarEndAllowed;
145     }
146 
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public void setGrammarEndAllowed( boolean grammarEndAllowed )
153     {
154         this.grammarEndAllowed = grammarEndAllowed;
155     }
156 
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public Enum<?> getTransition()
163     {
164         return transition;
165     }
166 
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     public void setTransition( Enum<?> transition )
173     {
174         this.transition = transition;
175     }
176 
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     public void setCurrentTLV( TLV currentTLV )
183     {
184         this.tlv = currentTLV;
185     }
186 
187 
188     /**
189      * {@inheritDoc}
190      */
191     @Override
192     public TLV getCurrentTLV()
193     {
194         return this.tlv;
195     }
196 
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public TLV getParentTLV()
203     {
204         return parentTLV;
205     }
206 
207 
208     /**
209      * {@inheritDoc}
210      */
211     @Override
212     public void setParentTLV( TLV parentTLV )
213     {
214         this.parentTLV = parentTLV;
215     }
216 
217 
218     /**
219      * Clean the container for the next usage.
220      */
221     public void clean()
222     {
223         tlv = null;
224         parentTLV = null;
225         transition = ( ( States ) transition ).getStartState();
226         state = TLVStateEnum.TAG_STATE_START;
227     }
228 
229 
230     /**
231      * {@inheritDoc}
232      */
233     @Override
234     public int getNewTlvId()
235     {
236         return id++;
237     }
238 
239 
240     /**
241      * {@inheritDoc}
242      */
243     @Override
244     public int getTlvId()
245     {
246         return tlv.getId();
247     }
248 
249 
250     /**
251      * {@inheritDoc}
252      */
253     @Override
254     public int getDecodedBytes()
255     {
256         return decodedBytes;
257     }
258 
259 
260     /**
261      * {@inheritDoc}
262      */
263     @Override
264     public void setDecodedBytes( int decodedBytes )
265     {
266         this.decodedBytes = decodedBytes;
267     }
268 
269 
270     /**
271      * {@inheritDoc}
272      */
273     @Override
274     public void incrementDecodedBytes( int nb )
275     {
276         decodedBytes += nb;
277     }
278 
279 
280     /**
281      * {@inheritDoc}
282      */
283     @Override
284     public int getMaxPDUSize()
285     {
286         return maxPDUSize;
287     }
288 
289 
290     /**
291      * {@inheritDoc}
292      */
293     @Override
294     public void setMaxPDUSize( int maxPDUSize )
295     {
296         if ( maxPDUSize > 0 )
297         {
298             this.maxPDUSize = maxPDUSize;
299         }
300         else
301         {
302             this.maxPDUSize = Integer.MAX_VALUE;
303         }
304     }
305 
306 
307     /**
308      * {@inheritDoc}
309      */
310     @Override
311     public ByteBuffer getStream()
312     {
313         return stream;
314     }
315 
316 
317     /**
318      * {@inheritDoc}
319      */
320     @Override
321     public void setStream( ByteBuffer stream )
322     {
323         this.stream = stream;
324     }
325 
326 
327     /**
328      * {@inheritDoc}
329      */
330     @Override
331     public void rewind()
332     {
333         int start = stream.position() - 1 - tlv.getLengthNbBytes();
334         ( ( Buffer ) stream ).position( start );
335     }
336 
337 
338     /**
339      * {@inheritDoc}
340      */
341     @Override
342     public void updateParent()
343     {
344         TLV parentTlv = tlv.getParent();
345 
346         while ( ( parentTlv != null ) && ( parentTlv.getExpectedLength() == 0 ) )
347         {
348             parentTlv = parentTlv.getParent();
349         }
350 
351         this.parentTLV = parentTlv;
352     }
353 
354 
355     /**
356      * {@inheritDoc}
357      */
358     @Override
359     public boolean isGathering()
360     {
361         return gathering;
362     }
363 
364 
365     /**
366      * {@inheritDoc}
367      */
368     @Override
369     public void setGathering( boolean gathering )
370     {
371         this.gathering = gathering;
372     }
373 
374 }