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.io.UnsupportedEncodingException;
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      /**
45       * Helper function that dump a byte in hex form
46       *
47       * @param octet The byte to dump
48       * @return A string representation of the byte
49       */
50      public static String dumpByte( byte octet )
51      {
52          return new String( new byte[]
53              { '0', 'x', HEX_CHAR[( octet & 0x00F0 ) >> 4], HEX_CHAR[octet & 0x000F] } );
54      }
55  
56  
57      /**
58       * Helper function that dump an array of bytes in hex form
59       *
60       * @param buffer The bytes array to dump
61       * @return A string representation of the array of bytes
62       */
63      public static String dumpBytes( byte[] buffer )
64      {
65          if ( buffer == null )
66          {
67              return "";
68          }
69  
70          StringBuffer sb = new StringBuffer();
71  
72          for ( byte b : buffer )
73          {
74              sb.append( "0x" ).append( ( char ) ( HEX_CHAR[( b & 0x00F0 ) >> 4] ) ).append(
75                  ( char ) ( HEX_CHAR[b & 0x000F] ) ).append( " " );
76          }
77  
78          return sb.toString();
79      }
80  
81  
82      /**
83       * Return UTF-8 encoded byte[] representation of a String
84       *
85       * @param string The string to be transformed to a byte array
86       * @return The transformed byte array
87       */
88      public static byte[] getBytesUtf8( String string )
89      {
90          if ( string == null )
91          {
92              return new byte[0];
93          }
94  
95          try
96          {
97              return string.getBytes( "UTF-8" );
98          }
99          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 }