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: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021:
022: /**
023: * Base class for an application specific object
024: */
025: public class DERApplicationSpecific extends DERObject {
026: private int tag;
027: private byte[] octets;
028:
029: public DERApplicationSpecific(int tag, byte[] octets) {
030: this .tag = tag;
031: this .octets = octets;
032: }
033:
034: public DERApplicationSpecific(int tag, DEREncodable object)
035: throws IOException {
036: this .tag = tag | DERTags.CONSTRUCTED;
037:
038: ByteArrayOutputStream baos = new ByteArrayOutputStream();
039: DEROutputStream dos = new DEROutputStream(baos);
040:
041: dos.writeObject(object);
042:
043: this .octets = baos.toByteArray();
044: }
045:
046: public boolean isConstructed() {
047: return (tag & DERTags.CONSTRUCTED) != 0;
048: }
049:
050: public byte[] getContents() {
051: return octets;
052: }
053:
054: public int getApplicationTag() {
055: return tag & 0x1F;
056: }
057:
058: public DERObject getObject() throws IOException {
059: return new ASN1InputStream(new ByteArrayInputStream(
060: getContents())).readObject();
061: }
062:
063: /* (non-Javadoc)
064: * @see org.apache.geronimo.crypto.asn1.DERObject#encode(org.apache.geronimo.crypto.asn1.DEROutputStream)
065: */
066: void encode(DEROutputStream out) throws IOException {
067: out.writeEncoded(DERTags.APPLICATION | tag, octets);
068: }
069:
070: public boolean equals(Object o) {
071: if ((o == null) || !(o instanceof DERApplicationSpecific)) {
072: return false;
073: }
074:
075: DERApplicationSpecific other = (DERApplicationSpecific) o;
076:
077: if (tag != other.tag) {
078: return false;
079: }
080:
081: if (octets.length != other.octets.length) {
082: return false;
083: }
084:
085: for (int i = 0; i < octets.length; i++) {
086: if (octets[i] != other.octets[i]) {
087: return false;
088: }
089: }
090:
091: return true;
092: }
093:
094: public int hashCode() {
095: byte[] b = this .getContents();
096: int value = 0;
097:
098: for (int i = 0; i != b.length; i++) {
099: value ^= (b[i] & 0xff) << (i % 4);
100: }
101:
102: return value ^ this.getApplicationTag();
103: }
104: }
|