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: /**
019: * @author Vladimir N. Molotkov, Stepan M. Mishura
020: * @version $Revision$
021: */package org.apache.harmony.security.asn1;
022:
023: import java.io.IOException;
024: import java.io.UnsupportedEncodingException;
025:
026: /**
027: * This class is the super class for all string ASN.1 types
028: *
029: * @see http://asn1.elibel.tm.fr/en/standards/index.htm
030: */
031:
032: public abstract class ASN1StringType extends ASN1Type {
033:
034: // TODO: what about defining them as separate classes?
035: // TODO: check decoded/encoded characters
036: public static final ASN1StringType BMPSTRING = new ASN1StringType(
037: TAG_BMPSTRING) {
038: };
039:
040: public static final ASN1StringType IA5STRING = new ASN1StringType(
041: TAG_IA5STRING) {
042: };
043:
044: public static final ASN1StringType GENERALSTRING = new ASN1StringType(
045: TAG_GENERALSTRING) {
046: };
047:
048: public static final ASN1StringType PRINTABLESTRING = new ASN1StringType(
049: TAG_PRINTABLESTRING) {
050: };
051:
052: public static final ASN1StringType TELETEXSTRING = new ASN1StringType(
053: TAG_TELETEXSTRING) {
054: };
055:
056: public static final ASN1StringType UNIVERSALSTRING = new ASN1StringType(
057: TAG_UNIVERSALSTRING) {
058: };
059:
060: public static final ASN1StringType UTF8STRING = new ASN1StringType(
061: TAG_UTF8STRING) {
062:
063: public Object getDecodedObject(BerInputStream in)
064: throws IOException {
065: return new String(in.buffer, in.contentOffset, in.length,
066: "UTF-8"); //$NON-NLS-1$
067: }
068:
069: public void setEncodingContent(BerOutputStream out) {
070:
071: try {
072: byte[] bytes = ((String) out.content).getBytes("UTF-8"); //$NON-NLS-1$
073: out.content = bytes;
074: out.length = bytes.length;
075: } catch (UnsupportedEncodingException e) {
076: throw new RuntimeException(e.getMessage());
077: }
078: }
079: };
080:
081: public ASN1StringType(int tagNumber) {
082: super (tagNumber);
083: }
084:
085: //
086: //
087: // Decode
088: //
089: //
090:
091: /**
092: * Tests provided identifier.
093: *
094: * @param identifier -
095: * identifier to be verified
096: * @return - true if identifier correspond to primitive or constructed
097: * identifier of this ASN.1 string type, otherwise false
098: */
099: public final boolean checkTag(int identifier) {
100: return this .id == identifier || this .constrId == identifier;
101: }
102:
103: public Object decode(BerInputStream in) throws IOException {
104:
105: in.readString(this );
106:
107: if (in.isVerify) {
108: return null;
109: }
110: return getDecodedObject(in);
111: }
112:
113: /**
114: * Extracts String object from BER input stream.
115: *
116: * @param in - BER input stream
117: * @return java.land.String object
118: */
119: public Object getDecodedObject(BerInputStream in)
120: throws IOException {
121: return new String(in.buffer, in.contentOffset, in.length);
122: }
123:
124: //
125: //
126: // Encode
127: //
128: //
129:
130: public void encodeASN(BerOutputStream out) {
131: out.encodeTag(id);
132: encodeContent(out);
133: }
134:
135: public void encodeContent(BerOutputStream out) {
136: out.encodeString();
137: }
138:
139: public void setEncodingContent(BerOutputStream out) {
140:
141: byte[] bytes = ((String) out.content).getBytes();
142:
143: out.content = bytes;
144: out.length = bytes.length;
145: }
146: }
|