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.nio.charset.StandardCharsets;
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 private Asn1StringUtils()
45 {
46 }
47
48
49
50
51
52
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
63
64
65
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
88
89
90
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
105
106
107
108
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 }