Source for gnu.java.security.der.DERValue

   1: /* DERValue.java -- a value read or written to a DER encoding.
   2:    Copyright (C) 2003 Free Software Foundation, Inc.
   3: 
   4: This file is 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, or (at your option)
   9: 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; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 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.java.security.der;
  40: 
  41: import gnu.java.security.x509.Util;
  42: 
  43: import java.io.ByteArrayOutputStream;
  44: import java.io.IOException;
  45: 
  46: public class DERValue implements DER
  47: {
  48: 
  49:   // Fields.
  50:   // ------------------------------------------------------------------------
  51: 
  52:   private final int tagClass;
  53:   private final boolean constructed;
  54:   private final int tag;
  55:   private int length;
  56:   private final Object value;
  57:   private byte[] encoded;
  58: 
  59:   // Constructor.
  60:   // ------------------------------------------------------------------------
  61: 
  62:   public DERValue(int tag, int length, Object value, byte[] encoded)
  63:   {
  64:     tagClass = tag & 0xC0;
  65:     this.tag = tag & 0x1F;
  66:     constructed = (tag & CONSTRUCTED) == CONSTRUCTED;
  67:     this.length = length;
  68:     this.value = value;
  69:     if (encoded != null)
  70:       this.encoded = (byte[]) encoded.clone();
  71:   }
  72: 
  73:   public DERValue(int tag, Object value)
  74:   {
  75:     this(tag, 0, value, null);
  76:   }
  77: 
  78:   // Instance methods.
  79:   // ------------------------------------------------------------------------
  80: 
  81:   public int getExternalTag()
  82:   {
  83:     return tagClass | tag | (constructed ? 0x20 : 0x00);
  84:   }
  85: 
  86:   public int getTag()
  87:   {
  88:     return tag;
  89:   }
  90: 
  91:   public int getTagClass()
  92:   {
  93:     return tagClass;
  94:   }
  95: 
  96:   public boolean isConstructed()
  97:   {
  98:     return constructed;
  99:   }
 100: 
 101:   public int getLength()
 102:   {
 103:     if (encoded == null)
 104:       {
 105:         try
 106:           {
 107:             ByteArrayOutputStream out = new ByteArrayOutputStream();
 108:             length = DERWriter.write(out, this);
 109:             encoded = out.toByteArray();
 110:           }
 111:         catch (IOException ioe)
 112:           {
 113:             IllegalArgumentException iae = new IllegalArgumentException ();
 114:             iae.initCause (ioe);
 115:             throw iae;
 116:           }
 117:       }
 118:     return length;
 119:   }
 120: 
 121:   public Object getValue()
 122:   {
 123:     return value;
 124:   }
 125: 
 126:   public Object getValueAs (final int derType) throws IOException
 127:   {
 128:     byte[] encoded = getEncoded ();
 129:     encoded[0] = (byte) derType;
 130:     return DERReader.read (encoded).getValue ();
 131:   }
 132: 
 133:   public byte[] getEncoded()
 134:   {
 135:     if (encoded == null)
 136:       {
 137:         try
 138:           {
 139:             ByteArrayOutputStream out = new ByteArrayOutputStream();
 140:             length = DERWriter.write(out, this);
 141:             encoded = out.toByteArray();
 142:           }
 143:         catch (IOException ioe)
 144:           {
 145:             IllegalArgumentException iae = new IllegalArgumentException ();
 146:             iae.initCause (ioe);
 147:             throw iae;
 148:           }
 149:       }
 150:     return (byte[]) encoded.clone();
 151:   }
 152: 
 153:   public int getEncodedLength()
 154:   {
 155:     if (encoded == null)
 156:       {
 157:         try
 158:           {
 159:             ByteArrayOutputStream out = new ByteArrayOutputStream();
 160:             length = DERWriter.write(out, this);
 161:             encoded = out.toByteArray();
 162:           }
 163:         catch (IOException ioe)
 164:           {
 165:             IllegalArgumentException iae = new IllegalArgumentException ();
 166:             iae.initCause (ioe);
 167:             throw iae;
 168:           }
 169:       }
 170:     return encoded.length;
 171:   }
 172: 
 173:   public String toString()
 174:   {
 175:     String start = "DERValue ( [";
 176:     if (tagClass == DER.UNIVERSAL)
 177:       start = start + "UNIVERSAL ";
 178:     else if (tagClass == DER.PRIVATE)
 179:       start = start + "PRIVATE ";
 180:     else if (tagClass == DER.APPLICATION)
 181:       start = start + "APPLICATION ";
 182:     start = start + tag + "] constructed=" + constructed + ", value=";
 183:     if (constructed)
 184:      start = start + "\n" + Util.hexDump(getEncoded(), "\t");
 185:     else
 186:      start = start + value;
 187:     return start + " )";
 188:   }
 189: }