KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > asn1 > DERApplicationSpecific


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.asn1;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * Base class for an application specific object
26  */

27 public class DERApplicationSpecific
28     extends DERObject
29 {
30     private int tag;
31     private byte[] octets;
32
33     public DERApplicationSpecific(
34         int tag,
35         byte[] octets)
36     {
37         this.tag = tag;
38         this.octets = octets;
39     }
40
41     public DERApplicationSpecific(
42         int tag,
43         DEREncodable object)
44         throws IOException JavaDoc
45     {
46         this.tag = tag | DERTags.CONSTRUCTED;
47
48         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
49         DEROutputStream dos = new DEROutputStream(baos);
50
51         dos.writeObject(object);
52
53         this.octets = baos.toByteArray();
54     }
55
56     public boolean isConstructed()
57     {
58         return (tag & DERTags.CONSTRUCTED) != 0;
59     }
60
61     public byte[] getContents()
62     {
63         return octets;
64     }
65
66     public int getApplicationTag()
67     {
68         return tag & 0x1F;
69     }
70
71     public DERObject getObject()
72         throws IOException JavaDoc
73     {
74         return new ASN1InputStream(new ByteArrayInputStream JavaDoc(getContents())).readObject();
75     }
76
77     /* (non-Javadoc)
78      * @see org.apache.geronimo.util.asn1.DERObject#encode(org.apache.geronimo.util.asn1.DEROutputStream)
79      */

80     void encode(DEROutputStream out) throws IOException JavaDoc
81     {
82         out.writeEncoded(DERTags.APPLICATION | tag, octets);
83     }
84
85     public boolean equals(
86             Object JavaDoc o)
87     {
88         if ((o == null) || !(o instanceof DERApplicationSpecific))
89         {
90             return false;
91         }
92
93         DERApplicationSpecific other = (DERApplicationSpecific)o;
94
95         if (tag != other.tag)
96         {
97             return false;
98         }
99
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 }
129
Popular Tags