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 /** 024 * DER NumericString object - this is an ascii string of characters {0,1,2,3,4,5,6,7,8,9, }. 025 */ 026 public class DERNumericString 027 extends DERObject 028 implements DERString 029 { 030 String string; 031 032 /** 033 * return a Numeric string from the passed in object 034 * 035 * @exception IllegalArgumentException if the object cannot be converted. 036 */ 037 public static DERNumericString getInstance( 038 Object obj) 039 { 040 if (obj == null || obj instanceof DERNumericString) 041 { 042 return (DERNumericString)obj; 043 } 044 045 if (obj instanceof ASN1OctetString) 046 { 047 return new DERNumericString(((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 an Numeric 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 DERNumericString getInstance( 068 ASN1TaggedObject obj, 069 boolean explicit) 070 { 071 return getInstance(obj.getObject()); 072 } 073 074 /** 075 * basic constructor - with bytes. 076 */ 077 public DERNumericString( 078 byte[] string) 079 { 080 char[] cs = new char[string.length]; 081 082 for (int i = 0; i != cs.length; i++) 083 { 084 cs[i] = (char)(string[i] & 0xff); 085 } 086 087 this.string = new String(cs); 088 } 089 090 /** 091 * basic constructor - with string. 092 */ 093 public DERNumericString( 094 String string) 095 { 096 this.string = string; 097 } 098 099 public String getString() 100 { 101 return string; 102 } 103 104 public byte[] getOctets() 105 { 106 char[] cs = string.toCharArray(); 107 byte[] bs = new byte[cs.length]; 108 109 for (int i = 0; i != cs.length; i++) 110 { 111 bs[i] = (byte)cs[i]; 112 } 113 114 return bs; 115 } 116 117 void encode( 118 DEROutputStream out) 119 throws IOException 120 { 121 out.writeEncoded(NUMERIC_STRING, this.getOctets()); 122 } 123 124 public int hashCode() 125 { 126 return this.getString().hashCode(); 127 } 128 129 public boolean equals( 130 Object o) 131 { 132 if (!(o instanceof DERNumericString)) 133 { 134 return false; 135 } 136 137 DERNumericString s = (DERNumericString)o; 138 139 return this.getString().equals(s.getString()); 140 } 141 }