home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / VPage / Java.bin / CLASSES.ZIP / sun / security / util / DerValue.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-07-08  |  5.9 KB  |  244 lines

  1. package sun.security.util;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.DataInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9.  
  10. public class DerValue {
  11.    public byte tag;
  12.    protected DerInputBuffer buffer;
  13.    public DerInputStream data;
  14.    private int length;
  15.    public static final byte tag_Integer = 2;
  16.    public static final byte tag_BitString = 3;
  17.    public static final byte tag_OctetString = 4;
  18.    public static final byte tag_Null = 5;
  19.    public static final byte tag_ObjectId = 6;
  20.    public static final byte tag_PrintableString = 19;
  21.    public static final byte tag_IA5String = 22;
  22.    public static final byte tag_UtcTime = 23;
  23.    public static final byte tag_Sequence = 48;
  24.    public static final byte tag_SequenceOf = 48;
  25.    public static final byte tag_Set = 49;
  26.    public static final byte tag_SetOf = 49;
  27.  
  28.    boolean isUniversal() {
  29.       return (this.tag & 192) == 0;
  30.    }
  31.  
  32.    boolean isApplication() {
  33.       return (this.tag & 192) == 64;
  34.    }
  35.  
  36.    public boolean isContextSpecific() {
  37.       return (this.tag & 192) == 128;
  38.    }
  39.  
  40.    boolean isPrivate() {
  41.       return (this.tag & 192) == 192;
  42.    }
  43.  
  44.    public boolean isConstructed() {
  45.       return (this.tag & 32) == 32;
  46.    }
  47.  
  48.    public DerValue(String var1) throws IOException {
  49.       this.tag = 19;
  50.       this.length = var1.length();
  51.       byte[] var3 = new byte[this.length];
  52.  
  53.       for(int var2 = 0; var2 < this.length; ++var2) {
  54.          var3[var2] = (byte)var1.charAt(var2);
  55.       }
  56.  
  57.       this.buffer = new DerInputBuffer(var3);
  58.       this.data = new DerInputStream(this.buffer);
  59.       this.data.mark(Integer.MAX_VALUE);
  60.    }
  61.  
  62.    public DerValue(byte var1, byte[] var2) {
  63.       this.tag = var1;
  64.       this.buffer = new DerInputBuffer(var2);
  65.       this.length = var2.length;
  66.       this.data = new DerInputStream(this.buffer);
  67.       this.data.mark(Integer.MAX_VALUE);
  68.    }
  69.  
  70.    DerValue(DerInputBuffer var1) throws IOException {
  71.       this.tag = (byte)((ByteArrayInputStream)var1).read();
  72.       this.length = DerInputStream.getLength(var1);
  73.       this.buffer = var1.dup();
  74.       this.buffer.truncate(this.length);
  75.       this.data = new DerInputStream(this.buffer);
  76.       ((ByteArrayInputStream)var1).skip((long)this.length);
  77.    }
  78.  
  79.    public DerValue(byte[] var1) throws IOException {
  80.       this.init(true, new ByteArrayInputStream(var1));
  81.    }
  82.  
  83.    public DerValue(byte[] var1, int var2, int var3) throws IOException {
  84.       this.init(true, new ByteArrayInputStream(var1, var2, var3));
  85.    }
  86.  
  87.    public DerValue(InputStream var1) throws IOException {
  88.       this.init(false, var1);
  89.    }
  90.  
  91.    private void init(boolean var1, InputStream var2) throws IOException {
  92.       this.tag = (byte)var2.read();
  93.       this.length = DerInputStream.getLength(var2);
  94.       if (this.length != 0) {
  95.          if (var1 && var2.available() != this.length) {
  96.             throw new IOException("extra DER value data (constructor)");
  97.          } else {
  98.             byte[] var3 = new byte[this.length];
  99.             DataInputStream var4 = new DataInputStream(var2);
  100.             var4.readFully(var3);
  101.             this.buffer = new DerInputBuffer(var3);
  102.             this.data = new DerInputStream(this.buffer);
  103.          }
  104.       }
  105.    }
  106.  
  107.    public void emit(DerOutputStream var1) throws IOException {
  108.       ((ByteArrayOutputStream)var1).write(this.tag);
  109.       var1.putLength(this.length);
  110.       if (this.length > 0) {
  111.          byte[] var2 = new byte[this.length];
  112.          this.buffer.reset();
  113.          if (this.buffer.read(var2) != this.length) {
  114.             throw new IOException("short DER value read (emit)");
  115.          }
  116.  
  117.          ((OutputStream)var1).write(var2);
  118.       }
  119.  
  120.    }
  121.  
  122.    public ObjectIdentifier getOID() throws IOException {
  123.       if (this.tag != 6) {
  124.          throw new IOException("DerValue.getOID, not an OID " + this.tag);
  125.       } else {
  126.          return new ObjectIdentifier(this.buffer);
  127.       }
  128.    }
  129.  
  130.    public byte[] getOctetString() throws IOException {
  131.       if (this.tag != 4) {
  132.          throw new IOException("DerValue.getOctetString, not an Octet String: " + this.tag);
  133.       } else {
  134.          byte[] var1 = new byte[this.length];
  135.          if (this.buffer.read(var1) != this.length) {
  136.             throw new IOException("short read on DerValue buffer");
  137.          } else {
  138.             return var1;
  139.          }
  140.       }
  141.    }
  142.  
  143.    public BigInt getInteger() throws IOException {
  144.       if (this.tag != 2) {
  145.          throw new IOException("DerValue.getInteger, not an int " + this.tag);
  146.       } else {
  147.          return this.buffer.getUnsigned(this.data.available());
  148.       }
  149.    }
  150.  
  151.    public byte[] getBitString() throws IOException {
  152.       if (this.tag != 3) {
  153.          throw new IOException("DerValue.getBitString, not a bit string " + this.tag);
  154.       } else {
  155.          return this.buffer.getBitString();
  156.       }
  157.    }
  158.  
  159.    public String getPrintableString() throws IOException {
  160.       if (this.tag != 19) {
  161.          throw new IOException("DerValue.getPrintableString, not a string " + this.tag);
  162.       } else {
  163.          StringBuffer var1 = new StringBuffer(this.length);
  164.  
  165.          try {
  166.             int var2 = this.length;
  167.             this.data.reset();
  168.  
  169.             while(var2-- > 0) {
  170.                var1.append((char)this.data.getByte());
  171.             }
  172.          } catch (IOException var3) {
  173.             return null;
  174.          }
  175.  
  176.          return new String(var1);
  177.       }
  178.    }
  179.  
  180.    public String getIA5String() throws IOException {
  181.       if (this.tag != 22) {
  182.          throw new IOException("DerValue.getPrintableString, not a string " + this.tag);
  183.       } else {
  184.          StringBuffer var1 = new StringBuffer(this.length);
  185.  
  186.          try {
  187.             int var2 = this.length;
  188.             this.data.reset();
  189.  
  190.             while(var2-- > 0) {
  191.                var1.append((char)this.data.getByte());
  192.             }
  193.          } catch (IOException var3) {
  194.             return null;
  195.          }
  196.  
  197.          return new String(var1);
  198.       }
  199.    }
  200.  
  201.    public boolean equals(Object var1) {
  202.       return var1 instanceof DerValue ? this.equals((DerValue)var1) : false;
  203.    }
  204.  
  205.    public boolean equals(DerValue var1) {
  206.       this.data.reset();
  207.       var1.data.reset();
  208.       if (this == var1) {
  209.          return true;
  210.       } else {
  211.          return this.tag != var1.tag ? false : this.buffer.equals(var1.buffer);
  212.       }
  213.    }
  214.  
  215.    public String toString() {
  216.       try {
  217.          if (this.tag == 19) {
  218.             return "\"" + this.getPrintableString() + "\"";
  219.          } else if (this.tag == 5) {
  220.             return "[DerValue, null]";
  221.          } else {
  222.             return this.tag == 6 ? "OID." + this.getOID() : "[DerValue, tag = " + this.tag + ", length = " + this.length + "]";
  223.          }
  224.       } catch (IOException var1) {
  225.          throw new IllegalArgumentException("misformatted DER value");
  226.       }
  227.    }
  228.  
  229.    public byte[] toByteArray() throws IOException {
  230.       DerOutputStream var1 = new DerOutputStream();
  231.       this.emit(var1);
  232.       this.data.reset();
  233.       return ((ByteArrayOutputStream)var1).toByteArray();
  234.    }
  235.  
  236.    public DerInputStream toDerInputStream() throws IOException {
  237.       if (this.tag != 48 && this.tag != 49) {
  238.          throw new IOException("toDerInputStream rejects tag type " + this.tag);
  239.       } else {
  240.          return new DerInputStream(this.buffer);
  241.       }
  242.    }
  243. }
  244.