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