View Javadoc

1   /*
2    *   Copyright 2005 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  
18  package org.apache.asn1.der;
19  
20  import java.io.IOException;
21  import java.util.Arrays;
22  
23  /***
24   * DER object.
25   */
26  public abstract class DERObject implements DEREncodable
27  {
28  	static final int TERMINATOR        = 0x00;
29      static final int BOOLEAN           = 0x01;
30      static final int INTEGER           = 0x02;
31      static final int BIT_STRING        = 0x03;
32      static final int OCTET_STRING      = 0x04;
33      static final int NULL              = 0x05;
34      static final int OBJECT_IDENTIFIER = 0x06;
35      static final int EXTERNAL          = 0x08;
36      static final int ENUMERATED        = 0x0a;
37      static final int SEQUENCE          = 0x10;
38      static final int SET               = 0x11;
39      static final int NUMERIC_STRING    = 0x12;
40      static final int PRINTABLE_STRING  = 0x13;
41      static final int T61_STRING        = 0x14;
42      static final int VIDEOTEX_STRING   = 0x15;
43      static final int IA5_STRING        = 0x16;
44      static final int UTC_TIME          = 0x17;
45      static final int GENERALIZED_TIME  = 0x18;
46      static final int GRAPHIC_STRING    = 0x19;
47      static final int VISIBLE_STRING    = 0x1a;
48      static final int GENERAL_STRING    = 0x1b;
49      static final int UNIVERSAL_STRING  = 0x1c;
50      static final int BMP_STRING        = 0x1e;
51      static final int UTF8_STRING       = 0x0c;
52      static final int CONSTRUCTED       = 0x20;
53      static final int APPLICATION       = 0x40;
54      static final int TAGGED            = 0x80;
55      
56      protected int    tag;
57      protected byte[] value;
58      
59      /***
60       * Basic DERObject constructor.
61       */
62      protected DERObject( int tag, byte[] value )
63      {
64      	this.tag   = tag;
65      	this.value = value;
66      }
67      
68      public void encode( ASN1OutputStream out )
69          throws IOException
70      {
71          out.writeEncoded( tag, value );
72      }
73      
74      byte[] getOctets()
75      {
76          return value;
77      }
78      
79      /***
80       * Fast rotate left and XOR hashcode generator.
81       * @return a hash code for the byte array backing this object.
82       */
83      public int hashCode()
84      {
85          int hash = 0;
86          int len = value.length;
87          
88          for ( int i = 0; i < len; i++ )
89          {
90          	// rotate left and xor
91              hash <<= 1;
92              if ( hash < 0 )
93              {
94              	hash |= 1;
95              }
96              hash ^= value[ i ];
97          }
98          
99          return hash;
100     }
101     
102     /***
103      * Two DERObjects are equal if their underlying byte arrays are equal.
104      * @return true if the two DERObject underlying byte arrays are equal.
105      */
106     public boolean equals( Object o )
107     {
108     	if ( this == o )
109     	{
110     		return true;
111     	}
112     	
113         if ( !( o instanceof DERObject ) )
114         {
115         	return false;
116         }
117         
118         DERObject that = (DERObject)o;
119         
120         return Arrays.equals( this.value, that.value );
121     }
122 }
123