generalsubtrees.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 537 行 · 第 1/2 页

JAVA
537
字号
/* * @(#)GeneralSubtrees.java	1.4 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program is distributed in the hope that it will be useful, but   * WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   * General Public License version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * *//* * Note that there are two versions of this file, this subsetted * CDC/FP version which avoids using the X400Address class, and * the "complete" one for the security optional package. Be sure * you're changing the correct file! */package sun.security.x509;import java.io.*;import java.util.*;import sun.security.util.*;/** * Represent the GeneralSubtrees ASN.1 object. * <p> * The ASN.1 for this is * <pre> * GeneralSubtrees ::= SEQUENCE SIZE (1..MAX) OF GeneralSubtree * </pre> * </p> * * @version 1.4, 10/10/06 * * @author Amit Kapoor * @author Hemma Prafullchandra * @author Andreas Sterbenz */public class GeneralSubtrees implements Cloneable {        private final List trees;    // Private variables    private static final int NAME_DIFF_TYPE = GeneralNameInterface.NAME_DIFF_TYPE;    private static final int NAME_MATCH = GeneralNameInterface.NAME_MATCH;    private static final int NAME_NARROWS = GeneralNameInterface.NAME_NARROWS;    private static final int NAME_WIDENS = GeneralNameInterface.NAME_WIDENS;    private static final int NAME_SAME_TYPE = GeneralNameInterface.NAME_SAME_TYPE;    /**     * The default constructor for the class.     */    public GeneralSubtrees() {	trees = new ArrayList();    }        private GeneralSubtrees(GeneralSubtrees source) {	ArrayList sourceTrees = (ArrayList)source.trees;	trees = (List)sourceTrees.clone();    }    /**     * Create the object from the passed DER encoded form.     *     * @param val the DER encoded form of the same.     */    public GeneralSubtrees(DerValue val) throws IOException {	this();        if (val.tag != DerValue.tag_Sequence) {            throw new IOException("Invalid encoding of GeneralSubtrees.");        }        while (val.data.available() != 0) {            DerValue opt = val.data.getDerValue();            GeneralSubtree tree = new GeneralSubtree(opt);            add(tree);        }    }        public GeneralSubtree get(int index) {	return (GeneralSubtree)trees.get(index);    }        public void remove(int index) {	trees.remove(index);    }        public void add(GeneralSubtree tree) {	if (tree == null) {	    throw new NullPointerException();	}	trees.add(tree);    }        public boolean contains(GeneralSubtree tree) {	if (tree == null) {	    throw new NullPointerException();	}	return trees.contains(tree);    }        public int size() {	return trees.size();    }        public Iterator iterator() {	return trees.iterator();    }        public Object clone() {	return new GeneralSubtrees(this);    }    /**     * Return a printable string of the GeneralSubtree.     */    public String toString() {        String s = "   GeneralSubtrees:\n" + trees.toString() + "\n";        return s;    }    /**     * Encode the GeneralSubtrees.     *     * @params out the DerOutputStrean to encode this object to.     */    public void encode(DerOutputStream out) throws IOException {        DerOutputStream seq = new DerOutputStream();        for (int i = 0, n = size(); i < n; i++) {            get(i).encode(seq);        }        out.write(DerValue.tag_Sequence, seq);    }    /**     * Compare two general subtrees by comparing the subtrees     * of each.     *     * @param other GeneralSubtrees to compare to this     * @returns true if match     */    public boolean equals(Object obj) {	if (this == obj) {	    return true;	}	if (obj instanceof GeneralSubtrees == false) {	    return false;	}	GeneralSubtrees other = (GeneralSubtrees)obj;	return this.trees.equals(other.trees);    }        public int hashCode() {	return trees.hashCode();    }    /**     * Return the GeneralNameInterface form of the GeneralName in one of     * the GeneralSubtrees.     *     * @param ndx index of the GeneralSubtree from which to obtain the name     */    private GeneralNameInterface getGeneralNameInterface(int ndx) {	return getGeneralNameInterface(get(ndx));    }        private static GeneralNameInterface getGeneralNameInterface(GeneralSubtree gs) {	GeneralName gn = gs.getName();	GeneralNameInterface gni = gn.getName();	return gni;    }    /**     * minimize this GeneralSubtrees by removing all redundant entries.     * Internal method used by intersect and reduce.     */    private void minimize() {	// Algorithm: compare each entry n to all subsequent entries in         // the list: if any subsequent entry matches or widens entry n, 	// remove entry n. If any subsequent entries narrow entry n, remove 	// the subsequent entries.	for (int i = 0; i < size(); i++) {	    GeneralNameInterface current = getGeneralNameInterface(i);	    boolean remove1 = false;	    /* compare current to subsequent elements */	    for (int j = i + 1; j < size(); j++) {		GeneralNameInterface subsequent = getGeneralNameInterface(j);		switch (current.constrains(subsequent)) {		    case GeneralNameInterface.NAME_DIFF_TYPE:			/* not comparable; different name types; keep checking */			continue;		    case GeneralNameInterface.NAME_MATCH:			/* delete one of the duplicates */			remove1 = true;			break;		    case GeneralNameInterface.NAME_NARROWS: 			/* subsequent narrows current */			/* remove narrower name (subsequent) */			remove(j);			j--; /* continue check with new subsequent */			continue;		    case GeneralNameInterface.NAME_WIDENS: 			/* subsequent widens current */			/* remove narrower name current */			remove1 = true;			break;		    case GeneralNameInterface.NAME_SAME_TYPE:			/* keep both for now; keep checking */			continue;		}		break;	    } /* end of this pass of subsequent elements */	    if (remove1) {		remove(i);		i--; /* check the new i value */	    }	}    }    /**     * create a subtree containing an instance of the input     * name type that widens all other names of that type.     *     * @params name GeneralNameInterface name     * @returns GeneralSubtree containing widest name of that type     * @throws RuntimeException on error (should not occur)     */    private GeneralSubtree createWidestSubtree(GeneralNameInterface name) {	try {	    GeneralName newName;	    switch (name.getType()) {	    case GeneralNameInterface.NAME_ANY:		// Create new OtherName with same OID as baseName, but 		// empty value		ObjectIdentifier otherOID = ((OtherName)name).getOID();		newName = new GeneralName(new OtherName(otherOID, null));		break;	    case GeneralNameInterface.NAME_RFC822:		newName = new GeneralName(new RFC822Name(""));		break;	    case GeneralNameInterface.NAME_DNS:	        newName = new GeneralName(new DNSName(""));		break;            /*             * CDC/FP subsets X400Address into the security             * optional package.	    case GeneralNameInterface.NAME_X400:

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?