KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > dcerpc > UUID


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.dcerpc;
18
19 import org.alfresco.filesys.util.DataPacker;
20
21 /**
22  * Universal Unique Identifier Class
23  */

24 public class UUID
25 {
26
27     // UUID constants
28

29     public static final int UUID_LENGTH = 36;
30     public static final int UUID_LENGTH_BINARY = 16;
31     private static final String JavaDoc UUID_VALIDCHARS = "0123456789ABCDEFabcdef";
32
33     // UUID string
34

35     private String JavaDoc m_uuid;
36
37     // Interface version
38

39     private int m_ifVersion;
40
41     // UUID bytes
42

43     private byte[] m_uuidBytes;
44
45     /**
46      * Class constructor
47      *
48      * @param id String
49      */

50     public UUID(String JavaDoc id)
51     {
52         if (validateUUID(id))
53         {
54             m_uuid = id;
55             m_ifVersion = 1;
56         }
57     }
58
59     /**
60      * Class constructor
61      *
62      * @param id String
63      * @param ver int
64      */

65     public UUID(String JavaDoc id, int ver)
66     {
67         if (validateUUID(id))
68         {
69             m_uuid = id;
70             m_ifVersion = ver;
71         }
72     }
73
74     /**
75      * Class constructor
76      *
77      * @param buf byte[]
78      * @param off int
79      */

80     public UUID(byte[] buf, int off)
81     {
82
83         // Copy the UUID bytes and generate the UUID string
84

85         if ((off + UUID_LENGTH_BINARY) <= buf.length)
86         {
87
88             // Take a copy of the UUID bytes
89

90             m_uuidBytes = new byte[UUID_LENGTH_BINARY];
91             for (int i = 0; i < UUID_LENGTH_BINARY; i++)
92                 m_uuidBytes[i] = buf[off + i];
93
94             // Generate the string version of the UUID
95

96             m_uuid = generateUUIDString(m_uuidBytes);
97         }
98     }
99
100     /**
101      * Determine if the UUID is valid
102      *
103      * @return boolean
104      */

105     public final boolean isValid()
106     {
107         return m_uuid != null ? true : false;
108     }
109
110     /**
111      * Return the UUID string
112      *
113      * @return String
114      */

115     public final String JavaDoc getUUID()
116     {
117         return m_uuid;
118     }
119
120     /**
121      * Return the interface version
122      *
123      * @return int
124      */

125     public final int getVersion()
126     {
127         return m_ifVersion;
128     }
129
130     /**
131      * Set the interface version
132      *
133      * @param ver int
134      */

135     public final void setVersion(int ver)
136     {
137         m_ifVersion = ver;
138     }
139
140     /**
141      * Return the UUID as a byte array
142      *
143      * @return byte[]
144      */

145     public final byte[] getBytes()
146     {
147
148         // Check if the byte array has been created
149

150         if (m_uuidBytes == null)
151         {
152
153             // Allocate the byte array
154

155             m_uuidBytes = new byte[UUID_LENGTH_BINARY];
156
157             try
158             {
159
160                 // Convert the first integer and pack into the buffer
161

162                 String JavaDoc val = m_uuid.substring(0, 8);
163                 long lval = Long.parseLong(val, 16);
164                 DataPacker.putIntelInt((int) (lval & 0xFFFFFFFF), m_uuidBytes, 0);
165
166                 // Convert the second word and pack into the buffer
167

168                 val = m_uuid.substring(9, 13);
169                 int ival = Integer.parseInt(val, 16);
170                 DataPacker.putIntelShort(ival, m_uuidBytes, 4);
171
172                 // Convert the third word and pack into the buffer
173

174                 val = m_uuid.substring(14, 18);
175                 ival = Integer.parseInt(val, 16);
176                 DataPacker.putIntelShort(ival, m_uuidBytes, 6);
177
178                 // Convert the fourth word and pack into the buffer
179

180                 val = m_uuid.substring(19, 23);
181                 ival = Integer.parseInt(val, 16);
182                 DataPacker.putShort((short) (ival & 0xFFFF), m_uuidBytes, 8);
183
184                 // Convert the final block of hex pairs to bytes
185

186                 int strPos = 24;
187                 int bytPos = 10;
188
189                 for (int i = 0; i < 6; i++)
190                 {
191                     val = m_uuid.substring(strPos, strPos + 2);
192                     m_uuidBytes[bytPos++] = (byte) (Short.parseShort(val, 16) & 0xFF);
193                     strPos += 2;
194                 }
195             }
196             catch (NumberFormatException JavaDoc ex)
197             {
198                 m_uuidBytes = null;
199             }
200         }
201
202         // Return the UUID bytes
203

204         return m_uuidBytes;
205     }
206
207     /**
208      * Validate a UUID string
209      *
210      * @param idStr String
211      * @reutrn boolean
212      */

213     public static final boolean validateUUID(String JavaDoc idStr)
214     {
215
216         // Check if the UUID string is the correct length
217

218         if (idStr == null || idStr.length() != UUID_LENGTH)
219             return false;
220
221         // Check for seperators
222

223         if (idStr.charAt(8) != '-' || idStr.charAt(13) != '-' || idStr.charAt(18) != '-' || idStr.charAt(23) != '-')
224             return false;
225
226         // Check for hex digits
227

228         int i = 0;
229         for (i = 0; i < 8; i++)
230             if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1)
231                 return false;
232         for (i = 9; i < 13; i++)
233             if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1)
234                 return false;
235         for (i = 14; i < 18; i++)
236             if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1)
237                 return false;
238         for (i = 19; i < 23; i++)
239             if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1)
240                 return false;
241         for (i = 24; i < 36; i++)
242             if (UUID_VALIDCHARS.indexOf(idStr.charAt(i)) == -1)
243                 return false;
244
245         // Valid UUID string
246

