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

   1: /* ClientHelloBuilder.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: import java.util.List;
  43: 
  44: /**
  45:  * Builder for {@link ClientHello} objects.
  46:  *
  47:  * @author Casey Marshall (csm@gnu.org)
  48:  */
  49: public class ClientHelloBuilder extends ClientHello implements Builder
  50: {
  51:   public ClientHelloBuilder()
  52:   {
  53:     super(ByteBuffer.allocate(256));
  54:   }
  55: 
  56:   /* (non-Javadoc)
  57:    * @see gnu.javax.net.ssl.provider.Builder#buffer()
  58:    */
  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: }