KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > poa > util > IdUtil


1 package org.jacorb.poa.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2004 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22  
23 /**
24  * This class collects some oid related basic routines.
25  *
26  * @author Reimo Tiedemann, FU Berlin
27  * @version 1.05, 21/01/00, RT
28  */

29
30 public final class IdUtil
31 {
32
33     public static byte[] concat(byte[] first, byte[] second)
34     {
35     byte[] result = new byte[first.length+second.length];
36     System.arraycopy(first, 0, result, 0, first.length);
37     System.arraycopy(second, 0, result, first.length, second.length);
38     return result;
39     
40 }
41     /**
42      * creates an id as a concatenation of the current time in msec
43      * and random_len random bytes
44      */

45
46     public static byte[] createId(int random_len)
47     {
48     byte[] time = toId(System.currentTimeMillis());
49     long range = (long) Math.pow(10, random_len)-1;
50     byte[] random = toId((long)(Math.random()*range));
51     return concat(time, random);
52     }
53
54     public static boolean equals(byte[] first, byte[] second) {
55     if (first.length != second.length) return false;
56     for (int i=0; i<first.length; i++) {
57         if (first[i] != second[i]) return false;
58     }
59     return true;
60     }
61
62     /**
63      * compares first len bytes of two byte arrays
64      */

65
66     public static boolean equals(byte[] first, byte[] second, int len)
67     {
68     if (first.length < len || second.length < len) return false;
69     for (int i=0; i<len; i++)
70     {
71         if (first[i] != second[i]) return false;
72     }
73     return true;
74     }
75
76     /**
77      * extracts len bytes from id, the first byte to be copied is at index offset
78      */

79
80     public static byte[] extract(byte[] id, int offset, int len)
81     {
82     byte[] result = new byte[len];
83     System.arraycopy(id, offset, result, 0, len);
84     return result;
85     }
86
87     /**
88      * converts the number l into a byte array
89      */

90
91     public static byte[] toId(long l)
92     {
93     /*
94       String str = "" + l;
95       return str.getBytes();
96         
97       String str = "" + l;
98     */

99     String JavaDoc str = Long.toOctalString(l);
100     if (str.length() % 2 != 0) str = "0" + str;
101     byte[] result = new byte[str.length()/2];
102     StringBuffer JavaDoc number = new StringBuffer JavaDoc("xx");
103     for (int i=0; i<result.length; i++) {
104         number.setCharAt(0, str.charAt(i*2));
105         number.setCharAt(1, str.charAt(i*2+1));
106         result[i] = new Integer JavaDoc(number.toString()).byteValue();
107     }
108     for (int i=0; i<result.length; i++) {
109         if (result[i] == org.jacorb.poa.POAConstants.OBJECT_KEY_SEP_BYTE) {
110         result[i] = (byte) 48; // char 0 , hex 30
111

112         } else if (result[i] == org.jacorb.poa.POAConstants.MASK_BYTE) {
113         result[i] = (byte) 19; // char !!, hex 13
114
}
115     }
116     return result;
117     }
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
Popular Tags