1 /* ServerNameList.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is a part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or (at
9 your option) any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu.javax.net.ssl.provider;
41 import gnu.javax.net.ssl.provider.Extension.Value;
43 import java.io.PrintWriter;
44 import java.io.StringWriter;
46 import java.nio.ByteBuffer;
47 import java.nio.ByteOrder;
48 import java.nio.CharBuffer;
49 import java.nio.charset.CharacterCodingException;
50 import java.nio.charset.Charset;
51 import java.nio.charset.CharsetEncoder;
52 import java.util.List;
53 import java.util.NoSuchElementException;
56 * The ServerName extension.
62 case host_name: HostName;
70 opaque HostName<1..2^16-1>;
73 ServerName server_name_list<1..2^16-1>
74 } ServerNameList;</pre>
76 * <p><b>Implementation note: this class does not currently contain a
77 * <code>set</code> method. If you are modifying this list, then use the
78 * {@link #get(int)} method, and modify the returned {@link ServerName}.
82 public class ServerNameList extends Value implements Iterable<ServerNameList.ServerName>
84 private ByteBuffer buffer;
86 public ServerNameList (final ByteBuffer buffer)
88 this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
91 public ServerNameList(List<ServerName> names)
94 for (ServerName name : names)
95 length += name.length();
96 buffer = ByteBuffer.allocate(length);
97 buffer.putShort((short) (length - 2));
98 for (ServerName name : names)
99 buffer.put(name.buffer());
105 return (buffer.getShort(0) & 0xFFFF) + 2;
108 public ByteBuffer buffer()
110 return (ByteBuffer) buffer.duplicate().limit(length());
116 final int len = length();
117 for (int i = 2; i < len; )
119 int l = buffer.getShort(i+1);
126 public ServerName get (int index)
128 final int len = length();
130 throw new IndexOutOfBoundsException("0; " + index);
133 int l = buffer.getShort(3);
134 for (i = 2; i < len && n < index; )
136 l = buffer.getShort(i+1);
141 throw new IndexOutOfBoundsException(n + "; " + index);
142 ByteBuffer buf = ((ByteBuffer) buffer.duplicate().position(i).limit(i+l+3)).slice();
143 return new ServerName (buf);
146 public void setLength(final int newLength)
148 if (newLength < 0 || newLength > 65535)
149 throw new IllegalArgumentException("length must be between 0 and 65535");
150 buffer.putShort(0, (short) newLength);
153 public String toString()
155 return toString(null);
158 public String toString(String prefix)
160 StringWriter str = new StringWriter();
161 PrintWriter out = new PrintWriter(str);
162 if (prefix != null) out.print(prefix);
163 out.println ("ServerNameList {");
164 String subprefix = " ";
166 subprefix = prefix + subprefix;
167 for (ServerName name : this)
169 out.println (name.toString(subprefix));
171 if (prefix != null) out.print(prefix);
173 return str.toString();
176 public java.util.Iterator<ServerName> iterator()
178 return new Iterator();
181 public class Iterator implements java.util.Iterator<ServerName>
190 public boolean hasNext()
192 return index < size();
195 public ServerName next() throws NoSuchElementException
199 return get (index++);
201 catch (IndexOutOfBoundsException ioobe)
203 throw new NoSuchElementException();
209 throw new UnsupportedOperationException();
213 public static class ServerName implements Constructed
215 private ByteBuffer buffer;
217 public ServerName(final ByteBuffer buffer)
219 this.buffer = buffer.duplicate().order(ByteOrder.BIG_ENDIAN);
222 public ServerName(NameType type, String name)
224 CharsetEncoder utf8 = Charset.forName("UTF-8").newEncoder();
225 ByteBuffer nameBuf = null;
228 nameBuf = utf8.encode(CharBuffer.wrap(name));
230 catch (CharacterCodingException cce)
232 // We don't expect this to happen; it's UTF-8.
233 throw new IllegalArgumentException(cce);
235 int length = 3 + nameBuf.remaining();
236 buffer = ByteBuffer.allocate(length);
237 buffer.put((byte) type.getValue());
238 buffer.putShort((short) (length - 3));
245 return (buffer.getShort(1) & 0xFFFF) + 3;
248 public ByteBuffer buffer()
250 return (ByteBuffer) buffer.duplicate().limit(length());
253 public NameType type()
255 int v = (buffer.get(0) & 0xFF);
258 return NameType.HOST_NAME;
260 throw new IllegalArgumentException ("illegal name type: " + v);
266 Charset cs = Charset.forName ("UTF-8");
267 return cs.decode(((ByteBuffer) buffer.duplicate().position(3).limit(len))).toString();
270 public String toString()
272 return toString (null);
275 public String toString(String prefix)
277 StringWriter str = new StringWriter();
278 PrintWriter out = new PrintWriter(str);
279 if (prefix != null) out.print (prefix);
280 out.println ("struct {");
281 if (prefix != null) out.print (prefix);
282 out.print (" name_type = ");
285 if (prefix != null) out.print (prefix);
286 out.print (" server_name = ");
289 if (prefix != null) out.print (prefix);
290 out.print ("} ServerName;");
291 return str.toString();
295 public static enum NameType
299 private final int value;
301 private NameType (int value)
306 public int getValue()