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    /**
045     * Helper function that dump a byte in hex form
046     *
047     * @param octet The byte to dump
048     * @return A string representation of the byte
049     */
050    public static String dumpByte( byte octet )
051    {
052        return new String( new byte[]
053            { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] } );
054    }
055
056
057    /**
058     * Helper function that dump an array of bytes in hex form
059     *
060     * @param buffer The bytes array to dump
061     * @return A string representation of the array of bytes
062     */
063    public static String dumpBytes( byte[] buffer )
064    {
065        if ( buffer == null )
066        {
067            return "";
068        }
069
070        StringBuffer sb = new StringBuffer();
071
072        for ( byte b : buffer )
073        {
074            sb.append( "0x" ).append( ( char ) ( HEX_CHAR[( b & 0x00F0 ) >> 4] ) ).append(
075                ( char ) ( HEX_CHAR[b & 0x000F] ) ).append( " " );
076        }
077
078        return sb.toString();
079    }
080
081
082    /**
083     * Return UTF-8 encoded byte[] representation of a String
084     *
085     * @param string The string to be transformed to a byte array
086     * @return The transformed byte array
087     */
088    public static byte[] getBytesUtf8( String string )
089    {
090        if ( string == null )
091        {
092            return new byte[0];
093        }
094
095        try
096        {
097            return string.getBytes( "UTF-8" );
098        }
099        catch ( UnsupportedEncodingException uee )
100        {
101            return EMPTY_BYTES;
102        }
103    }
104
105
106    /**
107     * Transform a string to an array of ASCII bytes, where the byte array will contain
108     * only values in [0, 127].
109     *
110     * @param string The byte array to transform
111     * @return The resulting string
112     */
113    public static byte[] asciiStringToByte( String string )
114    {
115        if ( ( string == null ) || ( string.length() == 0 ) )
116        {
117            return EMPTY_BYTES;
118        }
119
120        byte[] result = new byte[string.length()];
121
122        for ( int i = 0; i < result.length; i++ )
123        {
124            result[i] = ( byte ) string.charAt( i );
125        }
126
127        return result;
128    }
129}