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 */
020package org.apache.directory.api.asn1.util;
021
022
023import java.io.UnsupportedEncodingException;
024
025
026/**
027 * Little helper class for the asn1 package.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public final class Asn1StringUtils
032{
033    /** Hex chars */
034    private static final byte[] HEX_CHAR = new byte[]
035        { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
036
037    /**
038     * The empty byte[]
039     */
040    public static final byte[] EMPTY_BYTES = new byte[]
041        {};
042
043
044    private Asn1StringUtils()
045    {
046    }
047
048    /**
049     * Helper function that dump a byte in hex form
050     *
051     * @param octet The byte to dump
052     * @return A string representation of the byte
053     */
054    public static String dumpByte( byte octet )
055    {
056        try
057        {
058            return new String( new byte[]
059                { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] }, "UTF-8" );
060        }
061        catch ( UnsupportedEncodingException e )
062        {
063            // Can't happen
064            return "";
065        }
066    }
067
068
069    /**
070     * Helper function that dump an array of bytes in hex form
071     *
072     * @param buffer The bytes array to dump
073     * @return A string representation of the array of bytes
074     */
075    public static String dumpBytes( byte[] buffer )
076    {
077        if ( buffer == null )
078        {
079            return "";
080        }
081
082        StringBuffer sb = new StringBuffer();
083
084        for ( byte b : buffer )
085        {
086            sb.append( "0x" ).append( ( char ) ( HEX_CHAR[( b & 0x00F0 ) >> 4] ) ).append(
087                ( char ) ( HEX_CHAR[b & 0x000F] ) ).append( " " );
088        }
089
090        return sb.toString();
091    }
092
093
094    /**
095     * Return UTF-8 encoded byte[] representation of a String
096     *
097     * @param string The string to be transformed to a byte array
098     * @return The transformed byte array
099     */
100    public static byte[] getBytesUtf8( String string )
101    {
102        if ( string == null )
103        {
104            return new byte[0];
105        }
106
107        try
108        {
109            return string.getBytes( "UTF-8" );
110        }
111        catch ( UnsupportedEncodingException uee )
112        {
113            return EMPTY_BYTES;
114        }
115    }
116
117
118    /**
119     * Transform a string to an array of ASCII bytes, where the byte array will contain
120     * only values in [0, 127].
121     *
122     * @param string The byte array to transform
123     * @return The resulting string
124     */
125    public static byte[] asciiStringToByte( String string )
126    {
127        if ( ( string == null ) || ( string.length() == 0 ) )
128        {
129            return EMPTY_BYTES;
130        }
131
132        byte[] result = new byte[string.length()];
133
134        for ( int i = 0; i < result.length; i++ )
135        {
136            result[i] = ( byte ) string.charAt( i );
137        }
138
139        return result;
140    }
141}