View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.api.asn1.util;
21  
22  
23  import java.nio.charset.StandardCharsets;
24  
25  
26  /**
27   * Little helper class for the asn1 package.
28   *
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   */
31  public final class Asn1StringUtils
32  {
33      /** Hex chars */
34      private static final byte[] HEX_CHAR = new byte[]
35          { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
36  
37      /**
38       * The empty byte[]
39       */
40      public static final byte[] EMPTY_BYTES = new byte[]
41          {};
42  
43  
44      private Asn1StringUtils()
45      {
46      }
47  
48      /**
49       * Helper function that dump a byte in hex form
50       *
51       * @param octet The byte to dump
52       * @return A string representation of the byte
53       */
54      public static String dumpByte( byte octet )
55      {
56          return new String( new byte[]
57              { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] }, StandardCharsets.UTF_8 );
58      }
59  
60  
61      /**
62       * Helper function that dump an array of bytes in hex form
63       *
64       * @param buffer The bytes array to dump
65       * @return A string representation of the array of bytes
66       */
67      public static String dumpBytes( byte[] buffer )
68      {
69          if ( buffer == null )
70          {
71              return "";
72          }
73  
74          StringBuilder sb = new StringBuilder();
75  
76          for ( byte b : buffer )
77          {
78              sb.append( "0x" ).append( ( char ) ( HEX_CHAR[( b & 0x00F0 ) >> 4] ) ).append(
79                  ( char ) ( HEX_CHAR[b & 0x000F] ) ).append( " " );
80          }
81  
82          return sb.toString();
83      }
84  
85  
86      /**
87       * Return UTF-8 encoded byte[] representation of a String
88       *
89       * @param string The string to be transformed to a byte array
90       * @return The transformed byte array
91       */
92      public static byte[] getBytesUtf8( String string )
93      {
94          if ( string == null )
95          {
96              return EMPTY_BYTES;
97          }
98  
99          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 }