1 package org.bouncycastle.asn1; 2 3 import java.io.IOException; 4 5 /** 6 * Base class for ASN.1 primitive objects. These are the actual objects used to generate byte encodings. 7 */ 8 public abstract class ASN1Primitive 9 extends ASN1Object 10 { ASN1Primitive()11 ASN1Primitive() 12 { 13 14 } 15 16 /** 17 * Create a base ASN.1 object from a byte stream. 18 * 19 * @param data the byte stream to parse. 20 * @return the base ASN.1 object represented by the byte stream. 21 * @exception IOException if there is a problem parsing the data, or parsing the stream did not exhaust the available data. 22 */ fromByteArray(byte[] data)23 public static ASN1Primitive fromByteArray(byte[] data) 24 throws IOException 25 { 26 ASN1InputStream aIn = new ASN1InputStream(data); 27 28 try 29 { 30 ASN1Primitive o = aIn.readObject(); 31 32 if (aIn.available() != 0) 33 { 34 throw new IOException("Extra data detected in stream"); 35 } 36 37 return o; 38 } 39 catch (ClassCastException e) 40 { 41 throw new IOException("cannot recognise object in stream"); 42 } 43 } 44 equals(Object o)45 public final boolean equals(Object o) 46 { 47 if (this == o) 48 { 49 return true; 50 } 51 52 return (o instanceof ASN1Encodable) && asn1Equals(((ASN1Encodable)o).toASN1Primitive()); 53 } 54 toASN1Primitive()55 public ASN1Primitive toASN1Primitive() 56 { 57 return this; 58 } 59 60 /** 61 * Return the current object as one which encodes using Distinguished Encoding Rules. 62 * 63 * @return a DER version of this. 64 */ toDERObject()65 ASN1Primitive toDERObject() 66 { 67 return this; 68 } 69 70 /** 71 * Return the current object as one which encodes using Definite Length encoding. 72 * 73 * @return a DL version of this. 74 */ toDLObject()75 ASN1Primitive toDLObject() 76 { 77 return this; 78 } 79 hashCode()80 public abstract int hashCode(); 81 isConstructed()82 abstract boolean isConstructed(); 83 encodedLength()84 abstract int encodedLength() throws IOException; 85 encode(ASN1OutputStream out)86 abstract void encode(ASN1OutputStream out) throws IOException; 87 asn1Equals(ASN1Primitive o)88 abstract boolean asn1Equals(ASN1Primitive o); 89 }