1package org.bouncycastle.asn1; 2 3import java.io.IOException; 4 5/** 6 * Parser for indefinite-length tagged objects. 7 */ 8public class BERTaggedObjectParser 9 implements ASN1TaggedObjectParser 10{ 11 private boolean _constructed; 12 private int _tagNumber; 13 private ASN1StreamParser _parser; 14 15 BERTaggedObjectParser( 16 boolean constructed, 17 int tagNumber, 18 ASN1StreamParser parser) 19 { 20 _constructed = constructed; 21 _tagNumber = tagNumber; 22 _parser = parser; 23 } 24 25 /** 26 * Return true if this tagged object is marked as constructed. 27 * 28 * @return true if constructed, false otherwise. 29 */ 30 public boolean isConstructed() 31 { 32 return _constructed; 33 } 34 35 /** 36 * Return the tag number associated with this object. 37 * 38 * @return the tag number. 39 */ 40 public int getTagNo() 41 { 42 return _tagNumber; 43 } 44 45 /** 46 * Return an object parser for the contents of this tagged object. 47 * 48 * @param tag the actual tag number of the object (needed if implicit). 49 * @param isExplicit true if the contained object was explicitly tagged, false if implicit. 50 * @return an ASN.1 encodable object parser. 51 * @throws IOException if there is an issue building the object parser from the stream. 52 */ 53 public ASN1Encodable getObjectParser( 54 int tag, 55 boolean isExplicit) 56 throws IOException 57 { 58 if (isExplicit) 59 { 60 if (!_constructed) 61 { 62 throw new IOException("Explicit tags must be constructed (see X.690 8.14.2)"); 63 } 64 return _parser.readObject(); 65 } 66 67 return _parser.readImplicit(_constructed, tag); 68 } 69 70 /** 71 * Return an in-memory, encodable, representation of the tagged object. 72 * 73 * @return an ASN1TaggedObject. 74 * @throws IOException if there is an issue loading the data. 75 */ 76 public ASN1Primitive getLoadedObject() 77 throws IOException 78 { 79 return _parser.readTaggedObject(_constructed, _tagNumber); 80 } 81 82 /** 83 * Return an ASN1TaggedObject representing this parser and its contents. 84 * 85 * @return an ASN1TaggedObject 86 */ 87 public ASN1Primitive toASN1Primitive() 88 { 89 try 90 { 91 return this.getLoadedObject(); 92 } 93 catch (IOException e) 94 { 95 throw new ASN1ParsingException(e.getMessage()); 96 } 97 } 98}