View Javadoc

1   /*
2    *   Copyright 2005 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.asn1new;
18  
19  import java.nio.ByteBuffer;
20  
21  import org.apache.asn1.codec.DecoderException;
22  import org.apache.asn1.codec.EncoderException;
23  
24  
25  /***
26   * An abstract class which implements basic TLV operations.
27   * 
28   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
29   */
30  public abstract class Asn1Object
31  {
32      //~ Instance fields ----------------------------------------------------------------------------
33  
34      /*** The object's current length. It is used while decoding PDUs */
35      private transient int currentLength;
36  
37      /*** The object's expected length. It is used while decoding PDUs */
38      private transient int expectedLength;
39  
40      /*** The encapsulating Object */
41      protected transient Asn1Object parent;
42  
43      //~ Methods ------------------------------------------------------------------------------------
44  
45      /***
46       * Get the current object length, which is the sum of all inner length
47       * already decoded.
48       * 
49       * @return The current object's length
50       */
51      public int getCurrentLength()
52      {
53          return currentLength;
54      }
55  
56      /***
57       * Compute the object length, which is the sum of all inner length.
58       * 
59       * @return The object's computed length
60       */
61      public abstract int computeLength();
62  
63      /***
64       * Encode the object to a PDU.
65       * 
66       * @param buffer The buffer where to put the PDU
67       * @return The PDU.
68       */
69      public ByteBuffer encode( ByteBuffer buffer )  throws EncoderException {
70          return null;
71      }
72  
73      /***
74       * Get the expected object length.
75       * 
76       * @return The expected object's length
77       */
78      public int getExpectedLength()
79      {
80          return expectedLength;
81      }
82  
83      /***
84       * Add a length to the object
85       * 
86       * @param length The length to add.
87       * @throws DecoderException Thrown if the current length exceed the expected length
88       */
89      public void addLength( int length ) throws DecoderException
90      {
91          currentLength += length;
92  
93          if ( currentLength > expectedLength )
94          {
95              throw new DecoderException( "Current Length is above expected Length" );
96          }
97      }
98  
99      /***
100      * Set the expected length
101      *
102      * @param expectedLength The expectedLength to set.
103      */
104     public void setExpectedLength( int expectedLength )
105     {
106         this.expectedLength = expectedLength;
107     }
108 
109     /***
110      * Set the current length
111      *
112      * @param currentLength The currentLength to set.
113      */
114     public void setCurrentLength( int currentLength )
115     {
116         this.currentLength = currentLength;
117     }
118 
119     /***
120      * Get the parent 
121      *
122      * @return Returns the parent.
123      */
124     public Asn1Object getParent()
125     {
126         return parent;
127     }
128 
129     /***
130      * Set the parent
131      *
132      * @param parent The parent to set.
133      */
134     public void setParent( Asn1Object parent )
135     {
136         this.parent = parent;
137     }
138 }