Path: blob/master/src/java.base/share/classes/sun/security/x509/GeneralNames.java
41159 views
/*1* Copyright (c) 1997, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.x509;2627import java.util.*;28import java.io.IOException;2930import sun.security.util.*;3132/**33* This object class represents the GeneralNames type required in34* X509 certificates.35* <p>The ASN.1 syntax for this is:36* <pre>37* GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName38* </pre>39*40* @author Amit Kapoor41* @author Hemma Prafullchandra42*43*/44public class GeneralNames {4546private final List<GeneralName> names;4748/**49* Create the GeneralNames, decoding from the passed DerValue.50*51* @param derVal the DerValue to construct the GeneralNames from.52* @exception IOException on error.53*/54public GeneralNames(DerValue derVal) throws IOException {55this();56if (derVal.tag != DerValue.tag_Sequence) {57throw new IOException("Invalid encoding for GeneralNames.");58}59if (derVal.data.available() == 0) {60throw new IOException("No data available in "61+ "passed DER encoded value.");62}63// Decode all the GeneralName's64while (derVal.data.available() != 0) {65DerValue encName = derVal.data.getDerValue();6667GeneralName name = new GeneralName(encName);68add(name);69}70}7172/**73* The default constructor for this class.74*/75public GeneralNames() {76names = new ArrayList<GeneralName>();77}7879public GeneralNames add(GeneralName name) {80if (name == null) {81throw new NullPointerException();82}83names.add(name);84return this;85}8687public GeneralName get(int index) {88return names.get(index);89}9091public boolean isEmpty() {92return names.isEmpty();93}9495public int size() {96return names.size();97}9899public Iterator<GeneralName> iterator() {100return names.iterator();101}102103public List<GeneralName> names() {104return names;105}106107/**108* Write the extension to the DerOutputStream.109*110* @param out the DerOutputStream to write the extension to.111* @exception IOException on error.112*/113public void encode(DerOutputStream out) throws IOException {114if (isEmpty()) {115return;116}117118DerOutputStream temp = new DerOutputStream();119for (GeneralName gn : names) {120gn.encode(temp);121}122out.write(DerValue.tag_Sequence, temp);123}124125/**126* compare this GeneralNames to other object for equality127*128* @return true if this equals obj129*/130public boolean equals(Object obj) {131if (this == obj) {132return true;133}134if (obj instanceof GeneralNames == false) {135return false;136}137GeneralNames other = (GeneralNames)obj;138return this.names.equals(other.names);139}140141public int hashCode() {142return names.hashCode();143}144145public String toString() {146return names.toString();147}148149}150151152