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