1:
37:
38:
39: package ;
40:
41: import ;
42:
43:
47: public class ServerHelloBuilder extends ServerHello implements Builder
48: {
49: public ServerHelloBuilder()
50: {
51:
52:
53: super(ByteBuffer.allocate(SESSID_OFFSET2 + 35));
54: }
55:
56: public ByteBuffer buffer()
57: {
58: return ((ByteBuffer) buffer.duplicate().position(0).limit(length())).slice();
59: }
60:
61:
62:
63:
64:
65: public void setVersion (final ProtocolVersion version)
66: {
67: buffer.putShort (0, (short) version.rawValue ());
68: }
69:
70: public void setSessionId (final byte[] sessionId)
71: {
72: setSessionId (sessionId, 0, sessionId.length);
73: }
74:
75: public void setSessionId (final byte[] sessionId, final int offset,
76: final int length)
77: {
78: if (length < 0 || length > 32)
79: throw new IllegalArgumentException("length must be between 0 and 32");
80: buffer.put(SESSID_OFFSET, (byte) length);
81: ((ByteBuffer) buffer.duplicate().position(SESSID_OFFSET2))
82: .put(sessionId, offset, length);
83: }
84:
85: public void setCipherSuite (final CipherSuite suite)
86: {
87: int offset = SESSID_OFFSET + (buffer.get(SESSID_OFFSET) & 0xFF) + 1;
88: ((ByteBuffer) buffer.duplicate().position(offset)).put(suite.id());
89: }
90:
91: public void setCompressionMethod (final CompressionMethod comp)
92: {
93: int offset = SESSID_OFFSET + (buffer.get(SESSID_OFFSET) & 0xFF) + 3;
94: buffer.put (offset, (byte) comp.getValue ());
95: }
96:
97:
98:
99: public void setDisableExtensions(boolean disable)
100: {
101: disableExtensions = disable;
102: }
103:
104: public void setExtensionsLength (final int length)
105: {
106: if (length < 0 || length > 16384)
107: throw new IllegalArgumentException("length must be nonnegative and not exceed 16384");
108: int needed = SESSID_OFFSET2 + (buffer.get(SESSID_OFFSET) & 0xFF) + 5 + length;
109: if (buffer.capacity() < needed)
110: ensureCapacity(needed);
111: buffer.putShort (SESSID_OFFSET2 + (buffer.get (SESSID_OFFSET) & 0xFF) + 3,
112: (short) length);
113: }
114:
115: public void setExtensions(ByteBuffer extensions)
116: {
117: extensions = (ByteBuffer)
118: extensions.duplicate().limit(extensions.position() + extensionsLength());
119: ((ByteBuffer) buffer.duplicate().position(SESSID_OFFSET2
120: + (buffer.get(SESSID_OFFSET) & 0xFF)
121: )).put(extensions);
122: }
123:
124: public void ensureCapacity(int newCapacity)
125: {
126: ByteBuffer newBuffer = ByteBuffer.allocate(newCapacity);
127: newBuffer.put(buffer);
128: newBuffer.position(0);
129: buffer = newBuffer;
130: }
131: }