1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: public class DERValue implements DER
45: {
46:
47:
48:
49:
50: private final int tagClass;
51: private final boolean constructed;
52: private final int tag;
53: private int length;
54: private final Object value;
55: private byte[] encoded;
56:
57:
58:
59:
60: public DERValue(int tag, int length, Object value, byte[] encoded)
61: {
62: tagClass = tag & 0xC0;
63: this.tag = tag & 0x1F;
64: constructed = (tag & CONSTRUCTED) == CONSTRUCTED;
65: this.length = length;
66: this.value = value;
67: if (encoded != null)
68: this.encoded = (byte[]) encoded.clone();
69: }
70:
71: public DERValue(int tag, Object value)
72: {
73: this(tag, 0, value, null);
74: }
75:
76:
77:
78:
79: public int getExternalTag()
80: {
81: return tagClass | tag | (constructed ? 0x20 : 0x00);
82: }
83:
84: public int getTag()
85: {
86: return tag;
87: }
88:
89: public int getTagClass()
90: {
91: return tagClass;
92: }
93:
94: public boolean isConstructed()
95: {
96: return constructed;
97: }
98:
99: public int getLength()
100: {
101: if (encoded == null)
102: {
103: try
104: {
105: ByteArrayOutputStream out = new ByteArrayOutputStream();
106: length = DERWriter.write(out, this);
107: encoded = out.toByteArray();
108: }
109: catch (IOException ioe)
110: {
111: encoded = new byte[0];
112: }
113: }
114: return length;
115: }
116:
117: public Object getValue()
118: {
119: return value;
120: }
121:
122: public Object getValueAs (final int derType) throws IOException
123: {
124: byte[] encoded = getEncoded ();
125: encoded[0] = (byte) derType;
126: return DERReader.read (encoded).getValue ();
127: }
128:
129: public byte[] getEncoded()
130: {
131: if (encoded == null)
132: {
133: try
134: {
135: ByteArrayOutputStream out = new ByteArrayOutputStream();
136: length = DERWriter.write(out, this);
137: encoded = out.toByteArray();
138: }
139: catch (IOException ioe)
140: {
141: encoded = new byte[0];
142: }
143: }
144: return (byte[]) encoded.clone();
145: }
146:
147: public int getEncodedLength()
148: {
149: if (encoded == null)
150: {
151: try
152: {
153: ByteArrayOutputStream out = new ByteArrayOutputStream();
154: length = DERWriter.write(out, this);
155: encoded = out.toByteArray();
156: }
157: catch (IOException ioe)
158: {
159: encoded = new byte[0];
160: }
161: }
162: return encoded.length;
163: }
164:
165: public String toString()
166: {
167: return "DERValue [ tag=" + tag + ", class=" + tagClass + ", constructed="
168: + constructed + ", value=" + value + " ]";
169: }
170: }