001 package org.apache.commons.ssl.asn1; 002 003 import java.io.ByteArrayOutputStream; 004 import java.io.IOException; 005 006 public abstract class ASN1Encodable 007 implements DEREncodable { 008 public static final String DER = "DER"; 009 public static final String BER = "BER"; 010 011 public byte[] getEncoded() 012 throws IOException { 013 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 014 ASN1OutputStream aOut = new ASN1OutputStream(bOut); 015 016 aOut.writeObject(this); 017 018 return bOut.toByteArray(); 019 } 020 021 public byte[] getEncoded( 022 String encoding) 023 throws IOException { 024 if (encoding.equals(DER)) { 025 ByteArrayOutputStream bOut = new ByteArrayOutputStream(); 026 DEROutputStream dOut = new DEROutputStream(bOut); 027 028 dOut.writeObject(this); 029 030 return bOut.toByteArray(); 031 } 032 033 return this.getEncoded(); 034 } 035 036 /** 037 * Return the DER encoding of the object, null if the DER encoding can not be made. 038 * 039 * @return a DER byte array, null otherwise. 040 */ 041 public byte[] getDEREncoded() { 042 try { 043 return this.getEncoded(DER); 044 } 045 catch (IOException e) { 046 return null; 047 } 048 } 049 050 public int hashCode() { 051 return this.toASN1Object().hashCode(); 052 } 053 054 public boolean equals( 055 Object o) { 056 if (this == o) { 057 return true; 058 } 059 060 if (!(o instanceof DEREncodable)) { 061 return false; 062 } 063 064 DEREncodable other = (DEREncodable) o; 065 066 return this.toASN1Object().equals(other.getDERObject()); 067 } 068 069 public DERObject getDERObject() { 070 return this.toASN1Object(); 071 } 072 073 public abstract DERObject toASN1Object(); 074 }