Source for gnu.javax.net.ssl.provider.ServerHelloBuilder

   1: /* ServerHelloBuilder.java --
   2:    Copyright (C) 2006  Free Software Foundation, Inc.
   3: 
   4: This file is a part of GNU Classpath.
   5: 
   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.
  10: 
  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.
  15: 
  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
  19: USA
  20: 
  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
  24: combination.
  25: 
  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. */
  37: 
  38: 
  39: package gnu.javax.net.ssl.provider;
  40: 
  41: import java.nio.ByteBuffer;
  42: 
  43: /**
  44:  * @author csm
  45:  *
  46:  */
  47: public class ServerHelloBuilder extends ServerHello implements Builder
  48: {
  49:   public ServerHelloBuilder()
  50:   {
  51:     // Allocate a large enough buffer to hold a hello with the maximum
  52:     // size session ID, and no extensions.
  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:   // We don't reallocate the buffer in any of the following methods,
  62:   // because we always allocate a large enough buffer for the base
  63:   // object in the constructor.
  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:   // For extensions, we do reallocate the buffer.
  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: }