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