1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48:
49: public final class CipherSuiteList implements Iterable<CipherSuite>
50: {
51: private final ByteBuffer buffer;
52: private final ProtocolVersion version;
53: private int modCount;
54:
55: public CipherSuiteList (final ByteBuffer buffer)
56: {
57: this (buffer, ProtocolVersion.SSL_3);
58: }
59:
60: public CipherSuiteList (final ByteBuffer buffer, final ProtocolVersion version)
61: {
62: this.version = version;
63: this.buffer = buffer;
64: modCount = 0;
65: }
66:
67:
72: public int size ()
73: {
74: return (buffer.getShort (0) & 0xFFFF) >>> 1;
75: }
76:
77:
85: public CipherSuite get (final int index)
86: {
87: int size = size ();
88: if (index < 0 || index >= size)
89: throw new IndexOutOfBoundsException ("limit: " + size
90: + "; requested: " + index);
91: return CipherSuite.forValue(buffer.getShort(2 + (index << 1))).resolve();
92: }
93:
94:
108: public void put (final int index, final CipherSuite suite)
109: {
110: int size = size ();
111: if (index < 0 || index >= size)
112: throw new IndexOutOfBoundsException ("limit: " + size
113: + "; requested: " + index);
114: buffer.position (2 + (index << 1));
115: buffer.put (suite.id ());
116: modCount++;
117: }
118:
119:
133: public void setSize (final int newSize)
134: {
135: if (newSize < 0 || newSize > 32767)
136: throw new IllegalArgumentException ("size must be between 0 and 32767");
137: if ((newSize << 1) + 2 > buffer.capacity ())
138: throw new IllegalArgumentException ("limit: " + buffer.capacity ()
139: + "; requested: " + newSize);
140: buffer.putShort (0, (short) (newSize << 1));
141: modCount++;
142: }
143:
144: public String toString ()
145: {
146: return toString (null);
147: }
148:
149: public String toString (final String prefix)
150: {
151: StringWriter str = new StringWriter ();
152: PrintWriter out = new PrintWriter (str);
153: if (prefix != null)
154: out.print (prefix);
155: out.print ("[");
156: out.print (size ());
157: out.println ("] {");
158: for (Iterator it = new Iterator (); it.hasNext (); )
159: {
160: CipherSuite suite = (CipherSuite) it.next ();
161: if (prefix != null)
162: out.print (prefix);
163: out.print (" ");
164: out.print (suite);
165: if (it.hasNext ())
166: out.print (",");
167: out.println ();
168: }
169: if (prefix != null)
170: out.print (prefix);
171: out.print ("};");
172: return str.toString ();
173: }
174:
175: public boolean equals (Object o)
176: {
177: if (!(o instanceof CipherSuiteList))
178: return false;
179: CipherSuiteList that = (CipherSuiteList) o;
180:
181: if (size () != that.size ())
182: return false;
183:
184: for (Iterator it1 = new Iterator (), it2 = that.new Iterator ();
185: it1.hasNext () && it2.hasNext (); )
186: {
187: if (!it1.next ().equals (it2.next ()))
188: return false;
189: }
190: return true;
191: }
192:
193: public java.util.Iterator<CipherSuite> iterator ()
194: {
195: return new Iterator ();
196: }
197:
198:
205: public class Iterator implements ListIterator<CipherSuite>
206: {
207: private final int modCount;
208: private int index;
209:
210: Iterator ()
211: {
212: this.modCount = CipherSuiteList.this.modCount;
213: index = 0;
214: }
215:
216: public void add (CipherSuite cs)
217: {
218: throw new UnsupportedOperationException ();
219: }
220:
221: public boolean hasNext ()
222: {
223: return (index < size ());
224: }
225:
226: public boolean hasPrevious ()
227: {
228: return (index > 0);
229: }
230:
231: public CipherSuite next () throws NoSuchElementException
232: {
233: if (modCount != CipherSuiteList.this.modCount)
234: throw new ConcurrentModificationException ();
235: try
236: {
237: return get (index++);
238: }
239: catch (IndexOutOfBoundsException ioobe)
240: {
241: throw new NoSuchElementException ();
242: }
243: }
244:
245: public int nextIndex ()
246: {
247: if (hasNext ())
248: return (index + 1);
249: return -1;
250: }
251:
252: public CipherSuite previous () throws NoSuchElementException
253: {
254: if (index == 0)
255: throw new NoSuchElementException ();
256: if (modCount != CipherSuiteList.this.modCount)
257: throw new ConcurrentModificationException ();
258: try
259: {
260: return get (--index);
261: }
262: catch (IndexOutOfBoundsException ioobe)
263: {
264: throw new NoSuchElementException ();
265: }
266: }
267:
268: public int previousIndex ()
269: {
270: return (index - 1);
271: }
272:
273: public void remove ()
274: {
275: throw new UnsupportedOperationException ();
276: }
277:
278: public void set (final CipherSuite cs)
279: {
280: put (index, cs);
281: }
282: }
283: }