247         return true;
248     }
249
250     /**
251      * Generate a UUID string from the binary representation
252      *
253      * @param buf byte[]
254      * @return String
255      */

256     public static final String JavaDoc generateUUIDString(byte[] buf)
257     {
258
259         // Build up the UUID string
260

261         StringBuffer JavaDoc str = new StringBuffer JavaDoc(UUID_LENGTH);
262
263         // Convert the first longword
264

265         int ival = DataPacker.getIntelInt(buf, 0);
266         str.append(Integer.toHexString(ival));
267         while (str.length() != 8)
268             str.insert(0, ' ');
269         str.append("-");
270
271         // Convert the second word
272

273         ival = DataPacker.getIntelShort(buf, 4) & 0xFFFF;
274         str.append(Integer.toHexString(ival));
275         while (str.length() != 13)
276             str.insert(9, '0');
277         str.append("-");
278
279         // Convert the third word
280

281         ival = DataPacker.getIntelShort(buf, 6) & 0xFFFF;
282         str.append(Integer.toHexString(ival));
283         while (str.length() != 18)
284             str.insert(14, '0');
285         str.append("-");
286
287         // Convert the remaining bytes
288

289         for (int i = 8; i < UUID_LENGTH_BINARY; i++)
290         {
291
292             // Get the current byte value and add to the string
293

294             ival = (int) (buf[i] & 0xFF);
295             if (ival < 16)
296                 str.append('0');
297             str.append(Integer.toHexString(ival));
298
299             // Add the final seperator
300

301             if (i == 9)
302                 str.append("-");
303         }
304
305         // Return the UUID string
306

307         return str.toString();
308     }
309
310     /**
311      * Compare a UUID with the current UUID
312      *
313      * @param id UUID
314      * @return boolean
315      */

316     public final boolean compareTo(UUID id)
317     {
318
319         // Compare the UUID versions
320

321         if (getVersion() != id.getVersion())
322             return false;
323
324         // Compare the UUID bytes
325

326         byte[] thisBytes = getBytes();
327         byte[] idBytes = id.getBytes();
328
329         for (int i = 0; i < UUID_LENGTH_BINARY; i++)
330             if (thisBytes[i] != idBytes[i])
331                 return false;
332         return true;
333     }
334
335     /**
336      * Write the binary UUID to the specified buffer, and optionally the UUID version
337      *
338      * @param buf byte[]
339      * @param off int
340      * @param writeVer boolean
341      * @return int
342      */

343     public final int storeUUID(byte[] buf, int off, boolean writeVer)
344     {
345
346         // Get the UUID bytes
347

348         int pos = off;
349         byte[] uuidByts = getBytes();
350         if (uuidByts == null)
351             return pos;
352
353         // Write the binary UUID to the buffer
354

355         for (int i = 0; i < UUID_LENGTH_BINARY; i++)
356             buf[pos + i] = uuidByts[i];
357         pos += UUID_LENGTH_BINARY;
358
359         // Check if version should be written to the buffer
360

361         if (writeVer)
362         {
363             DataPacker.putIntelInt(getVersion(), buf, pos);
364             pos += 4;
365         }
366
367         // Return the new buffer position
368

369         return pos;
370     }
371
372     /**
373      * Return the UUID as a string
374      *
375      * @return String
376      */

377     public String JavaDoc toString()
378     {
379         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
380
381         str.append("[");
382         str.append(m_uuid);
383         str.append(":");
384         str.append(m_ifVersion);
385         str.append("]");
386
387         return str.toString();
388     }
389
390     /***********************************************************************************************
391      * Test Code
392      *
393      * @param args String[]
394      */

395     /**
396      * public final static void main(String[] args) { System.out.println("UUID Test");
397      * System.out.println("---------"); String[] uuids = { "12345678-1234-abcd-ef00-01234567cffb",
398      * "8a885d04-1ceb-11c9-9fe8-08002b104860", "338cd001-2244-31f1-aaaa-900038001003",
399      * "367abb81-9844-35f1-ad32-98f038001003", "4b324fc8-1670-01d3-1278-5a47bf6ee188",
400      * "6bffd098-a112-3610-9833-46c3f87e345a", "12345678-1234-abcd-ef00-0123456789ac",
401      * "12345778-1234-abcd-ef00-0123456789ab", "1ff70682-0a51-30e8-076d-740be8cee98b" }; // Validate
402      * and convert the UUIDs for ( int i = 0; i < uuids.length; i++) { UUID u = new UUID(uuids[i]);
403      * if ( u.isValid()) { System.out.println("" + (i+1) + ": " + u.toString()); byte[] bytes =
404      * u.getBytes(); HexDump.Dump(bytes,bytes.length, 0); System.out.println("Convert to string: " +
405      * generateUUIDString(bytes)); } else System.out.println("Invalid UUID: " + uuids[i]); } }
406      */

407 }
408
Popular Tags