KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > unitTests > util > BitUtil


1 /*
2
3    Derby - Class org.apache.derbyTesting.unitTests.util.BitUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.unitTests.util;
23
24
25 /**
26  * This class provides utility methods for
27  * converting byte arrays to hexidecimal Strings and manipulating BIT/BIT VARYING values as a
28  * packed vector of booleans.
29  *
30  * <P> The BIT/BIT VARYING methods are modeled after
31  * some methods in the <I>java.util.BitSet</I> class.
32  * An alternative to using a SQL BIT (VARYING) column
33  * in conjunction with the methods provided herein to
34  * provide bit manipulation would be to use a serialized
35  * <I>java.util.BitSet</I> column instead.
36  * <p>
37  * This class contains the following static methods: <UL>
38  * <LI> void <B>set</B>(byte[] bytes, int position) to set a bit</LI>
39  * <LI> void <B>clear</B>(byte[] bytes, int position) to clear a bit</LI>
40  * <LI> boolean <B>get</B>(byte[] bytes, int position) to get the
41  * bit status </LI> </UL>
42  * <p>
43  * Since these methods effectively allow a SQL BIT to be
44  * considered as an array of booleans, all offsets (position
45  * parameters) are zero based. So if you want to set
46  * the first bit of a BIT type, you would use <I>
47  * set(MyBitColumn, 0) </I>.
48  * <p>
49  * Examples: <UL>
50  * <LI> SELECT BitUtil::get(bitcol, 2) FROM mytab </LI>
51  * <LI> UPDATE mytab SET bitcol = BitUtil::set(bitcol, 2) </LI>
52  * <LI> UPDATE mytab SET bitcol = BitUtil::clear(bitcol, 2) </LI> </UL>
53  *
54  */

55 public class BitUtil
56 {
57     /**
58      * Set the bit at the specified position
59      *
60      * @param bytes the byte array
61      * @param position the bit to set, starting from zero
62      *
63      * @return the byte array with the set bit
64      *
65      * @exception IndexOutOfBoundsException on bad position
66      */

67     public static byte[] set(byte[] bytes, int position)
68     {
69         if (position >= 0)
70         {
71             int bytepos = position >> 3;
72             if (bytepos < bytes.length)
73             {
74                 int bitpos = 7 - (position % 8);
75
76                 bytes[bytepos] |= (1 << bitpos);
77                 return bytes;
78             }
79         }
80         throw new IndexOutOfBoundsException JavaDoc(Integer.toString(position));
81     }
82
83     /**
84      * Clear the bit at the specified position
85      *
86      * @param bytes the byte array
87      * @param position the bit to clear, starting from zero
88      *
89      * @return the byte array with the cleared bit
90      *
91      * @exception IndexOutOfBoundsException on bad position
92      */

93     public static byte[] clear(byte[] bytes, int position)
94     {
95         if (position >= 0)
96         {
97             int bytepos = position >> 3;
98             if (bytepos < bytes.length)
99             {
100                 int bitpos = 7 - (position % 8);
101                 bytes[bytepos] &= ~(1 << bitpos);
102                 return bytes;
103             }
104         }
105         
106         throw new IndexOutOfBoundsException JavaDoc(Integer.toString(position));
107     }
108
109     /**
110      * Check to see if the specified bit is set
111      *
112      * @param bytes the byte array
113      * @param position the bit to check, starting from zero
114      *
115      * @return true/false
116      *
117      * @exception IndexOutOfBoundsException on bad position
118      */

119     public static boolean get(byte[] bytes, int position)
120     {
121         if (position >= 0)
122         {
123             int bytepos = position >> 3;
124             if (bytepos < bytes.length)
125             {
126                 int bitpos = 7 - (position % 8);
127                 return ((bytes[bytepos] & (1 << bitpos)) != 0);
128             }
129         }
130         throw new IndexOutOfBoundsException JavaDoc(Integer.toString(position));
131     }
132
133     private static char[] hex_table = {
134                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
135                 'a', 'b', 'c', 'd', 'e', 'f'
136             };
137
138     /**
139         Convert a byte array to a human-readable String for debugging purposes.
140     */

141     public static String JavaDoc hexDump(byte[] data)
142     {
143             byte byte_value;
144
145
146             StringBuffer JavaDoc str = new StringBuffer JavaDoc(data.length * 3);
147
148             str.append("Hex dump:\n");
149
150             for (int i = 0; i < data.length; i += 16)
151             {
152                 // dump the header: 00000000:
153
String JavaDoc offset = Integer.toHexString(i);
154
155                 // "0" left pad offset field so it is always 8 char's long.
156
for (int offlen = offset.length(); offlen < 8; offlen++)
157                     str.append("0");
158                 str.append(offset);
159                 str.append(":");
160
161                 // dump hex version of 16 bytes per line.
162
for (int j = 0; (j < 16) && ((i + j) < data.length); j++)
163                 {
164                     byte_value = data[i + j];
165
166                     // add spaces between every 2 bytes.
167
if ((j % 2) == 0)
168                         str.append(" ");
169
170                     // dump a single byte.
171
byte high_nibble = (byte) ((byte_value & 0xf0) >>> 4);
172                     byte low_nibble = (byte) (byte_value & 0x0f);
173
174                     str.append(hex_table[high_nibble]);
175                     str.append(hex_table[low_nibble]);
176                 }
177
178                 // dump ascii version of 16 bytes
179
str.append(" ");
180
181                 for (int j = 0; (j < 16) && ((i + j) < data.length); j++)
182                 {
183                     char char_value = (char) data[i + j];
184
185                     // RESOLVE (really want isAscii() or isPrintable())
186
if (Character.isLetterOrDigit(char_value))
187                         str.append(String.valueOf(char_value));
188                     else
189                         str.append(".");
190                 }
191                     
192                 // new line
193
str.append("\n");
194             }
195             return(str.toString());
196
197     }
198 }
199
Popular Tags