001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.geronimo.util.asn1; 019 020 import java.io.ByteArrayInputStream; 021 import java.io.ByteArrayOutputStream; 022 import java.io.IOException; 023 024 /** 025 * Base class for an application specific object 026 */ 027 public class DERApplicationSpecific 028 extends DERObject 029 { 030 private int tag; 031 private byte[] octets; 032 033 public DERApplicationSpecific( 034 int tag, 035 byte[] octets) 036 { 037 this.tag = tag; 038 this.octets = octets; 039 } 040 041 public DERApplicationSpecific( 042 int tag, 043 DEREncodable object) 044 throws IOException 045 { 046 this.tag = tag | DERTags.CONSTRUCTED; 047 048 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 049 DEROutputStream dos = new DEROutputStream(baos); 050 051 dos.writeObject(object); 052 053 this.octets = baos.toByteArray(); 054 } 055 056 public boolean isConstructed() 057 { 058 return (tag & DERTags.CONSTRUCTED) != 0; 059 } 060 061 public byte[] getContents() 062 { 063 return octets; 064 } 065 066 public int getApplicationTag() 067 { 068 return tag & 0x1F; 069 } 070 071 public DERObject getObject() 072 throws IOException 073 { 074 return new ASN1InputStream(new ByteArrayInputStream(getContents())).readObject(); 075 } 076 077 /* (non-Javadoc) 078 * @see org.apache.geronimo.util.asn1.DERObject#encode(org.apache.geronimo.util.asn1.DEROutputStream) 079 */ 080 void encode(DEROutputStream out) throws IOException 081 { 082 out.writeEncoded(DERTags.APPLICATION | tag, octets); 083 } 084 085 public boolean equals( 086 Object o) 087 { 088 if ((o == null) || !(o instanceof DERApplicationSpecific)) 089 { 090 return false; 091 } 092 093 DERApplicationSpecific other = (DERApplicationSpecific)o; 094 095 if (tag != other.tag) 096 { 097 return false; 098 } 099 100 if (octets.length != other.octets.length) 101 { 102 return false; 103 } 104 105 for (int i = 0; i < octets.length; i++) 106 { 107 if (octets[i] != other.octets[i]) 108 { 109 return false; 110 } 111 } 112 113 return true; 114 } 115 116 public int hashCode() 117 { 118 byte[] b = this.getContents(); 119 int value = 0; 120 121 for (int i = 0; i != b.length; i++) 122 { 123 value ^= (b[i] & 0xff) << (i % 4); 124 } 125 126 return value ^ this.getApplicationTag(); 127 } 128 }