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