001 /* 002 * Copyright (c) 2000 - 2006 The Legion Of The Bouncy Castle (http://www.bouncycastle.org) 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 005 * software and associated documentation files (the "Software"), to deal in the Software 006 * without restriction, including without limitation the rights to use, copy, modify, 007 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 008 * permit persons to whom the Software is furnished to do so, subject to the following 009 * conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in all copies 012 * or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 015 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 016 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 017 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 018 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 019 * DEALINGS IN THE SOFTWARE. 020 * 021 */ 022 023 package org.apache.directory.shared.asn1.der; 024 025 026 import java.io.ByteArrayOutputStream; 027 import java.io.IOException; 028 import java.util.Enumeration; 029 import java.util.Vector; 030 031 032 public class DERSequence implements DEREncodable 033 { 034 private Vector<DEREncodable> v = new Vector<DEREncodable>(); 035 036 037 public void add( DEREncodable obj ) 038 { 039 v.addElement( obj ); 040 } 041 042 043 public Enumeration<DEREncodable> getObjects() 044 { 045 return v.elements(); 046 } 047 048 049 public DEREncodable get( int i ) 050 { 051 return ( DEREncodable ) v.elementAt( i ); 052 } 053 054 055 public int size() 056 { 057 return v.size(); 058 } 059 060 061 /** 062 * As DER requires the constructed, definite-length model to be used for 063 * structured types, this varies slightly from the ASN.1 descriptions given. 064 * Rather than just outputing SEQUENCE, we also have to specify CONSTRUCTED, 065 * and the objects length. 066 */ 067 public void encode( ASN1OutputStream out ) throws IOException 068 { 069 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 070 ASN1OutputStream aos = new ASN1OutputStream( baos ); 071 072 Enumeration<DEREncodable> e = getObjects(); 073 074 while ( e.hasMoreElements() ) 075 { 076 aos.writeObject( e.nextElement() ); 077 } 078 079 aos.close(); 080 081 byte[] bytes = baos.toByteArray(); 082 083 out.writeEncoded( DERObject.SEQUENCE | DERObject.CONSTRUCTED, bytes ); 084 } 085 }