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 */ 017 018 package org.apache.geronimo.util.asn1; 019 020 import java.io.IOException; 021 022 /** 023 * DER BMPString object. 024 */ 025 public class DERBMPString 026 extends DERObject 027 implements DERString 028 { 029 String string; 030 031 /** 032 * return a BMP String from the given object. 033 * 034 * @param obj the object we want converted. 035 * @exception IllegalArgumentException if the object cannot be converted. 036 */ 037 public static DERBMPString getInstance( 038 Object obj) 039 { 040 if (obj == null || obj instanceof DERBMPString) 041 { 042 return (DERBMPString)obj; 043 } 044 045 if (obj instanceof ASN1OctetString) 046 { 047 return new DERBMPString(((ASN1OctetString)obj).getOctets()); 048 } 049 050 if (obj instanceof ASN1TaggedObject) 051 { 052 return getInstance(((ASN1TaggedObject)obj).getObject()); 053 } 054 055 throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName()); 056 } 057 058 /** 059 * return a BMP String from a tagged object. 060 * 061 * @param obj the tagged object holding the object we want 062 * @param explicit true if the object is meant to be explicitly 063 * tagged false otherwise. 064 * @exception IllegalArgumentException if the tagged object cannot 065 * be converted. 066 */ 067 public static DERBMPString getInstance( 068 ASN1TaggedObject obj, 069 boolean explicit) 070 { 071 return getInstance(obj.getObject()); 072 } 073 074 075 /** 076 * basic constructor - byte encoded string. 077 */ 078 public DERBMPString( 079 byte[] string) 080 { 081 char[] cs = new char[string.length / 2]; 082 083 for (int i = 0; i != cs.length; i++) 084 { 085 cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff)); 086 } 087 088 this.string = new String(cs); 089 } 090 091 /** 092 * basic constructor 093 */ 094 public DERBMPString( 095 String string) 096 { 097 this.string = string; 098 } 099 100 public String getString() 101 { 102 return string; 103 } 104 105 public int hashCode() 106 { 107 return this.getString().hashCode(); 108 } 109 110 public boolean equals( 111 Object o) 112 { 113 if (!(o instanceof DERBMPString)) 114 { 115 return false; 116 } 117 118 DERBMPString s = (DERBMPString)o; 119 120 return this.getString().equals(s.getString()); 121 } 122 123 void encode( 124 DEROutputStream out) 125 throws IOException 126 { 127 char[] c = string.toCharArray(); 128 byte[] b = new byte[c.length * 2]; 129 130 for (int i = 0; i != c.length; i++) 131 { 132 b[2 * i] = (byte)(c[i] >> 8); 133 b[2 * i + 1] = (byte)c[i]; 134 } 135 136 out.writeEncoded(BMP_STRING, b); 137 } 138 }