@@ -0,0 +1,2711 @@
+package org.bouncycastle.crypto.tls;%0D%0A%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.InputStream;%0D%0Aimport java.io.OutputStream;%0D%0A%0D%0Aimport org.bouncycastle.util.Strings;%0D%0A%0D%0Apublic class ServerName%0D%0A%7B%0D%0A    protected short nameType;%0D%0A    protected Object name;%0D%0A%0D%0A    public ServerName(short nameType, Object name)%0D%0A    %7B%0D%0A        if (!isCorrectType(nameType, name))%0D%0A        %7B%0D%0A            throw new IllegalArgumentException(%22'name' is not an instance of the correct type%22);%0D%0A        %7D%0D%0A%0D%0A        this.nameType = nameType;%0D%0A        this.name = name;%0D%0A    %7D%0D%0A%0D%0A    public short getNameType()%0D%0A    %7B%0D%0A        return nameType;%0D%0A    %7D%0D%0A%0D%0A    public Object getName()%0D%0A    %7B%0D%0A        return name;%0D%0A    %7D%0D%0A%0D%0A    public String getHostName()%0D%0A    %7B%0D%0A        if (!isCorrectType(NameType.host_name, name))%0D%0A        %7B%0D%0A            throw new IllegalStateException(%22'name' is not a HostName string%22);%0D%0A        %7D%0D%0A        return (String)name;%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Encode this %7B@link ServerName%7D to an %7B@link OutputStream%7D.%0D%0A     * %0D%0A     * @param output%0D%0A     *            the %7B@link OutputStream%7D to encode to.%0D%0A     * @throws IOException%0D%0A     */%0D%0A    public void encode(OutputStream output) throws IOException%0D%0A    %7B%0D%0A        TlsUtils.writeUint8(nameType, output);%0D%0A%0D%0A        switch (nameType)%0D%0A        %7B%0D%0A        case NameType.host_name:%0D%0A            byte%5B%5D utf8Encoding = Strings.toUTF8ByteArray((String)name);%0D%0A            TlsUtils.writeOpaque16(utf8Encoding, output);%0D%0A            break;%0D%0A        default:%0D%0A            throw new TlsFatalAlert(AlertDescription.internal_error);%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A    /**%0D%0A     * Parse a %7B@link ServerName%7D from an %7B@link InputStream%7D.%0D%0A     * %0D%0A     * @param input%0D%0A     *            the %7B@link InputStream%7D to parse from.%0D%0A     * @return a %7B@link ServerName%7D object.%0D%0A     * @throws IOException%0D%0A     */%0D%0A    public static ServerName parse(InputStream input) throws IOException%0D%0A    %7B%0D%0A        short name_type = TlsUtils.readUint8(input);%0D%0A        Object name;%0D%0A%0D%0A        switch (name_type)%0D%0A        %7B%0D%0A        case NameType.host_name:%0D%0A        %7B%0D%0A            byte%5B%5D utf8Encoding = TlsUtils.readOpaque16(input);%0D%0A            name = Strings.fromUTF8ByteArray(utf8Encoding);%0D%0A            break;%0D%0A        %7D%0D%0A        default:%0D%0A            throw new TlsFatalAlert(AlertDescription.decode_error);%0D%0A        %7D%0D%0A%0D%0A        return new ServerName(name_type, name);%0D%0A    %7D%0D%0A%0D%0A    protected static boolean isCorrectType(short nameType, Object name)%0D%0A    %7B%0D%0A        switch (nameType)%0D%0A        %7B%0D%0A        case NameType.host_name:%0D%0A            return name instanceof String;%0D%0A        default:%0D%0A            throw new IllegalArgumentException(%22'name' is an unsupported value%22);%0D%0A        %7D%0D%0A    %7D%0D%0A%7D%0D%0A

Add ServerName struct from RFC 3546