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

   1: /* ClientCertificateTypeList.java -- A list of certificate types.
   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 gnu.javax.net.ssl.provider.CertificateRequest.ClientCertificateType;
  42: 
  43: import java.io.PrintWriter;
  44: import java.io.StringWriter;
  45: 
  46: import java.nio.ByteBuffer;
  47: 
  48: import java.util.ConcurrentModificationException;
  49: import java.util.ListIterator;
  50: import java.util.NoSuchElementException;
  51: 
  52: public class ClientCertificateTypeList implements Iterable<ClientCertificateType>
  53: {
  54:   private final ByteBuffer buffer;
  55:   private int modCount;
  56: 
  57:   public ClientCertificateTypeList (final ByteBuffer buffer)
  58:   {
  59:     this.buffer = buffer;
  60:     modCount = 0;
  61:   }
  62: 
  63:   public int size ()
  64:   {
  65:     return (buffer.get (0) & 0xFF);
  66:   }
  67: 
  68:   public CertificateRequest.ClientCertificateType get (final int index)
  69:   {
  70:     int size = size ();
  71:     if (index < 0 || index >= size)
  72:       throw new IndexOutOfBoundsException ("limit: " + size
  73:                                            + "; requested: " + index);
  74:     return CertificateRequest.ClientCertificateType.forValue
  75:       (buffer.get (index + 1) & 0xFF);
  76:   }
  77: 
  78:   public java.util.Iterator<ClientCertificateType> iterator()
  79:   {
  80:     return new Iterator();
  81:   }
  82: 
  83:   public void put (final int index, final CertificateRequest.ClientCertificateType type)
  84:   {
  85:     int size = size ();
  86:     if (index < 0 || index >= size)
  87:       throw new IndexOutOfBoundsException ("limit: " + size
  88:                                            + "; requested: " + index);
  89:     buffer.put (index + 1, (byte) type.getValue ());
  90:     modCount++;
  91:   }
  92: 
  93:   public void setSize (final int newSize)
  94:   {
  95:     if (newSize < 0 || newSize > 255)
  96:       throw new IllegalArgumentException ("size must be between 0 and 255");
  97:     if (newSize + 1 > buffer.capacity ())
  98:       throw new IllegalArgumentException ("limit: " + (buffer.capacity () - 1)
  99:                                           + "; requested: " + newSize);
 100:     buffer.put (0, (byte) newSize);
 101:     modCount++;
 102:   }
 103: 
 104:   public String toString ()
 105:   {
 106:     return toString (null);
 107:   }
 108: 
 109:   public String toString (final String prefix)
 110:   {
 111:     StringWriter str = new StringWriter ();
 112:     PrintWriter out = new PrintWriter (str);
 113:     if (prefix != null) out.print (prefix);
 114:     out.print ("[");
 115:     out.print (size ());
 116:     out.println ("] {");
 117:     for (Iterator it = new Iterator (); it.hasNext (); )
 118:       {
 119:         if (prefix != null) out.print (prefix);
 120:         out.print ("  ");
 121:         out.print (it.next ());
 122:         if (it.hasNext ())
 123:           out.print (",");
 124:         out.println ();
 125:       }
 126:     if (prefix != null) out.print (prefix);
 127:     out.println ("};");
 128:     return str.toString ();
 129:   }
 130: 
 131:   public boolean equals (Object o)
 132:   {
 133:     if (!(o instanceof ClientCertificateTypeList))
 134:       return false;
 135:     ClientCertificateTypeList that = (ClientCertificateTypeList) o;
 136: 
 137:     if (size () != that.size ())
 138:       return false;
 139: 
 140:     for (Iterator it1 = new Iterator (), it2 = that.new Iterator ();
 141:          it1.hasNext () && it2.hasNext (); )
 142:       {
 143:         if (!it1.next ().equals (it2.next ()))
 144:           return false;
 145:       }
 146:     return true;
 147:   }
 148: 
 149:   public class Iterator implements ListIterator<CertificateRequest.ClientCertificateType>
 150:   {
 151:     private int index;
 152:     private final int modCount;
 153: 
 154:     Iterator ()
 155:     {
 156:       index = 0;
 157:       modCount = ClientCertificateTypeList.this.modCount;
 158:     }
 159: 
 160:     public void add (CertificateRequest.ClientCertificateType type)
 161:     {
 162:       throw new UnsupportedOperationException ();
 163:     }
 164: 
 165:     public boolean hasNext ()
 166:     {
 167:       return (index < size ());
 168:     }
 169: 
 170:     public boolean hasPrevious ()
 171:     {
 172:       return (index > 0);
 173:     }
 174: 
 175:     public CertificateRequest.ClientCertificateType next () throws NoSuchElementException
 176:     {
 177:       if (modCount != ClientCertificateTypeList.this.modCount)
 178:         throw new ConcurrentModificationException ();
 179:       try
 180:         {
 181:           return get (index++);
 182:         }
 183:       catch (IndexOutOfBoundsException ioobe)
 184:         {
 185:           throw new NoSuchElementException ();
 186:         }
 187:     }
 188: 
 189:     public int nextIndex ()
 190:     {
 191:       if (hasNext ())
 192:         return (index + 1);
 193:       return -1;
 194:     }
 195: 
 196:     public CertificateRequest.ClientCertificateType previous () throws NoSuchElementException
 197:     {
 198:       if (index == 0)
 199:         throw new NoSuchElementException ();
 200:       if (modCount != ClientCertificateTypeList.this.modCount)
 201:         throw new ConcurrentModificationException ();
 202:       try
 203:         {
 204:           return get (--index);
 205:         }
 206:       catch (IndexOutOfBoundsException ioobe)
 207:         {
 208:           throw new NoSuchElementException ();
 209:         }
 210:     }
 211: 
 212:     public int previousIndex ()
 213:     {
 214:       return (index - 1);
 215:     }
 216: 
 217:     public void remove ()
 218:     {
 219:       throw new UnsupportedOperationException ();
 220:     }
 221: 
 222:     public void set (final CertificateRequest.ClientCertificateType type)
 223:     {
 224:       put (index, type);
 225:     }
 226:   }
 227: }