Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hsqldb/ByteArray.java


1   /* Copyrights and Licenses
2    *
3    * This product includes Hypersonic SQL.
4    * Originally developed by Thomas Mueller and the Hypersonic SQL Group. 
5    *
6    * Copyright (c) 1995-2000 by the Hypersonic SQL Group. All rights reserved. 
7    * Redistribution and use in source and binary forms, with or without modification, are permitted
8    * provided that the following conditions are met: 
9    *     -  Redistributions of source code must retain the above copyright notice, this list of conditions
10   *         and the following disclaimer. 
11   *     -  Redistributions in binary form must reproduce the above copyright notice, this list of
12   *         conditions and the following disclaimer in the documentation and/or other materials
13   *         provided with the distribution. 
14   *     -  All advertising materials mentioning features or use of this software must display the
15   *        following acknowledgment: "This product includes Hypersonic SQL." 
16   *     -  Products derived from this software may not be called "Hypersonic SQL" nor may
17   *        "Hypersonic SQL" appear in their names without prior written permission of the
18   *         Hypersonic SQL Group. 
19   *     -  Redistributions of any form whatsoever must retain the following acknowledgment: "This
20   *          product includes Hypersonic SQL." 
21   * This software is provided "as is" and any expressed or implied warranties, including, but
22   * not limited to, the implied warranties of merchantability and fitness for a particular purpose are
23   * disclaimed. In no event shall the Hypersonic SQL Group or its contributors be liable for any
24   * direct, indirect, incidental, special, exemplary, or consequential damages (including, but
25   * not limited to, procurement of substitute goods or services; loss of use, data, or profits;
26   * or business interruption). However caused any on any theory of liability, whether in contract,
27   * strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this
28   * software, even if advised of the possibility of such damage. 
29   * This software consists of voluntary contributions made by many individuals on behalf of the
30   * Hypersonic SQL Group.
31   *
32   *
33   * For work added by the HSQL Development Group:
34   *
35   * Copyright (c) 2001-2002, The HSQL Development Group
36   * All rights reserved.
37   *
38   * Redistribution and use in source and binary forms, with or without
39   * modification, are permitted provided that the following conditions are met:
40   *
41   * Redistributions of source code must retain the above copyright notice, this
42   * list of conditions and the following disclaimer, including earlier
43   * license statements (above) and comply with all above license conditions.
44   *
45   * Redistributions in binary form must reproduce the above copyright notice,
46   * this list of conditions and the following disclaimer in the documentation
47   * and/or other materials provided with the distribution, including earlier
48   * license statements (above) and comply with all above license conditions.
49   *
50   * Neither the name of the HSQL Development Group nor the names of its
51   * contributors may be used to endorse or promote products derived from this
52   * software without specific prior written permission.
53   *
54   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
55   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57   * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, 
58   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
59   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
60   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
61   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
62   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65   */
66  
67  
68  package org.hsqldb;
69  
70  import java.io.ByteArrayInputStream;
71  import java.io.ByteArrayOutputStream;
72  import java.io.ObjectInputStream;
73  import java.io.ObjectOutputStream;
74  import java.sql.SQLException;
75  
76  // fredt@users 20020320 - doc 1.7.0 - update
77  // fredt@users 20020825 - patch 1.7.1 - converted to static methods only
78  // BINARY objest are now represented internally as byte[] and use the static
79  // methods in this class to compare or convert the byte[] objects
80  
81  /**
82   *  This class allows HSQLDB to store binary data as an array of bytes. It
83   *  contains methods to create and access the data, perform comparisons,
84   *  etc.
85   *
86   * @version  1.7.0
87   */
88  class ByteArray {
89  
90      /**
91       * Private constructor, no instance of this is available.
92       *
93       */
94      private ByteArray() {}
95  
96      /**
97       * Converts the specified hexadecimal digit <CODE>String</CODE>
98       * to an equivalent array of bytes.
99       *
100      * @param hexString a <CODE>String</CODE> of hexadecimal digits
101      * @throws SQLException if the specified string contains non-hexadecimal digits.
102      * @return a byte array equivalent to the specified string of hexadecimal digits
103      */
104     static byte[] hexToByteArray(String hexString) throws SQLException {
105         return StringConverter.hexToByte(hexString);
106     }
107 
108     /**
109      * Compares a <CODE>byte[]</CODE> with another specified
110      * <CODE>byte[]</CODE> for order.  Returns a negative integer, zero,
111      * or a positive integer as the first object is less than, equal to, or
112      * greater than the specified second <CODE>byte[]</CODE>.<p>
113      *
114      * @param o1 the first byte[] to be compared
115      * @param o2 the second byte[] to be compared
116      * @return a negative integer, zero, or a positive integer as this object
117      * is less than, equal to, or greater than the specified object.
118      */
119     static int compareTo(byte[] o1, byte[] o2) {
120 
121         int len  = o1.length;
122         int lenb = o2.length;
123 
124         for (int i = 0; ; i++) {
125             int a = 0;
126             int b = 0;
127 
128             if (i < len) {
129                 a = ((int) o1[i]) & 0xff;
130             } else if (i >= lenb) {
131                 return 0;
132             }
133 
134             if (i < lenb) {
135                 b = ((int) o2[i]) & 0xff;
136             }
137 
138             if (a > b) {
139                 return 1;
140             }
141 
142             if (b > a) {
143                 return -1;
144             }
145         }
146     }
147 
148     /**
149      * Retrieves the serialized form of the specified <CODE>Object</CODE>
150      * as an array of bytes.
151      *
152      * @param s the Object to serialize
153      * @return  a static byte array representing the passed Object
154      * @throws SQLException if a serialization failure occurs
155      */
156     static byte[] serialize(Object s) throws SQLException {
157 
158         ByteArrayOutputStream bo = new ByteArrayOutputStream();
159 
160         try {
161             ObjectOutputStream os = new ObjectOutputStream(bo);
162 
163             os.writeObject(s);
164 
165             return bo.toByteArray();
166         } catch (Exception e) {
167             throw Trace.error(Trace.SERIALIZATION_FAILURE, e.getMessage());
168         }
169     }
170 
171     /**
172      * Retrieves the serialized form of the specified <CODE>Object</CODE>
173      * as an equivalent <CODE>String</CODE> of hexadecimal digits.
174      *
175      * @param s the Object to serialize
176      * @return  A String representing the passed Object
177      * @throws SQLException if a serialization failure occurs
178      */
179     static String serializeToString(Object s) throws SQLException {
180         return StringConverter.byteToHex(serialize(s));
181     }
182 
183     /**
184      * Deserializes the specified byte array to an
185      * <CODE>Object</CODE> instance.
186      *
187      * @return the Object resulting from deserializing the specified array of bytes
188      * @param ba the byte array to deserialize to an Object
189      * @throws SQLException if a serialization failure occurs
190      */
191     static Object deserialize(byte[] ba) throws SQLException {
192 
193         try {
194             ByteArrayInputStream bi = new ByteArrayInputStream(ba);
195             ObjectInputStream    is = new ObjectInputStream(bi);
196 
197             return is.readObject();
198         } catch (Exception e) {
199             throw Trace.error(Trace.SERIALIZATION_FAILURE, e.getMessage());
200         }
201     }
202 
203     /**
204      * Converts an array of bytes to an equivalent
205      * <CODE>String</CODE> of hexadecimal digits.
206      *
207      * @return  String representation of the byte[].
208      */
209     static public String toString(byte[] o) {
210         return StringConverter.byteToHex(o);
211     }
212 }