01: package org.bouncycastle.asn1;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05:
06: public abstract class ASN1Encodable implements DEREncodable {
07: public static final String DER = "DER";
08: public static final String BER = "BER";
09:
10: public byte[] getEncoded() throws IOException {
11: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
12: ASN1OutputStream aOut = new ASN1OutputStream(bOut);
13:
14: aOut.writeObject(this );
15:
16: return bOut.toByteArray();
17: }
18:
19: public byte[] getEncoded(String encoding) throws IOException {
20: if (encoding.equals(DER)) {
21: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
22: DEROutputStream dOut = new DEROutputStream(bOut);
23:
24: dOut.writeObject(this );
25:
26: return bOut.toByteArray();
27: }
28:
29: return this .getEncoded();
30: }
31:
32: /**
33: * Return the DER encoding of the object, null if the DER encoding can not be made.
34: *
35: * @return a DER byte array, null otherwise.
36: */
37: public byte[] getDEREncoded() {
38: try {
39: return this .getEncoded(DER);
40: } catch (IOException e) {
41: return null;
42: }
43: }
44:
45: public int hashCode() {
46: return this .toASN1Object().hashCode();
47: }
48:
49: public boolean equals(Object o) {
50: if (this == o) {
51: return true;
52: }
53:
54: if (!(o instanceof DEREncodable)) {
55: return false;
56: }
57:
58: DEREncodable other = (DEREncodable) o;
59:
60: return this .toASN1Object().equals(other.getDERObject());
61: }
62:
63: public DERObject getDERObject() {
64: return this .toASN1Object();
65: }
66:
67: public abstract DERObject toASN1Object();
68: }
|