001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.asn1;
021
022
023 import java.nio.ByteBuffer;
024
025 import org.apache.directory.shared.asn1.codec.DecoderException;
026 import org.apache.directory.shared.asn1.codec.EncoderException;
027
028
029 /**
030 * An abstract class which implements basic TLV operations.
031 *
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 * @version $Rev$, $Date$
034 */
035 public abstract class AbstractAsn1Object implements Asn1Object
036 {
037 // ~ Instance fields
038 // ----------------------------------------------------------------------------
039
040 /** The object's current length. It is used while decoding PDUs */
041 private int currentLength;
042
043 /** The object's expected length. It is used while decoding PDUs */
044 private int expectedLength;
045
046 /** The encapsulating Object */
047 protected AbstractAsn1Object parent;
048
049
050 /** The identifier of the associated TLV */
051 private int tlvId;
052
053 // ~ Methods
054 // ------------------------------------------------------------------------------------
055
056 /**
057 * Constructor associated with a TLV indentifier. Used when
058 * decoded a TLV, we create an association between the decode
059 * Asn1Object and the TLV which is the encoded form.
060 *
061 * @param tlvId The TLV Id.
062 */
063 protected AbstractAsn1Object( int tlvId )
064 {
065 this.tlvId = tlvId;
066 }
067
068
069 /**
070 * Default constructor. The TLV Id is set to -1. This constructor
071 * is called when an Asn1Object is created to be encoded, not decoded.
072 */
073 protected AbstractAsn1Object()
074 {
075 this.tlvId = -1;
076 }
077
078 /**
079 * Get the current object length, which is the sum of all inner length
080 * already decoded.
081 *
082 * @return The current object's length
083 */
084 public int getCurrentLength()
085 {
086 return currentLength;
087 }
088
089
090 /**
091 * Compute the object length, which is the sum of all inner length.
092 *
093 * @return The object's computed length
094 */
095 public abstract int computeLength();
096
097
098 /**
099 * Encode the object to a PDU.
100 *
101 * @param buffer The buffer where to put the PDU
102 * @return The PDU.
103 * @throws EncoderException if the buffer can't be encoded
104 */
105 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
106 {
107 return null;
108 }
109
110
111 /**
112 * Get the expected object length.
113 *
114 * @return The expected object's length
115 */
116 public int getExpectedLength()
117 {
118 return expectedLength;
119 }
120
121
122 /**
123 * Add a length to the object
124 *
125 * @param length
126 * The length to add.
127 * @throws DecoderException
128 * Thrown if the current length exceed the expected length
129 */
130 public void addLength( int length ) throws DecoderException
131 {
132 currentLength += length;
133
134 if ( currentLength > expectedLength )
135 {
136 throw new DecoderException( "Current Length is above expected Length" );
137 }
138 }
139
140
141 /**
142 * Set the expected length
143 *
144 * @param expectedLength
145 * The expectedLength to set.
146 */
147 public void setExpectedLength( int expectedLength )
148 {
149 this.expectedLength = expectedLength;
150 }
151
152
153 /**
154 * Set the current length
155 *
156 * @param currentLength
157 * The currentLength to set.
158 */
159 public void setCurrentLength( int currentLength )
160 {
161 this.currentLength = currentLength;
162 }
163
164
165 /**
166 * Get the parent
167 *
168 * @return Returns the parent.
169 */
170 public AbstractAsn1Object getParent()
171 {
172 return parent;
173 }
174
175
176 /**
177 * Set the parent
178 *
179 * @param parent
180 * The parent to set.
181 */
182 public void setParent( AbstractAsn1Object parent )
183 {
184 this.parent = parent;
185 }
186
187 /**
188 * @return The TLV identifier associated with this object
189 */
190 public int getTlvId()
191 {
192 return tlvId;
193 }
194 }