001: /*
002: * @(#)GeneralNames.java 1.18 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.security.x509;
029:
030: import java.util.*;
031: import java.io.IOException;
032:
033: import sun.security.util.*;
034:
035: /**
036: * This object class represents the GeneralNames type required in
037: * X509 certificates.
038: * <p>The ASN.1 syntax for this is:
039: * <pre>
040: * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
041: * </pre>
042: *
043: * @author Amit Kapoor
044: * @author Hemma Prafullchandra
045: *
046: * @version 1.18, 10/10/06
047: */
048: public class GeneralNames {
049:
050: private final List names;
051:
052: /**
053: * Create the GeneralNames, decoding from the passed DerValue.
054: *
055: * @param derVal the DerValue to construct the GeneralNames from.
056: * @exception IOException on error.
057: */
058: public GeneralNames(DerValue derVal) throws IOException {
059: this ();
060: if (derVal.tag != DerValue.tag_Sequence) {
061: throw new IOException("Invalid encoding for GeneralNames.");
062: }
063: if (derVal.data.available() == 0) {
064: throw new IOException("No data available in "
065: + "passed DER encoded value.");
066: }
067: // Decode all the GeneralName's
068: while (derVal.data.available() != 0) {
069: DerValue encName = derVal.data.getDerValue();
070:
071: GeneralName name = new GeneralName(encName);
072: add(name);
073: }
074: }
075:
076: /**
077: * The default constructor for this class.
078: */
079: public GeneralNames() {
080: names = new ArrayList();
081: }
082:
083: public GeneralNames add(GeneralName name) {
084: if (name == null) {
085: throw new NullPointerException();
086: }
087: names.add(name);
088: return this ;
089: }
090:
091: public GeneralName get(int index) {
092: return (GeneralName) names.get(index);
093: }
094:
095: public boolean isEmpty() {
096: return names.isEmpty();
097: }
098:
099: public int size() {
100: return names.size();
101: }
102:
103: public Iterator iterator() {
104: return names.iterator();
105: }
106:
107: /**
108: * Write the extension to the DerOutputStream.
109: *
110: * @param out the DerOutputStream to write the extension to.
111: * @exception IOException on error.
112: */
113: public void encode(DerOutputStream out) throws IOException {
114: if (isEmpty()) {
115: return;
116: }
117:
118: DerOutputStream temp = new DerOutputStream();
119: for (Iterator t = iterator(); t.hasNext();) {
120: GeneralName gn = (GeneralName) t.next();
121: gn.encode(temp);
122: }
123: out.write(DerValue.tag_Sequence, temp);
124: }
125:
126: /**
127: * compare this GeneralNames to other object for equality
128: *
129: * @returns true iff this equals other
130: */
131: public boolean equals(Object obj) {
132: if (this == obj) {
133: return true;
134: }
135: if (obj instanceof GeneralNames == false) {
136: return false;
137: }
138: GeneralNames other = (GeneralNames) obj;
139: return this .names.equals(other.names);
140: }
141:
142: public int hashCode() {
143: return names.hashCode();
144: }
145:
146: public String toString() {
147: return names.toString();
148: }
149:
150: }
|