001 /*
002 * Copyright (c) 2000 - 2006 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this
005 * software and associated documentation files (the "Software"), to deal in the Software
006 * without restriction, including without limitation the rights to use, copy, modify,
007 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
008 * permit persons to whom the Software is furnished to do so, subject to the following
009 * conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in all copies
012 * or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
015 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
016 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
017 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
018 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
019 * DEALINGS IN THE SOFTWARE.
020 *
021 */
022
023 package org.apache.directory.shared.asn1.der;
024
025
026 import java.io.UnsupportedEncodingException;
027
028
029 /**
030 * Interface for DER string objects.
031 */
032 public abstract class DERString extends DERObject
033 {
034 /**
035 * Basic DERObject constructor.
036 */
037 DERString(int tag, byte[] value)
038 {
039 super( tag, value );
040 }
041
042
043 /**
044 * Lazy accessor.
045 *
046 * @return underlying byte array converted to a String
047 */
048 public String getString()
049 {
050 return byteArrayToString( value );
051 }
052
053
054 /**
055 * Utility method for converting byte arrays to Strings.
056 *
057 * @param bytes
058 * @return String
059 */
060 protected static String byteArrayToString( byte[] bytes )
061 {
062 try
063 {
064 return new String( bytes, "UTF-8" );
065 }
066 catch ( UnsupportedEncodingException uee )
067 {
068 return "";
069 }
070 }
071
072
073 /**
074 * Utility method for converting Strings to bytes.
075 *
076 * @param string
077 * @return bytes
078 */
079 protected static byte[] stringToByteArray( String string )
080 {
081 try
082 {
083 return string.getBytes( "UTF-8" );
084 }
085 catch ( UnsupportedEncodingException uee )
086 {
087 return new byte[]
088 {};
089 }
090 }
091 }