01: // jTDS JDBC Driver for Microsoft SQL Server and Sybase
02: // Copyright (C) 2004 The jTDS Project
03: //
04: // This library is free software; you can redistribute it and/or
05: // modify it under the terms of the GNU Lesser General Public
06: // License as published by the Free Software Foundation; either
07: // version 2.1 of the License, or (at your option) any later version.
08: //
09: // This library is distributed in the hope that it will be useful,
10: // but WITHOUT ANY WARRANTY; without even the implied warranty of
11: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: // Lesser General Public License for more details.
13: //
14: // You should have received a copy of the GNU Lesser General Public
15: // License along with this library; if not, write to the Free Software
16: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: //
18: package net.sourceforge.jtds.jdbc;
19:
20: /**
21: * This class encapsulates the MS SQL2000 UniqueIdentifer data type.
22: *
23: * @author Mike Hutchinson.
24: * @version $Id: UniqueIdentifier.java,v 1.4 2005/04/28 14:29:30 alin_sinpalean Exp $
25: */
26: public class UniqueIdentifier {
27: private final byte[] bytes;
28:
29: /**
30: * Construct a unique identifier with the supplied byte array.
31: */
32: public UniqueIdentifier(byte[] id) {
33: bytes = id;
34: }
35:
36: /**
37: * Retrieve the unique identifier as a byte array.
38: *
39: * @return The unique identifier as a <code>byte[]</code>.
40: */
41: public byte[] getBytes() {
42: return (byte[]) bytes.clone();
43: }
44:
45: /**
46: * Retrieve the unique identifier as a formatted string.
47: * <p>Format is NNNNNNNN-NNNN-NNNN-NNNN-NNNNNNNNNNNN.
48: *
49: * @return The uniqueidentifier as a <code>String</code>.
50: */
51: public String toString() {
52: byte[] tmp = bytes;
53:
54: if (bytes.length == 16) {
55: // Need to swap some bytes for correct text version
56: tmp = new byte[bytes.length];
57: System.arraycopy(bytes, 0, tmp, 0, bytes.length);
58: tmp[0] = bytes[3];
59: tmp[1] = bytes[2];
60: tmp[2] = bytes[1];
61: tmp[3] = bytes[0];
62: tmp[4] = bytes[5];
63: tmp[5] = bytes[4];
64: tmp[6] = bytes[7];
65: tmp[7] = bytes[6];
66: }
67:
68: byte bb[] = new byte[1];
69:
70: StringBuffer buf = new StringBuffer(36);
71:
72: for (int i = 0; i < bytes.length; i++) {
73: bb[0] = tmp[i];
74: buf.append(Support.toHex(bb));
75:
76: if (i == 3 || i == 5 || i == 7 || i == 9) {
77: buf.append('-');
78: }
79: }
80:
81: return buf.toString();
82: }
83: }
|