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.ByteArrayOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  
25  public class DERObjectIdentifier extends DERObject
26  {
27      String identifier;
28  
29      DERObjectIdentifier( byte[] bytes )
30      {
31      	super( OBJECT_IDENTIFIER, bytes );
32      	
33          StringBuffer objId = new StringBuffer();
34          long         value = 0;
35          boolean      first = true;
36  
37          for ( int i = 0; i != bytes.length; i++ )
38          {
39              int b = bytes[ i ] & 0xff;
40  
41              value = value * 128 + ( b & 0x7f );
42              if ( ( b & 0x80 ) == 0 )             // end of number reached
43              {
44                  if ( first )
45                  {
46                      switch ( (int)value / 40 )
47                      {
48                      case 0:
49                          objId.append( '0' );
50                          break;
51                      case 1:
52                          objId.append( '1' );
53                          value -= 40;
54                          break;
55                      default:
56                          objId.append( '2' );
57                          value -= 80;
58                      }
59                      first = false;
60                  }
61  
62                  objId.append( '.' );
63                  objId.append( Long.toString( value ) );
64                  value = 0;
65              }
66          }
67  
68          this.identifier = objId.toString();
69      }
70      
71      private void writeField( OutputStream out, long fieldValue )
72          throws IOException
73      {
74          if ( fieldValue >= ( 1 << 7 ) )
75          {
76              if ( fieldValue >= ( 1 << 14 ) )
77              {
78                  if ( fieldValue >= ( 1 << 21 ) )
79                  {
80                      if ( fieldValue >= ( 1 << 28 ) )
81                      {
82                          if ( fieldValue >= ( 1 << 35 ) )
83                          {
84                              if ( fieldValue >= ( 1 << 42 ) )
85                              {
86                                  if ( fieldValue >= ( 1 << 49 ) )
87                                  {
88                                      if ( fieldValue >= ( 1 << 56 ) )
89                                      {
90                                          out.write( (int)( fieldValue >> 56 ) | 0x80 );
91                                      }
92                                      out.write( (int)( fieldValue >> 49 ) | 0x80 );
93                                  }
94                                  out.write( (int)( fieldValue >> 42 ) | 0x80 );
95                              }
96                              out.write( (int)( fieldValue >> 35 ) | 0x80 );
97                          }
98                          out.write( (int)( fieldValue >> 28 ) | 0x80 );
99                      }
100                     out.write( (int)( fieldValue >> 21 ) | 0x80 );
101                 }
102                 out.write( (int)( fieldValue >> 14 ) | 0x80 );
103             }
104             out.write( (int)( fieldValue >> 7 ) | 0x80 );
105         }
106         out.write( (int)fieldValue & 0x7f );
107     }
108 
109     public void encode( ASN1OutputStream out )
110         throws IOException
111     {
112         OIDTokenizer          tok  = new OIDTokenizer( identifier );
113         ByteArrayOutputStream baos = new ByteArrayOutputStream();
114         ASN1OutputStream      aos  = new ASN1OutputStream( baos );
115 
116         writeField( baos, Integer.parseInt( tok.nextToken() ) * 40
117                     + Integer.parseInt( tok.nextToken() ) );
118 
119         while ( tok.hasMoreTokens() )
120         {
121             writeField( baos, Long.parseLong( tok.nextToken() ) );
122         }
123 
124         aos.close();
125 
126         byte[] bytes = baos.toByteArray();
127 
128         out.writeEncoded( OBJECT_IDENTIFIER, bytes );
129     }
130 }
131