View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.asn1;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  
25  /**
26   * Base class for an application specific object
27   */
28  public class DERApplicationSpecific
29      extends DERObject
30  {
31      private int       tag;
32      private byte[]    octets;
33  
34      public DERApplicationSpecific(
35          int        tag,
36          byte[]    octets)
37      {
38          this.tag = tag;
39          this.octets = octets;
40      }
41  
42      public DERApplicationSpecific(
43          int                  tag,
44          DEREncodable         object)
45          throws IOException
46      {
47          this.tag = tag | DERTags.CONSTRUCTED;
48  
49          ByteArrayOutputStream baos = new ByteArrayOutputStream();
50          DEROutputStream dos = new DEROutputStream(baos);
51  
52          dos.writeObject(object);
53  
54          this.octets = baos.toByteArray();
55      }
56  
57      public boolean isConstructed()
58      {
59          return (tag & DERTags.CONSTRUCTED) != 0;
60      }
61  
62      public byte[] getContents()
63      {
64          return octets;
65      }
66  
67      public int getApplicationTag()
68      {
69          return tag & 0x1F;
70      }
71  
72      public DERObject getObject()
73          throws IOException
74      {
75          return new ASN1InputStream(new ByteArrayInputStream(getContents())).readObject();
76      }
77  
78      /* (non-Javadoc)
79       * @see org.apache.geronimo.util.asn1.DERObject#encode(org.apache.geronimo.util.asn1.DEROutputStream)
80       */
81      void encode(DEROutputStream out) throws IOException
82      {
83          out.writeEncoded(DERTags.APPLICATION | tag, octets);
84      }
85  
86      public boolean equals(
87              Object o)
88      {
89          if ((o == null) || !(o instanceof DERApplicationSpecific))
90          {
91              return false;
92          }
93  
94          DERApplicationSpecific other = (DERApplicationSpecific)o;
95  
96          if (tag != other.tag)
97          {
98              return false;
99          }
100 
101         if (octets.length != other.octets.length)
102         {
103             return false;
104         }
105 
106         for (int i = 0; i < octets.length; i++)
107         {
108             if (octets[i] != other.octets[i])
109             {
110                 return false;
111             }
112         }
113 
114         return true;
115     }
116 
117     public int hashCode()
118     {
119         byte[]  b = this.getContents();
120         int     value = 0;
121 
122         for (int i = 0; i != b.length; i++)
123         {
124             value ^= (b[i] & 0xff) << (i % 4);
125         }
126 
127         return value ^ this.getApplicationTag();
128     }
129 }