1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44:
49: public class ClientHelloBuilder extends ClientHello implements Builder
50: {
51: public ClientHelloBuilder()
52: {
53: super(ByteBuffer.allocate(256));
54: }
55:
56:
59: public ByteBuffer buffer()
60: {
61: return (ByteBuffer) buffer.duplicate().position(0).limit(length());
62: }
63:
64: public void setVersion(final ProtocolVersion version)
65: {
66: ensureCapacity(2);
67: buffer.putShort(0, (short) version.rawValue ());
68: }
69:
70: public void setSessionId (final byte[] buffer)
71: {
72: setSessionId(buffer, 0, buffer.length);
73: }
74:
75: public void setSessionId (final byte[] buffer, final int offset, final int length)
76: {
77: ensureCapacity(SESSID_OFFSET2 + length);
78: int len = Math.min (32, length);
79: this.buffer.put (SESSID_OFFSET, (byte) len);
80: this.buffer.position (SESSID_OFFSET2);
81: this.buffer.put (buffer, offset, len);
82: }
83:
84: public void setCipherSuites(List<CipherSuite> suites)
85: {
86: int off = getCipherSuitesOffset();
87: ensureCapacity(off + (2 * suites.size()) + 2);
88: buffer.putShort(off, (short) (suites.size() * 2));
89: int i = 2;
90: for (CipherSuite suite : suites)
91: {
92: ((ByteBuffer) buffer.duplicate().position(off+i)).put(suite.id());
93: i += 2;
94: }
95: }
96:
97: public void setCompressionMethods(List<CompressionMethod> methods)
98: {
99: int off = getCompressionMethodsOffset();
100: ensureCapacity(off + methods.size() + 1);
101: buffer.put(off, (byte) methods.size());
102: for (CompressionMethod method : methods)
103: buffer.put(++off, (byte) method.getValue());
104: }
105:
106: public void setExtensionsLength (final int length)
107: {
108: if (length < 0 || length > 16384)
109: throw new IllegalArgumentException("length must be nonnegative and not exceed 16384");
110: int needed = getExtensionsOffset() + 2 + length;
111: if (buffer.capacity() < needed)
112: ensureCapacity(needed);
113: buffer.putShort(getExtensionsOffset(), (short) length);
114: }
115:
116: public void setExtensions(ByteBuffer extensions)
117: {
118: int elen = extensions.getShort(0) & 0xFFFF;
119: setExtensionsLength(elen);
120: ((ByteBuffer) buffer.duplicate().position(getExtensionsOffset())).put(extensions);
121: }
122:
123: public void setDisableExtensions(boolean disableExtensions)
124: {
125: this.disableExtensions = disableExtensions;
126: }
127:
128: public void ensureCapacity(final int length)
129: {
130: if (buffer.capacity() >= length)
131: return;
132: ByteBuffer newBuf = ByteBuffer.allocate(length);
133: newBuf.put((ByteBuffer) buffer.position(0));
134: newBuf.position(0);
135: this.buffer = newBuf;
136: }
137: }