1 package com.android.hotspot2.asn1;
2 
3 import java.nio.ByteBuffer;
4 import java.util.Collection;
5 
6 public abstract class Asn1Object {
7     private final int mTag;
8     private final Asn1Class mClass;
9     private final boolean mConstructed;
10     private final int mLength;
11     private final ByteBuffer mPayload;
12 
Asn1Object(int tag, Asn1Class asn1Class, boolean constructed, int length)13     protected Asn1Object(int tag, Asn1Class asn1Class, boolean constructed, int length) {
14         this(tag, asn1Class, constructed, length, null);
15     }
16 
Asn1Object(int tag, Asn1Class asn1Class, boolean constructed, int length, ByteBuffer payload)17     protected Asn1Object(int tag, Asn1Class asn1Class, boolean constructed,
18                          int length, ByteBuffer payload) {
19         mTag = tag;
20         mClass = asn1Class;
21         mConstructed = constructed;
22         mLength = length;
23         mPayload = payload != null ? payload.duplicate() : null;
24     }
25 
getTag()26     public int getTag() {
27         return mTag;
28     }
29 
getAsn1Class()30     public Asn1Class getAsn1Class() {
31         return mClass;
32     }
33 
isConstructed()34     public boolean isConstructed() {
35         return mConstructed;
36     }
37 
isIndefiniteLength()38     public boolean isIndefiniteLength() {
39         return mLength == Asn1Decoder.IndefiniteLength;
40     }
41 
getLength()42     public int getLength() {
43         return mLength;
44     }
45 
getPayload()46     public ByteBuffer getPayload() {
47         return mPayload != null ? mPayload.duplicate() : null;
48     }
49 
getPayload(int position)50     protected ByteBuffer getPayload(int position) {
51         if (mPayload == null) {
52             return null;
53         }
54         ByteBuffer encoding = mPayload.duplicate();
55         encoding.position(position);
56         return encoding;
57     }
58 
setEndOfData(int position)59     protected void setEndOfData(int position) {
60         mPayload.limit(position);
61     }
62 
getEndOfData()63     protected int getEndOfData() {
64         return mPayload.limit();
65     }
66 
matches(Asn1ID id)67     public boolean matches(Asn1ID id) {
68         return mTag == id.getTag() && mClass == id.getAsn1Class();
69     }
70 
toSimpleString()71     public String toSimpleString() {
72         Asn1Tag tag = mClass == Asn1Class.Universal ? Asn1Decoder.mapTag(mTag) : null;
73         if (tag != null) {
74             return tag.name();
75         } else if (mClass == Asn1Class.Universal) {
76             return String.format("[%d]", mTag);
77         } else {
78             return String.format("[%s %d]", mClass, mTag);
79         }
80     }
81 
getChildren()82     public abstract Collection<Asn1Object> getChildren();
83 
84     @Override
toString()85     public String toString() {
86         return toSimpleString();
87     }
88 }
89