1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.shared.messages.value;
21
22
23 import java.nio.BufferOverflowException;
24 import java.nio.ByteBuffer;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.List;
28
29 import org.apache.directory.shared.asn1.AbstractAsn1Object;
30 import org.apache.directory.shared.asn1.ber.tlv.TLV;
31 import org.apache.directory.shared.asn1.ber.tlv.UniversalTag;
32 import org.apache.directory.shared.asn1.codec.EncoderException;
33 import org.apache.directory.shared.ldap.util.StringTools;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class HostAddresses extends AbstractAsn1Object
53 {
54
55 private static final Logger LOG = LoggerFactory.getLogger( HostAddresses.class );
56
57
58 private static final boolean IS_DEBUG = LOG.isDebugEnabled();
59
60
61 private List<HostAddress> addresses;
62
63
64 private transient int addressesLength;
65
66
67
68
69
70 public HostAddresses()
71 {
72 this.addresses = new ArrayList<HostAddress>();
73 }
74
75
76
77
78
79
80
81 public HostAddresses( HostAddress[] addresses )
82 {
83 if ( addresses == null )
84 {
85 this.addresses = new ArrayList<HostAddress>();
86 }
87 else
88 {
89 this.addresses = Arrays.asList( addresses );
90 }
91 }
92
93
94 public void addHostAddress( HostAddress hostAddress )
95 {
96 addresses.add( hostAddress );
97 }
98
99
100
101
102
103
104
105
106 public boolean contains( HostAddress address )
107 {
108 if ( addresses != null )
109 {
110 return addresses.contains( address );
111 }
112
113 return false;
114 }
115
116
117
118
119
120
121
122
123 public boolean equals( HostAddresses that )
124 {
125 if ( ( addresses == null && that.addresses != null )
126 || ( addresses != null && that.addresses == null ) )
127 {
128 return false;
129 }
130
131 if ( addresses != null && that.addresses != null )
132 {
133 if ( addresses.size() != that.addresses.size() )
134 {
135 return false;
136 }
137
138 HostAddress[] thisHostAddresses = ( HostAddress[] ) addresses.toArray();
139 HostAddress[] thatHostAddresses = ( HostAddress[] ) that.addresses.toArray();
140
141 for ( int i = 0; i < thisHostAddresses.length; i++ )
142 {
143 if ( !thisHostAddresses[i].equals( thatHostAddresses[i] ) )
144 {
145 return false;
146 }
147 }
148 }
149
150 return true;
151 }
152
153
154
155
156
157
158
159 public HostAddress[] getAddresses()
160 {
161 return ( HostAddress[] ) addresses.toArray();
162 }
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public int computeLength()
183 {
184
185 addressesLength = 0;
186
187 if ( ( addresses != null ) && ( addresses.size() != 0 ) )
188 {
189 for ( HostAddress hostAddress : addresses )
190 {
191 int length = hostAddress.computeLength();
192 addressesLength += length;
193 }
194 }
195
196 return 1 + TLV.getNbBytes( addressesLength ) + addressesLength;
197 }
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215 public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
216 {
217 if ( buffer == null )
218 {
219 throw new EncoderException( "Cannot put a PDU in a null buffer !" );
220 }
221
222 try
223 {
224
225 buffer.put( UniversalTag.SEQUENCE_TAG );
226 buffer.put( TLV.getBytes( addressesLength ) );
227
228
229 if ( ( addresses != null ) && ( addresses.size() != 0 ) )
230 {
231 for ( HostAddress hostAddress : addresses )
232 {
233 hostAddress.encode( buffer );
234 }
235 }
236 }
237 catch ( BufferOverflowException boe )
238 {
239 LOG.error(
240 "Cannot encode the HostAddresses object, the PDU size is {} when only {} bytes has been allocated", 1
241 + TLV.getNbBytes( addressesLength ) + addressesLength, buffer.capacity() );
242 throw new EncoderException( "The PDU buffer size is too small !" );
243 }
244
245 if ( IS_DEBUG )
246 {
247 LOG.debug( "HostAddresses encoding : {}", StringTools.dumpBytes( buffer.array() ) );
248 LOG.debug( "HostAddresses initial value : {}", toString() );
249 }
250
251 return buffer;
252 }
253
254
255
256
257
258 public String toString()
259 {
260 StringBuilder sb = new StringBuilder();
261 boolean isFirst = true;
262
263 for ( HostAddress hostAddress : addresses )
264 {
265 if ( isFirst )
266 {
267 isFirst = false;
268 }
269 else
270 {
271 sb.append( ", " );
272 }
273
274 sb.append( hostAddress.toString() );
275 }
276
277 return sb.toString();
278 }
279 }