KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > dcerpc > UUID


1 /* jcifs msrpc client library in Java
2  * Copyright (C) 2006 "Michael B. Allen" <jcifs at samba dot org>
3  * "Eric Glass" <jcifs at samba dot org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package jcifs.dcerpc;
21
22 import jcifs.util.*;
23
24 public class UUID extends rpc.uuid_t {
25
26     public static int hex_to_bin(char[] arr, int offset, int length) {
27         int value = 0;
28         int ai, count;
29
30         count = 0;
31         for (ai = offset; ai < arr.length && count < length; ai++) {
32             value <<= 4;
33             switch (arr[ai]) {
34                 case '0': case '1': case '2': case '3': case '4':
35                 case '5': case '6': case '7': case '8': case '9':
36                     value += arr[ai] - '0';
37                     break;
38                 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
39                     value += 10 + (arr[ai] - 'A');
40                     break;
41                 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
42                     value += 10 + (arr[ai] - 'a');
43                     break;
44                 default:
45                     throw new IllegalArgumentException JavaDoc(new String JavaDoc(arr, offset, length));
46             }
47             count++;
48         }
49
50         return value;
51     }
52     static final char[] HEXCHARS = {
53         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
54     };
55     public static String JavaDoc bin_to_hex(int value, int length) {
56         char[] arr = new char[length];
57         int ai = arr.length;
58         while (ai-- > 0) {
59             arr[ai] = HEXCHARS[value & 0xF];
60             value >>>= 4;
61         }
62         return new String JavaDoc(arr);
63     }
64     private static byte B(int i) { return (byte)(i & 0xFF); }
65     private static short S(int i) { return (short)(i & 0xFFFF); }
66
67     public UUID(String JavaDoc str) {
68         char[] arr = str.toCharArray();
69         time_low = hex_to_bin(arr, 0, 8);
70         time_mid = S(hex_to_bin(arr, 9, 4));
71         time_hi_and_version = S(hex_to_bin(arr, 14, 4));
72         clock_seq_hi_and_reserved = B(hex_to_bin(arr, 19, 2));
73         clock_seq_low = B(hex_to_bin(arr, 21, 2));
74         node = new byte[6];
75         node[0] = B(hex_to_bin(arr, 24, 2));
76         node[1] = B(hex_to_bin(arr, 26, 2));
77         node[2] = B(hex_to_bin(arr, 28, 2));
78         node[3] = B(hex_to_bin(arr, 30, 2));
79         node[4] = B(hex_to_bin(arr, 32, 2));
80         node[5] = B(hex_to_bin(arr, 34, 2));
81     }
82
83     public String JavaDoc toString() {
84         return bin_to_hex(time_low, 8) + '-' +
85                 bin_to_hex(time_mid, 4) + '-' +
86                 bin_to_hex(time_hi_and_version, 4) + '-' +
87                 bin_to_hex(clock_seq_hi_and_reserved, 2) +
88                 bin_to_hex(clock_seq_low, 2) + '-' +
89                 bin_to_hex(node[0], 2) +
90                 bin_to_hex(node[1], 2) +
91                 bin_to_hex(node[2], 2) +
92                 bin_to_hex(node[3], 2) +
93                 bin_to_hex(node[4], 2) +
94                 bin_to_hex(node[5], 2);
95     }
96 }
97
Popular Tags