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 public class DERGeneralString 023 extends DERObject implements DERString 024 { 025 private String string; 026 027 public static DERGeneralString getInstance( 028 Object obj) 029 { 030 if (obj == null || obj instanceof DERGeneralString) 031 { 032 return (DERGeneralString) obj; 033 } 034 if (obj instanceof ASN1OctetString) 035 { 036 return new DERGeneralString(((ASN1OctetString) obj).getOctets()); 037 } 038 if (obj instanceof ASN1TaggedObject) 039 { 040 return getInstance(((ASN1TaggedObject) obj).getObject()); 041 } 042 throw new IllegalArgumentException("illegal object in getInstance: " 043 + obj.getClass().getName()); 044 } 045 046 public static DERGeneralString getInstance( 047 ASN1TaggedObject obj, 048 boolean explicit) 049 { 050 return getInstance(obj.getObject()); 051 } 052 053 public DERGeneralString(byte[] string) 054 { 055 char[] cs = new char[string.length]; 056 for (int i = 0; i != cs.length; i++) 057 { 058 cs[i] = (char)(string[i] & 0xff); 059 } 060 this.string = new String(cs); 061 } 062 063 public DERGeneralString(String string) 064 { 065 this.string = string; 066 } 067 068 public String getString() 069 { 070 return string; 071 } 072 073 public byte[] getOctets() 074 { 075 char[] cs = string.toCharArray(); 076 byte[] bs = new byte[cs.length]; 077 for (int i = 0; i != cs.length; i++) 078 { 079 bs[i] = (byte) cs[i]; 080 } 081 return bs; 082 } 083 084 void encode(DEROutputStream out) 085 throws IOException 086 { 087 out.writeEncoded(GENERAL_STRING, this.getOctets()); 088 } 089 090 public int hashCode() 091 { 092 return this.getString().hashCode(); 093 } 094 095 public boolean equals(Object o) 096 { 097 if (!(o instanceof DERGeneralString)) 098 { 099 return false; 100 } 101 DERGeneralString s = (DERGeneralString) o; 102 return this.getString().equals(s.getString()); 103 } 104 }