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 */ 020package org.apache.directory.shared.asn1; 021 022 023import java.nio.ByteBuffer; 024 025import org.apache.directory.shared.i18n.I18n; 026 027 028/** 029 * An abstract class which implements basic TLV operations. 030 * 031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 032 */ 033public abstract class AbstractAsn1Object implements Asn1Object 034{ 035 /** The object's current length. It is used while decoding PDUs */ 036 private int currentLength; 037 038 /** The object's expected length. It is used while decoding PDUs */ 039 private int expectedLength; 040 041 /** The encapsulating Object */ 042 private AbstractAsn1Object parent; 043 044 /** The identifier of the associated TLV */ 045 private int tlvId; 046 047 048 /** 049 * Constructor associated with a TLV identifier. Used when 050 * decoded a TLV, we create an association between the decode 051 * Asn1Object and the TLV which is the encoded form. 052 * 053 * @param tlvId The TLV Id. 054 */ 055 protected AbstractAsn1Object( int tlvId ) 056 { 057 this.tlvId = tlvId; 058 } 059 060 061 /** 062 * Default constructor. The TLV Id is set to -1. This constructor 063 * is called when an Asn1Object is created to be encoded, not decoded. 064 */ 065 protected AbstractAsn1Object() 066 { 067 this.tlvId = -1; 068 } 069 070 071 /** 072 * Get the current object length, which is the sum of all inner length 073 * already decoded. 074 * 075 * @return The current object's length 076 */ 077 public int getCurrentLength() 078 { 079 return currentLength; 080 } 081 082 083 /** 084 * Compute the object length, which is the sum of all inner length. 085 * 086 * @return The object's computed length 087 */ 088 public abstract int computeLength(); 089 090 091 /** 092 * Encode the object to a PDU. 093 * 094 * @param buffer The buffer where to put the PDU 095 * @return The PDU. 096 * @throws EncoderException if the buffer can't be encoded 097 */ 098 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException 099 { 100 return null; 101 } 102 103 104 /** 105 * Get the expected object length. 106 * 107 * @return The expected object's length 108 */ 109 public int getExpectedLength() 110 { 111 return expectedLength; 112 } 113 114 115 /** 116 * Add a length to the object 117 * 118 * @param length The length to add. 119 * @throws DecoderException Thrown if the current length exceed the expected length 120 */ 121 public void addLength( int length ) throws DecoderException 122 { 123 currentLength += length; 124 125 if ( currentLength > expectedLength ) 126 { 127 throw new DecoderException( I18n.err( I18n.ERR_00041_CURRENT_LENGTH_EXCEED_EXPECTED_LENGTH ) ); 128 } 129 } 130 131 132 /** 133 * Set the expected length 134 * 135 * @param expectedLength The expectedLength to set. 136 */ 137 public void setExpectedLength( int expectedLength ) 138 { 139 this.expectedLength = expectedLength; 140 } 141 142 143 /** 144 * Set the current length 145 * 146 * @param currentLength The currentLength to set. 147 */ 148 public void setCurrentLength( int currentLength ) 149 { 150 this.currentLength = currentLength; 151 } 152 153 154 /** 155 * Get the parent 156 * 157 * @return Returns the parent. 158 */ 159 public AbstractAsn1Object getParent() 160 { 161 return parent; 162 } 163 164 165 /** 166 * Set the parent 167 * 168 * @param parent The parent to set. 169 */ 170 public void setParent( AbstractAsn1Object parent ) 171 { 172 this.parent = parent; 173 } 174 175 176 /** 177 * @return The TLV identifier associated with this object 178 */ 179 public int getTlvId() 180 { 181 return tlvId; 182 } 183}