001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.crypto.asn1;
017:
018: import java.io.IOException;
019:
020: /**
021: * DER BMPString object.
022: */
023: public class DERBMPString extends DERObject implements DERString {
024: String string;
025:
026: /**
027: * return a BMP String from the given object.
028: *
029: * @param obj the object we want converted.
030: * @exception IllegalArgumentException if the object cannot be converted.
031: */
032: public static DERBMPString getInstance(Object obj) {
033: if (obj == null || obj instanceof DERBMPString) {
034: return (DERBMPString) obj;
035: }
036:
037: if (obj instanceof ASN1OctetString) {
038: return new DERBMPString(((ASN1OctetString) obj).getOctets());
039: }
040:
041: if (obj instanceof ASN1TaggedObject) {
042: return getInstance(((ASN1TaggedObject) obj).getObject());
043: }
044:
045: throw new IllegalArgumentException(
046: "illegal object in getInstance: "
047: + obj.getClass().getName());
048: }
049:
050: /**
051: * return a BMP String from a tagged object.
052: *
053: * @param obj the tagged object holding the object we want
054: * @param explicit true if the object is meant to be explicitly
055: * tagged false otherwise.
056: * @exception IllegalArgumentException if the tagged object cannot
057: * be converted.
058: */
059: public static DERBMPString getInstance(ASN1TaggedObject obj,
060: boolean explicit) {
061: return getInstance(obj.getObject());
062: }
063:
064: /**
065: * basic constructor - byte encoded string.
066: */
067: public DERBMPString(byte[] string) {
068: char[] cs = new char[string.length / 2];
069:
070: for (int i = 0; i != cs.length; i++) {
071: cs[i] = (char) ((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
072: }
073:
074: this .string = new String(cs);
075: }
076:
077: /**
078: * basic constructor
079: */
080: public DERBMPString(String string) {
081: this .string = string;
082: }
083:
084: public String getString() {
085: return string;
086: }
087:
088: public int hashCode() {
089: return this .getString().hashCode();
090: }
091:
092: public boolean equals(Object o) {
093: if (!(o instanceof DERBMPString)) {
094: return false;
095: }
096:
097: DERBMPString s = (DERBMPString) o;
098:
099: return this .getString().equals(s.getString());
100: }
101:
102: void encode(DEROutputStream out) throws IOException {
103: char[] c = string.toCharArray();
104: byte[] b = new byte[c.length * 2];
105:
106: for (int i = 0; i != c.length; i++) {
107: b[2 * i] = (byte) (c[i] >> 8);
108: b[2 * i + 1] = (byte) c[i];
109: }
110:
111: out.writeEncoded(BMP_STRING, b);
112: }
113: }
|