1 package org.bouncycastle.asn1; 2 3 import java.io.IOException; 4 import java.io.OutputStream; 5 6 /** 7 * Base class for ASN.1 primitive objects. These are the actual objects used to generate byte encodings. 8 */ 9 public abstract class ASN1Primitive 10 extends ASN1Object 11 { ASN1Primitive()12 ASN1Primitive() 13 { 14 } 15 encodeTo(OutputStream output)16 public void encodeTo(OutputStream output) throws IOException 17 { 18 ASN1OutputStream.create(output).writeObject(this); 19 } 20 encodeTo(OutputStream output, String encoding)21 public void encodeTo(OutputStream output, String encoding) throws IOException 22 { 23 ASN1OutputStream.create(output, encoding).writeObject(this); 24 } 25 26 /** 27 * Create a base ASN.1 object from a byte stream. 28 * 29 * @param data the byte stream to parse. 30 * @return the base ASN.1 object represented by the byte stream. 31 * @exception IOException if there is a problem parsing the data, or parsing the stream did not exhaust the available data. 32 */ fromByteArray(byte[] data)33 public static ASN1Primitive fromByteArray(byte[] data) 34 throws IOException 35 { 36 ASN1InputStream aIn = new ASN1InputStream(data); 37 38 try 39 { 40 ASN1Primitive o = aIn.readObject(); 41 42 if (aIn.available() != 0) 43 { 44 throw new IOException("Extra data detected in stream"); 45 } 46 47 return o; 48 } 49 catch (ClassCastException e) 50 { 51 throw new IOException("cannot recognise object in stream"); 52 } 53 } 54 equals(Object o)55 public final boolean equals(Object o) 56 { 57 if (this == o) 58 { 59 return true; 60 } 61 62 return (o instanceof ASN1Encodable) && asn1Equals(((ASN1Encodable)o).toASN1Primitive()); 63 } 64 equals(ASN1Encodable other)65 public final boolean equals(ASN1Encodable other) 66 { 67 return this == other || (null != other && asn1Equals(other.toASN1Primitive())); 68 } 69 equals(ASN1Primitive other)70 public final boolean equals(ASN1Primitive other) 71 { 72 return this == other || asn1Equals(other); 73 } 74 toASN1Primitive()75 public final ASN1Primitive toASN1Primitive() 76 { 77 return this; 78 } 79 80 /** 81 * Return the current object as one which encodes using Distinguished Encoding Rules. 82 * 83 * @return a DER version of this. 84 */ toDERObject()85 ASN1Primitive toDERObject() 86 { 87 return this; 88 } 89 90 /** 91 * Return the current object as one which encodes using Definite Length encoding. 92 * 93 * @return a DL version of this. 94 */ toDLObject()95 ASN1Primitive toDLObject() 96 { 97 return this; 98 } 99 hashCode()100 public abstract int hashCode(); 101 102 /** 103 * Return true if this objected is a CONSTRUCTED one, false otherwise. 104 * @return true if CONSTRUCTED bit set on object's tag, false otherwise. 105 */ isConstructed()106 abstract boolean isConstructed(); 107 108 /** 109 * Return the length of the encoding this object will produce. 110 * @return the length of the object's encoding. 111 * @throws IOException if the encoding length cannot be calculated. 112 */ encodedLength()113 abstract int encodedLength() throws IOException; 114 encode(ASN1OutputStream out, boolean withTag)115 abstract void encode(ASN1OutputStream out, boolean withTag) throws IOException; 116 117 /** 118 * Equality (similarity) comparison for two ASN1Primitive objects. 119 */ asn1Equals(ASN1Primitive o)120 abstract boolean asn1Equals(ASN1Primitive o); 121 }