diff options
author | David Hook <dgh@cryptoworkshop.com> | 2013-11-26 11:53:40 +0400 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2013-11-26 11:53:40 +0400 |
commit | 43882cb7727d0b2344ddfaf4a3d7633d299e8b58 (patch) | |
tree | 8e5c744079e56d310c008eac5b7a8a76906ee8e2 /core/src/main/j2me/org/bouncycastle/crypto/tls/ServerNameList.java | |
parent | 797fe912ccb30e60cfe5651be23706e5738b01d7 (diff) |
core updates
Diffstat (limited to 'core/src/main/j2me/org/bouncycastle/crypto/tls/ServerNameList.java')
-rw-r--r-- | core/src/main/j2me/org/bouncycastle/crypto/tls/ServerNameList.java | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/core/src/main/j2me/org/bouncycastle/crypto/tls/ServerNameList.java b/core/src/main/j2me/org/bouncycastle/crypto/tls/ServerNameList.java new file mode 100644 index 00000000..c44bb896 --- /dev/null +++ b/core/src/main/j2me/org/bouncycastle/crypto/tls/ServerNameList.java @@ -0,0 +1,86 @@ +package org.bouncycastle.crypto.tls; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.Vector; + +public class ServerNameList +{ + protected Vector serverNameList; + + /** + * @param serverNameList a {@link Vector} of {@link ServerName}. + */ + public ServerNameList(Vector serverNameList) + { + if (serverNameList == null || serverNameList.isEmpty()) + { + throw new IllegalArgumentException("'serverNameList' must not be null or empty"); + } + + this.serverNameList = serverNameList; + } + + /** + * @return a {@link Vector} of {@link ServerName}. + */ + public Vector getServerNameList() + { + return serverNameList; + } + + /** + * Encode this {@link ServerNameList} to an {@link OutputStream}. + * + * @param output + * the {@link OutputStream} to encode to. + * @throws IOException + */ + public void encode(OutputStream output) throws IOException + { + ByteArrayOutputStream buf = new ByteArrayOutputStream(); + + for (int i = 0; i < serverNameList.size(); ++i) + { + ServerName entry = (ServerName)serverNameList.elementAt(i); + entry.encode(buf); + } + + TlsUtils.checkUint16(buf.size()); + TlsUtils.writeUint16(buf.size(), output); + output.write(buf.toByteArray()); + } + + /** + * Parse a {@link ServerNameList} from an {@link InputStream}. + * + * @param input + * the {@link InputStream} to parse from. + * @return a {@link ServerNameList} object. + * @throws IOException + */ + public static ServerNameList parse(InputStream input) throws IOException + { + int length = TlsUtils.readUint16(input); + if (length < 1) + { + throw new TlsFatalAlert(AlertDescription.decode_error); + } + + byte[] data = TlsUtils.readFully(length, input); + + ByteArrayInputStream buf = new ByteArrayInputStream(data); + + Vector server_name_list = new Vector(); + while (buf.available() > 0) + { + ServerName entry = ServerName.parse(buf); + server_name_list.addElement(entry); + } + + return new ServerNameList(server_name_list); + } +} |