blob: 033928c5b16ee514e901f8a1bdcf3d8973490ec2 [file] [log] [blame]
Stuart McCullochbb014372012-06-07 21:57:32 +00001package aQute.libg.asn1;
2
3import java.util.*;
4
5public class PDU implements Types, Iterable<PDU> {
6 final int identifier;
7 final Object payload;
8 byte data[] = new byte[100];
9
10
11 public PDU(int id, Object payload) {
12 identifier = id;
13 this.payload = payload;
14 }
15
16 public PDU(Date payload) {
17 identifier = UTCTIME;
18 this.payload = payload;
19 }
20
21 public PDU(int n) {
22 this(UNIVERSAL+INTEGER, n);
23 }
24
25 public PDU(boolean value) {
26 this(UNIVERSAL+BOOLEAN, value);
27 }
28
29 public PDU(String s) throws Exception {
30 this(UNIVERSAL+IA5STRING, s);
31 }
32
33 public PDU(byte[] data) {
34 this(UNIVERSAL+OCTET_STRING, data);
35 }
36
37 public PDU(BitSet bits) {
38 this(UNIVERSAL+BIT_STRING, bits);
39 }
40
41 public PDU(int top, int l1, int... remainder) {
42 identifier = UNIVERSAL+OBJECT_IDENTIFIER;
43 int[] ids = new int[remainder.length + 2];
44 ids[0] = top;
45 ids[1] = l1;
46 System.arraycopy(remainder, 0, ids, 2, remainder.length);
47 payload = ids;
48 }
49
50 public PDU(int tag, PDU... set) {
51 this(tag,(Object)set);
52 }
53
54 public PDU(PDU... set) {
55 this(SEQUENCE+CONSTRUCTED,set);
56 }
57
58
59 public int getTag() {
60 return identifier & TAGMASK;
61 }
62
63 int getClss() {
64 return identifier & CLASSMASK;
65 }
66
67 public boolean isConstructed() {
68 return (identifier & CONSTRUCTED) != 0;
69 }
70
71 public String getString() {
72 return (String) payload;
73 }
74
75 public Iterator<PDU> iterator() {
76 return Arrays.asList((PDU[]) payload).iterator();
77 }
78
79
80 public int[] getOID() {
81 assert getTag() == OBJECT_IDENTIFIER;
82 return (int[]) payload;
83 }
84
85 public Boolean getBoolean() {
86 assert getTag() == BOOLEAN;
87 return (Boolean) payload;
88 }
89
90 public BitSet getBits() {
91 assert getTag() == BIT_STRING;
92 return (BitSet) payload;
93 }
94
95 public int getInt() {
96 assert getTag() == INTEGER || getTag() == ENUMERATED;
97 return (Integer) payload;
98 }
99
100 public byte[] getBytes() {
101 return (byte[]) payload;
102 }
103
104 public PDU[] getChildren() {
105 assert isConstructed();
106 return (PDU[]) payload;
107 }
108
109 public Date getDate() {
110 assert getTag() == UTCTIME || getTag() == GENERALIZED_TIME;
111 return (Date) payload;
112 }
113
114}