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.FilterOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.nio.ByteBuffer;
24  
25  
26  public class ASN1OutputStream extends FilterOutputStream
27  {
28      public ASN1OutputStream( OutputStream os )
29      {
30          super( os );
31      }
32      
33      public ASN1OutputStream( ByteBuffer out )
34      {
35          super( newOutputStream( out ) );
36      }
37      
38  	public static OutputStream newOutputStream( final ByteBuffer buf )
39  	{
40          return new OutputStream()
41  		{
42              public synchronized void write( int integer ) throws IOException
43  			{
44                  buf.put( (byte)integer );
45              }
46      
47              public synchronized void write( byte[] bytes, int off, int len ) throws IOException
48  			{
49                  buf.put( bytes, off, len );
50              }
51          };
52      }
53  
54      private void writeLength( int length )
55          throws IOException
56      {
57          if ( length > 127 )
58          {
59              int size = 1;
60              int val = length;
61  
62              while ( ( val >>>= 8 ) != 0 )
63              {
64                  size++;
65              }
66              
67              write( (byte)( size | 0x80 ) );
68  
69              for ( int i = (size - 1) * 8; i >= 0; i -= 8 )
70              {
71                  write( (byte)( length >> i ) );
72              }
73          }
74          else
75          {
76              write( (byte)length );
77          }
78      }
79  
80      void writeEncoded( int tag, byte[] bytes )
81          throws IOException
82      {
83          write( tag );
84          writeLength( bytes.length );
85          write( bytes );
86      }
87  
88      public void writeObject( Object obj )
89          throws IOException
90      {
91          if ( obj == null )
92          {
93              writeNull();
94          }
95          else if ( obj instanceof DEREncodable )
96          {
97              ( (DEREncodable)obj ).encode( this );
98          }
99          else 
100         {
101             throw new IOException( "Object not DEREncodable." );
102         }
103     }
104     
105     protected void writeNull()
106         throws IOException
107     {
108         write( DERObject.NULL );
109         write( DERObject.TERMINATOR );
110     }
111 }
112