1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.asn1.util;
21
22
23 import java.io.UnsupportedEncodingException;
24
25
26
27
28
29
30
31 public final class Asn1StringUtils
32 {
33
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
39
40 public static final byte[] EMPTY_BYTES = new byte[]
41 {};
42
43
44
45
46
47
48
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
59
60
61
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
84
85
86
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
108
109
110
111
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 }