1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.kerberos.kdc;
21
22
23 import java.io.BufferedInputStream;
24 import java.io.CharArrayWriter;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.io.Reader;
29
30
31
32
33
34
35 public class KerberosTestUtils
36 {
37 public static char[] getControlDocument( String resource ) throws IOException
38 {
39 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream( resource );
40
41 Reader reader = new InputStreamReader( new BufferedInputStream( is ) );
42
43 CharArrayWriter writer = new CharArrayWriter();
44
45 try
46 {
47 char[] buf = new char[2048];
48 int len = 0;
49 while ( len >= 0 )
50 {
51 len = reader.read( buf );
52 if ( len > 0 )
53 {
54 writer.write( buf, 0, len );
55 }
56 }
57 }
58 finally
59 {
60 try
61 {
62 reader.close();
63 }
64 catch ( IOException ioe )
65 {
66 }
67 }
68
69 char[] isca = writer.toCharArray();
70 return isca;
71 }
72
73
74 public static byte[] getBytesFromResource( String resource ) throws IOException
75 {
76 InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream( resource );
77
78 BufferedInputStream stream = new BufferedInputStream( is );
79 int len = stream.available();
80 byte[] bytes = new byte[len];
81 stream.read( bytes, 0, len );
82
83 return bytes;
84 }
85
86
87 public static void hexdump( byte[] data )
88 {
89 hexdump( data, true );
90 }
91
92
93 public static void hexdump( byte[] data, boolean delimit )
94 {
95 String delimiter = new String( "-------------------------------------------------" );
96
97 if ( delimit )
98 {
99 System.out.println( delimiter );
100 }
101
102 int lineLength = 0;
103 for ( int ii = 0; ii < data.length; ii++ )
104 {
105 System.out.print( byte2hexString( data[ii] ) + " " );
106 lineLength++;
107
108 if ( lineLength == 8 )
109 {
110 System.out.print( " " );
111 }
112
113 if ( lineLength == 16 )
114 {
115 System.out.println();
116 lineLength = 0;
117 }
118 }
119
120 if ( delimit )
121 {
122 System.out.println();
123 System.out.println( delimiter );
124 }
125 }
126
127 public static final String[] hex_digit =
128 { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };
129
130
131 public static String byte2hexString( byte x )
132 {
133 String s = "";
134 for ( int ii = 0; ii < 2; ii++ )
135 {
136 s = hex_digit[( ( ( x ) & 0xff ) & ( 15 << ( ii * 4 ) ) ) >>> ( ii * 4 )] + s;
137 }
138
139 return s;
140 }
141
142
143 public static String int2hexString( int x )
144 {
145 String s = "";
146 for ( int ii = 0; ii < 8; ii++ )
147 {
148 s = hex_digit[( x & ( 15 << ( ii * 4 ) ) ) >>> ( ii * 4 )] + s;
149 }
150
151 return s;
152 }
153
154
155 public static String int2binString( int x )
156 {
157 String s = "";
158 for ( int ii = 0; ii < 32; ii++ )
159 {
160 if ( ( ii > 0 ) && ( ii % 4 == 0 ) )
161 {
162 s = " " + s;
163 }
164
165 s = hex_digit[( x & ( 1 << ii ) ) >>> ii] + s;
166 }
167
168 return s;
169 }
170
171
172 public static String long2hexString( long x )
173 {
174 String s = "";
175 for ( int ii = 0; ii < 16; ii++ )
176 {
177 s = hex_digit[( int ) ( ( x & ( 15L << ( ii * 4 ) ) ) >>> ( ii * 4 ) )] + s;
178 }
179
180 return s;
181 }
182
183
184 public static String long2binString( long x )
185 {
186 String s = "";
187 for ( int ii = 0; ii < 64; ii++ )
188 {
189 if ( ( ii > 0 ) && ( ii % 4 == 0 ) )
190 {
191 s = " " + s;
192 }
193
194 s = hex_digit[( int ) ( ( x & ( 1L << ii ) ) >>> ii )] + s;
195 }
196
197 return s;
198 }
199
200
201 public static String byte2hexString( byte[] input )
202 {
203 return byte2hexString( input, 0, input.length );
204 }
205
206
207 public static String byte2hexString( byte[] input, int offset )
208 {
209 return byte2hexString( input, offset, input.length );
210 }
211
212
213 public static String byte2hexString( byte[] input, int offset, int length )
214 {
215 String result = "";
216 for ( int ii = 0; ii < length; ii++ )
217 {
218 if ( ii + offset < input.length )
219 {
220 result += byte2hexString( input[ii + offset] );
221 }
222 }
223
224 return result;
225 }
226 }