001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with 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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.asn1.util;
021
022
023 import java.io.UnsupportedEncodingException;
024
025
026 /**
027 * Little helper class.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 * @version $Rev: 664290 $, $Date: 2008-06-07 08:28:06 +0200 (Sam, 07 jui 2008) $
031 */
032 public class Asn1StringUtils
033 {
034 // ~ Static fields/initializers
035 // -----------------------------------------------------------------
036
037 /** Hex chars */
038 private static final byte[] HEX_CHAR = new byte[]
039 { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
040
041 /**
042 * The empty byte[]
043 */
044 public static final byte[] EMPTY_BYTES = new byte[]
045 {};
046
047 // ~ Methods
048 // ------------------------------------------------------------------------------------
049
050 /**
051 * Helper function that dump a byte in hex form
052 *
053 * @param octet
054 * The byte to dump
055 * @return A string representation of the byte
056 */
057 public static String dumpByte( byte octet )
058 {
059 return new String( new byte[]
060 { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] } );
061 }
062
063
064 /**
065 * Helper function that dump an array of bytes in hex form
066 *
067 * @param buffer
068 * The bytes array to dump
069 * @return A string representation of the array of bytes
070 */
071 public static String dumpBytes( byte[] buffer )
072 {
073 if ( buffer == null )
074 {
075 return "";
076 }
077
078 StringBuffer sb = new StringBuffer();
079
080 for ( byte b:buffer )
081 {
082 sb.append( "0x" ).append( ( char ) ( HEX_CHAR[( b & 0x00F0 ) >> 4] ) ).append(
083 ( char ) ( HEX_CHAR[b & 0x000F] ) ).append( " " );
084 }
085
086 return sb.toString();
087 }
088
089
090 /**
091 * Return UTF-8 encoded byte[] representation of a String
092 *
093 * @param string
094 * The string to be transformed to a byte array
095 * @return The transformed byte array
096 */
097 public static byte[] getBytesUtf8( String string )
098 {
099 if ( string == null )
100 {
101 return new byte[0];
102 }
103
104 try
105 {
106 return string.getBytes( "UTF-8" );
107 }
108 catch ( UnsupportedEncodingException uee )
109 {
110 return new byte[]
111 {};
112 }
113 }
114
115 /**
116 * Thansform a string to an array of ASCII bytes, where the byte array will contain
117 * only values in [0, 127].
118 *
119 * @param string The byte array to transform
120 * @return The resulting string
121 */
122 public static byte[] asciiStringToByte( String string )
123 {
124 if ( ( string == null ) || ( string.length() == 0 ) )
125 {
126 return EMPTY_BYTES;
127 }
128
129 byte[] result = new byte[string.length()];
130
131 for ( int i = 0; i < result.length; i++ )
132 {
133 result[i] = (byte)string.charAt( i );
134 }
135
136 return result;
137 }
138 }