1 /* $Id: AsnSequence.java,v 1.1 2000/08/25 01:11:43 gelderen Exp $
2  *
3  * Copyright (c) 2000 The Cryptix Foundation Limited. All rights reserved.
4  */
5 
6 package cryptix.jce.provider.asn;
7 
8 
9 import java.io.IOException;
10 import java.util.Vector;
11 
12 
13 /**
14  * Immutable object representing an ASN.1 SEQUENCE.
15  *
16  * @version $Revision: 1.1 $
17  * @author  Jeroen C. van Gelderen (gelderen@cryptix.org)
18  */
19 public final class AsnSequence extends AsnObject
20 {
21     private final AsnObject[] vals;
22 
23 
AsnSequence(AsnInputStream is)24     /*package*/ AsnSequence(AsnInputStream is) throws IOException {
25         super(AsnObject.TAG_SEQUENCE);
26 
27         int len = is.readLength();
28         AsnInputStream sub_is = is.getSubStream( len );
29         Vector vec = new Vector(3);
30         while( sub_is.available() > 0 )
31             vec.addElement( sub_is.read() );
32         vec.copyInto(this.vals = new AsnObject[ vec.size() ]);
33     }
34 
35 
AsnSequence(AsnObject[] vals)36     public AsnSequence(AsnObject[] vals) {
37         super(AsnObject.TAG_SEQUENCE);
38 
39         this.vals = (AsnObject[])vals.clone();
40     }
41 
42 
AsnSequence(AsnObject a, AsnObject b)43     public AsnSequence(AsnObject a, AsnObject b) {
44         super(AsnObject.TAG_SEQUENCE);
45         AsnObject[] objs = new AsnObject[2];
46         objs[0] = a;
47         objs[1] = b;
48         this.vals = objs;
49     }
50 
51 
toString(String indent)52     public String toString(String indent) {
53         String s = indent + "SEQUENCE (" + this.vals.length +" elements):";
54         for(int i=0; i<this.vals.length; i++)
55             s += "\n" + this.vals[i].toString(indent + "    ");
56 
57         return s;
58     }
59 
60 
get(int index)61     public AsnObject get(int index) {
62         return this.vals[index];
63     }
64 
65 
size()66     public int size() {
67         return this.vals.length;
68     }
69 
70 
71     /** Write out payload. */
encodePayload(AsnOutputStream os)72     protected void encodePayload(AsnOutputStream os) throws IOException {
73         for(int i=0; i<this.vals.length; i++)
74             os.write(this.vals[i]);
75     }
76 
77 
getEncodedLengthOfPayload(AsnOutputStream os)78     protected int getEncodedLengthOfPayload(AsnOutputStream os) {
79         int len = 0;
80         for(int i=0; i<this.vals.length; i++)
81             len += this.vals[i].getEncodedLength(os);
82 
83         return len;
84     }
85 }
86