1 package org.bouncycastle.asn1; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 6 /** 7 * A DER encoding version of an application specific object. 8 */ 9 public class DLApplicationSpecific 10 extends ASN1ApplicationSpecific 11 { DLApplicationSpecific( boolean isConstructed, int tag, byte[] octets)12 DLApplicationSpecific( 13 boolean isConstructed, 14 int tag, 15 byte[] octets) 16 { 17 super(isConstructed, tag, octets); 18 } 19 20 /** 21 * Create an application specific object from the passed in data. This will assume 22 * the data does not represent a constructed object. 23 * 24 * @param tag the tag number for this object. 25 * @param octets the encoding of the object's body. 26 */ DLApplicationSpecific( int tag, byte[] octets)27 public DLApplicationSpecific( 28 int tag, 29 byte[] octets) 30 { 31 this(false, tag, octets); 32 } 33 34 /** 35 * Create an application specific object with a tagging of explicit/constructed. 36 * 37 * @param tag the tag number for this object. 38 * @param object the object to be contained. 39 */ DLApplicationSpecific( int tag, ASN1Encodable object)40 public DLApplicationSpecific( 41 int tag, 42 ASN1Encodable object) 43 throws IOException 44 { 45 this(true, tag, object); 46 } 47 48 /** 49 * Create an application specific object with the tagging style given by the value of constructed. 50 * 51 * @param constructed true if the object is constructed. 52 * @param tag the tag number for this object. 53 * @param object the object to be contained. 54 */ DLApplicationSpecific( boolean constructed, int tag, ASN1Encodable object)55 public DLApplicationSpecific( 56 boolean constructed, 57 int tag, 58 ASN1Encodable object) 59 throws IOException 60 { 61 super(constructed || object.toASN1Primitive().isConstructed(), tag, getEncoding(constructed, object)); 62 } 63 getEncoding(boolean explicit, ASN1Encodable object)64 private static byte[] getEncoding(boolean explicit, ASN1Encodable object) 65 throws IOException 66 { 67 byte[] data = object.toASN1Primitive().getEncoded(ASN1Encoding.DL); 68 69 if (explicit) 70 { 71 return data; 72 } 73 else 74 { 75 int lenBytes = getLengthOfHeader(data); 76 byte[] tmp = new byte[data.length - lenBytes]; 77 System.arraycopy(data, lenBytes, tmp, 0, tmp.length); 78 return tmp; 79 } 80 } 81 82 /** 83 * Create an application specific object which is marked as constructed 84 * 85 * @param tagNo the tag number for this object. 86 * @param vec the objects making up the application specific object. 87 */ DLApplicationSpecific(int tagNo, ASN1EncodableVector vec)88 public DLApplicationSpecific(int tagNo, ASN1EncodableVector vec) 89 { 90 super(true, tagNo, getEncodedVector(vec)); 91 } 92 getEncodedVector(ASN1EncodableVector vec)93 private static byte[] getEncodedVector(ASN1EncodableVector vec) 94 { 95 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 96 97 for (int i = 0; i != vec.size(); i++) 98 { 99 try 100 { 101 bOut.write(((ASN1Object)vec.get(i)).getEncoded(ASN1Encoding.DL)); 102 } 103 catch (IOException e) 104 { 105 throw new ASN1ParsingException("malformed object: " + e, e); 106 } 107 } 108 return bOut.toByteArray(); 109 } 110 111 /* (non-Javadoc) 112 * @see org.bouncycastle.asn1.ASN1Primitive#encode(org.bouncycastle.asn1.DEROutputStream) 113 */ encode(ASN1OutputStream out, boolean withTag)114 void encode(ASN1OutputStream out, boolean withTag) throws IOException 115 { 116 int flags = BERTags.APPLICATION; 117 if (isConstructed) 118 { 119 flags |= BERTags.CONSTRUCTED; 120 } 121 122 out.writeEncoded(withTag, flags, tag, octets); 123 } 124 } 125