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 * https://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 StringBuilder sb = new StringBuilder(); 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 * Helper function that dump an array of bytes in hex form 088 * 089 * @param buffer The bytes array to dump 090 * @param start The starting point in the buffer 091 * @param length The number of bytes to print 092 * @return A string representation of the array of bytes 093 */ 094 public static String dumpBytes( byte[] buffer, int start, int length ) 095 { 096 if ( buffer == null ) 097 { 098 return ""; 099 } 100 101 StringBuilder sb = new StringBuilder(); 102 103 for ( int i = start; i < start + length; i++ ) 104 { 105 byte b = buffer[i]; 106 107 sb.append( "0x" ).append( ( char ) ( HEX_CHAR[( b & 0x00F0 ) >> 4] ) ).append( 108 ( char ) ( HEX_CHAR[b & 0x000F] ) ).append( " " ); 109 } 110 111 return sb.toString(); 112 } 113 114 115 /** 116 * Return UTF-8 encoded byte[] representation of a String 117 * 118 * @param string The string to be transformed to a byte array 119 * @return The transformed byte array 120 */ 121 public static byte[] getBytesUtf8( String string ) 122 { 123 if ( string == null ) 124 { 125 return EMPTY_BYTES; 126 } 127 128 return string.getBytes( StandardCharsets.UTF_8 ); 129 } 130 131 132 /** 133 * Transform a string to an array of ASCII bytes, where the byte array will contain 134 * only values in [0, 127]. 135 * 136 * @param string The byte array to transform 137 * @return The resulting string 138 */ 139 public static byte[] asciiStringToByte( String string ) 140 { 141 if ( ( string == null ) || ( string.length() == 0 ) ) 142 { 143 return EMPTY_BYTES; 144 } 145 146 byte[] result = new byte[string.length()]; 147 148 for ( int i = 0; i < result.length; i++ ) 149 { 150 result[i] = ( byte ) string.charAt( i ); 151 } 152 153 return result; 154 } 155}