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.ber.tlv;
18
19 /**
20 * This class is used to store Tag, Length and Value decoded from a PDU.
21 *
22 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
23 */
24 public class TLV
25 {
26 //~ Static fields/initializers -----------------------------------------------------------------
27
28
29 //~ Instance fields ----------------------------------------------------------------------------
30
31 /** The current Tag being processed */
32 private Tag tag;
33
34 /** The current Length being processed */
35 private Length length;
36
37 /** The current Value being processed */
38 private Value value;
39
40 /** Reference the TLV which contains the current TLV, if any.
41 * As the enclosing TLV of a PDU does not have parent, it can be
42 * null in this case. Otherwise, it must point to a constructed TLV */
43 private TLV parent;
44
45 /** The expected length of the TLV's elements, if the current TLV is
46 * a constructed TLV.
47 */
48 private int expectedLength;
49
50 //~ Constructors -------------------------------------------------------------------------------
51
52 /**
53 * Creates a new TLV object.
54 */
55 public TLV()
56 {
57 tag = new Tag();
58 length = new Length();
59 value = new Value();
60
61 expectedLength = 0;
62 }
63
64 //~ Methods ------------------------------------------------------------------------------------
65
66 /**
67 * Reset the TLV, so it can be reused for the next PDU decoding.
68 */
69 public void reset()
70 {
71 tag.reset();
72 length.reset();
73 value.reset();
74
75 expectedLength = 0;
76 }
77
78 /**
79 * @return Returns the length.
80 */
81 public Length getLength()
82 {
83 return length;
84 }
85
86 /**
87 * Add the TLV Length part
88 *
89 * @param length The length to set.
90 */
91 public void setLength( Length length )
92 {
93 this.length = length;
94
95 expectedLength = length.getLength();
96 }
97
98 /**
99 * @return Returns the tag.
100 */
101 public Tag getTag()
102 {
103 return tag;
104 }
105
106 /**
107 * @return Returns the value.
108 */
109 public Value getValue()
110 {
111 return value;
112 }
113
114 /**
115 * Get a String representation of the TLV
116 *
117 * @return A String
118 */
119 public String toString()
120 {
121
122 StringBuffer sb = new StringBuffer();
123
124 sb.append( "TLV[ " );
125 sb.append( tag.toString() ).append( ", " );
126 sb.append( length.toString() ).append( ", " );
127 sb.append( value.toString() );
128 sb.append( "]" );
129
130 return sb.toString();
131 }
132
133 /**
134 * The TLV size is calculated by adding the Tag's size, the Length's
135 * size and the Value's length, if any.
136 *
137 * @return Returns the size of the TLV.
138 */
139 public int getSize() {
140 return tag.getSize() + length.getSize() + length.getLength();
141 }
142
143 /**
144 * @return Returns the parent.
145 */
146 public TLV getParent()
147 {
148 return parent;
149 }
150
151 /**
152 * @param parent The parent to set.
153 */
154 public void setParent(TLV parent)
155 {
156 this.parent = parent;
157 }
158
159 /**
160 * Get the TLV expected length.
161 *
162 * @return Returns the expectedLength.
163 */
164 public int getExpectedLength()
165 {
166 return expectedLength;
167 }
168
169 /**
170 * Set the new expected length of the current TLV.
171 * @param expectedLength The expectedLength to set.
172 */
173 public void setExpectedLength(int expectedLength)
174 {
175 this.expectedLength = expectedLength;
176 }
177 }