KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > Utils


1 /*
2
3    Derby - Class org.apache.derby.client.am.Utils
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.derby.client.am;
23
24 import java.sql.SQLException JavaDoc;
25 import org.apache.derby.iapi.types.SQLBit;
26 import org.apache.derby.shared.common.i18n.MessageUtil;
27 import org.apache.derby.shared.common.reference.MessageId;
28
29 // Self-contained utilities.
30
// Don't reference any other driver classes, except Configuration, from within this class.
31

32 public final class Utils {
33
34     static String JavaDoc getStringFromBytes(byte[] bytes) {
35         if (bytes == null) {
36             return "{}";
37         }
38         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc(bytes.length * 6 + 4);
39         stringBuffer.append("{ ");
40         for (int i = 0; i < bytes.length; i++) {
41             stringBuffer.append("0x");
42             stringBuffer.append(Integer.toHexString(bytes[i] & 0xff));
43             if (i != bytes.length - 1) {
44                 stringBuffer.append(", ");
45             }
46         }
47         stringBuffer.append(" }");
48         return stringBuffer.toString();
49     }
50
51     static String JavaDoc getStringFromInts(int[] ints) {
52         if (ints == null) {
53             return "{}";
54         }
55         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
56         stringBuffer.append("{ ");
57         for (int i = 0; i < ints.length; i++) {
58             stringBuffer.append(String.valueOf(ints[i]));
59             if (i != ints.length - 1) {
60                 stringBuffer.append(", ");
61             }
62         }
63         stringBuffer.append(" }");
64         return stringBuffer.toString();
65     }
66
67     static String JavaDoc getStringFromStrings(String JavaDoc[] strings) {
68         if (strings == null) {
69             return "{}";
70         }
71         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
72         stringBuffer.append("{ ");
73         for (int i = 0; i < strings.length; i++) {
74             stringBuffer.append(strings[i]);
75             if (i != strings.length - 1) {
76                 stringBuffer.append(", ");
77             }
78         }
79         stringBuffer.append(" }");
80         return stringBuffer.toString();
81     }
82
83     static public int computeBigDecimalPrecision(java.math.BigDecimal JavaDoc decimal) {
84         byte[] bBytes = decimal.unscaledValue().abs().toByteArray();
85
86         if (byteArrayCmp(bBytes, tenRadixArr[tenRadixArr.length - 1]) >= 0) {
87             throw new java.lang.IllegalArgumentException JavaDoc(
88                 SqlException.getMessageUtil().
89                     getTextMessage(MessageId.CONN_PRECISION_TOO_LARGE));
90         }
91
92         int lo = 0, hi = tenRadixArr.length - 1, mi = (hi + lo) / 2;
93         do {
94             int compare = byteArrayCmp(bBytes, tenRadixArr[mi]);
95             if (compare == 1) {
96                 lo = mi;
97             } else if (compare == -1) {
98                 hi = mi;
99             } else {
100                 break;
101             }
102
103             mi = (hi + lo) / 2;
104         } while (mi != lo);
105
106         return (mi + 1);
107     }
108
109     // Used only by computeBigDecimalPrecision()
110
private static int byteArrayCmp(byte[] arg1, byte[] arg2) {
111         int arg1Offset = 0;
112         int arg2Offset = 0;
113         int length;
114         if (arg1.length > arg2.length) {
115             int diff = arg1.length - arg2.length;
116             for (; arg1Offset < diff; arg1Offset++) {
117                 if (arg1[arg1Offset] != 0) {
118                     return 1;
119                 }
120             }
121             length = arg2.length;
122         } else if (arg1.length < arg2.length) {
123             int diff = arg2.length - arg1.length;
124             for (; arg2Offset < diff; arg2Offset++) {
125                 if (arg2[arg2Offset] != 0) {
126                     return -1;
127                 }
128             }
129             length = arg1.length;
130         } else {
131             length = arg1.length;
132         }
133
134         for (int i = 0; i < length; i++) {
135             int b1 = arg1[arg1Offset + i] & 0xFF;
136             int b2 = arg2[arg2Offset + i] & 0xFF;
137             if (b1 > b2) {
138                 return 1;
139             } else if (b1 < b2) {
140                 return -1;
141             }
142         }
143         return 0;
144     }
145
146     // Used only by computeBigDecimalPrecision()
147
// byte array of 1, 10, 100, 1000, 10000, ..., 10^31 for
148
// fast computing the length a BigDecimal.
149
private static byte[][] tenRadixArr = {
150         {(byte) 0x01}, // 10^0
151
{(byte) 0x0A}, // 10^1
152
{(byte) 0x64}, // 10^2
153
{(byte) 0x03, (byte) 0xe8}, // 10^3
154
{(byte) 0x27, (byte) 0x10}, // 10^4
155
{(byte) 0x01, (byte) 0x86, (byte) 0xa0}, // 10^5
156
{(byte) 0x0f, (byte) 0x42, (byte) 0x40}, // 10^6
157
{(byte) 0x98, (byte) 0x96, (byte) 0x80}, // 10^7
158
{(byte) 0x05, (byte) 0xf5, (byte) 0xe1, (byte) 0x00}, // 10^8
159
{(byte) 0x3b, (byte) 0x9a, (byte) 0xca, (byte) 0x00}, // 10^9
160
{(byte) 0x02, (byte) 0x54, (byte) 0x0b, (byte) 0xe4, (byte) 0x00}, // 10^10
161
{(byte) 0x17, (byte) 0x48, (byte) 0x76, (byte) 0xe8, (byte) 0x00}, // 10^11
162
{(byte) 0xe8, (byte) 0xd4, (byte) 0xa5, (byte) 0x10, (byte) 0x00}, // 10^12
163
{(byte) 0x09, (byte) 0x18, (byte) 0x4e, (byte) 0x72, (byte) 0xa0, (byte) 0x00}, // 10^13
164
{(byte) 0x5a, (byte) 0xf3, (byte) 0x10, (byte) 0x7a, (byte) 0x40, (byte) 0x00}, // 10^14
165
{(byte) 0x03, (byte) 0x8d, (byte) 0x7e, (byte) 0xa4, (byte) 0xc6, (byte) 0x80, (byte) 0x00}, // 10^15
166
{(byte) 0x23, (byte) 0x86, (byte) 0xf2, (byte) 0x6f, (byte) 0xc1, (byte) 0x00, (byte) 0x00}, // 10^16
167
{(byte) 0x01, (byte) 0x63, (byte) 0x45, (byte) 0x78, (byte) 0x5d, (byte) 0x8a, (byte) 0x00, (byte) 0x00}, // 10^17
168
{(byte) 0x0d, (byte) 0xe0, (byte) 0xb6, (byte) 0xb3, (byte) 0xa7, (byte) 0x64, (byte) 0x00, (byte) 0x00}, // 10^18
169
{(byte) 0x8a, (byte) 0xc7, (byte) 0x23, (byte) 0x04, (byte) 0x89, (byte) 0xe8, (byte) 0x00, (byte) 0x00}, // 10^19
170
{(byte) 0x05, (byte) 0x6b, (byte) 0xc7, (byte) 0x5e, (byte) 0x2d, (byte) 0x63, (byte) 0x10, (byte) 0x00, (byte) 0x00}, // 10^20
171
{(byte) 0x36, (byte) 0x35, (byte) 0xc9, (byte) 0xad, (byte) 0xc5, (byte) 0xde, (byte) 0xa0, (byte) 0x00, (byte) 0x00}, // 10^21
172
{(byte) 0x02, (byte) 0x1e, (byte) 0x19, (byte) 0xe0, (byte) 0xc9, (byte) 0xba, (byte) 0xb2, (byte) 0x40, (byte) 0x00, (byte) 0x00}, // 10^22
173
{(byte) 0x15, (byte) 0x2d, (byte) 0x02, (byte) 0xc7, (byte) 0xe1, (byte) 0x4a, (byte) 0xf6, (byte) 0x80, (byte) 0x00, (byte) 0x00}, // 10^23
174
{(byte) 0xd3, (byte) 0xc2, (byte) 0x1b, (byte) 0xce, (byte) 0xcc, (byte) 0xed, (byte) 0xa1, (byte) 0x00, (byte) 0x00, (byte) 0x00}, // 10^24
175
{(byte) 0x08, (byte) 0x45, (byte) 0x95, (byte) 0x16, (byte) 0x14, (byte) 0x01, (byte) 0x48, (byte) 0x4a, (byte) 0x00, (byte) 0x00, (byte) 0x00}, // 10^25
176
{(byte) 0x52, (byte) 0xb7, (byte) 0xd2, (byte) 0xdc, (byte) 0xc8, (byte) 0x0c, (byte) 0xd2, (byte) 0xe4, (byte) 0x00, (byte) 0x00, (byte) 0x00}, // 10^26
177
{(byte) 0x03, (byte) 0x3b, (byte) 0x2e, (byte) 0x3c, (byte) 0x9f, (byte) 0xd0, (byte) 0x80, (byte) 0x3c, (byte) 0xe8, (byte) 0x00, (byte) 0x00, (byte) 0x00}, // 10^27
178
{(byte) 0x20, (byte) 0x4f, (byte) 0xce, (byte) 0x5e, (byte) 0x3e, (byte) 0x25, (byte) 0x02, (byte) 0x61, (byte) 0x10, (byte) 0x00, (byte) 0x00, (byte) 0x00}, // 10^28
179
{(byte) 0x01, (byte) 0x43, (byte) 0x1e, (byte) 0x0f, (byte) 0xae, (byte) 0x6d, (byte) 0x72, (byte) 0x17, (byte) 0xca, (byte) 0xa0, (byte) 0x00, (byte) 0x00, (byte) 0x00}, // 10^29
180
{(byte) 0x0c, (byte) 0x9f, (byte) 0x2c, (byte) 0x9c, (byte) 0xd0, (byte) 0x46, (byte) 0x74, (byte) 0xed, (byte) 0xea, (byte) 0x40, (byte) 0x00, (byte) 0x00, (byte) 0x00}, // 10^30
181
{(byte) 0x7e, (byte) 0x37, (byte) 0xbe, (byte) 0x20, (byte) 0x22, (byte) 0xc0, (byte) 0x91, (byte) 0x4b, (byte) 0x26, (byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00} // 10^31
182
};
183
184     // If the input string is short, pad it with blanks.
185
// If the input string is long, truncate it.
186
static public String JavaDoc padOrTruncate(String JavaDoc s, int fixedLength) {
187         if (s.length() >= fixedLength) // we need to truncate
188
{
189             return s.substring(0, fixedLength);
190         } else { // we need to pad
191
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(s);
192             for (int i = 0; i < fixedLength - s.length(); i++) {
193                 buffer.append(" ");
194             }
195             return buffer.toString();
196         }
197     }
198
199     static public void checkForNegativePositiveSqlcard(Sqlca sqlca, Statement statement) throws SqlException {
200         if (sqlca != null) {
201             int sqlcode = sqlca.getSqlCode();
202             if (sqlcode < 0) {
203                 throw new SqlException(statement.agent_.logWriter_, sqlca);
204             } else {
205                 if (sqlcode > 0) {
206                     statement.accumulateWarning(new SqlWarning(statement.agent_.logWriter_, sqlca));
207                 }
208             }
209         }
210     }
211
212     static public void checkForNegativePositiveSqlcard(Sqlca sqlca, ResultSet resultSet) throws SqlException {
213         if (sqlca != null) {
214             int sqlcode = sqlca.getSqlCode();
215             if (sqlcode < 0) {
216                 throw new SqlException(resultSet.agent_.logWriter_, sqlca);
217             } else {
218                 if (sqlcode > 0) {
219                     resultSet.accumulateWarning(new SqlWarning(resultSet.agent_.logWriter_, sqlca));
220                 }
221             }
222         }
223     }
224
225     static public int getSqlcodeFromSqlca(Sqlca sqlca) {
226         if (sqlca == null) {
227             return 0;
228         }
229         return sqlca.getSqlCode();
230     }
231
232     static public int getUpdateCountFromSqlcard(Sqlca sqlca) {
233         if (sqlca == null) {
234             return 0;
235         } else {
236             return sqlca.getUpdateCount();
237         }
238     }
239
240     public static int min(int i, int j) {
241         return (i < j) ? i : j;
242     }
243
244     public static int max(int i, int j) {
245         return (i < j) ? j : i;
246     }
247
248     // latestException is assumed to be non-null, accumulatedExceptions can be null
249
public static SQLException JavaDoc accumulateSQLException(SQLException JavaDoc latestException,
250                                                       SQLException JavaDoc accumulatedExceptions) {
251         if (accumulatedExceptions == null) {
252             return latestException;
253         } else {
254             accumulatedExceptions.setNextException(latestException);
255             return accumulatedExceptions;
256         }
257     }
258
259     public static SqlException accumulateSQLException(SqlException latestException,
260                                                       SqlException accumulatedExceptions) {
261         if (accumulatedExceptions == null) {
262             return latestException;
263         } else {
264             accumulatedExceptions.setNextException(latestException);
265             return accumulatedExceptions;
266         }
267     }
268
269     // latestException is assumed to be non-null, accumulatedExceptions can be null
270
public static SqlWarning accumulateSQLWarning(SqlWarning latestException,
271                                                   SqlWarning accumulatedExceptions) {
272         latestException.setNextException(accumulatedExceptions);
273         return latestException;
274     }
275
276     // just a thought...
277
static String JavaDoc getSQLTypeName(int sqlType) {
278         switch (sqlType) {
279         case java.sql.Types.BIGINT:
280             return "BIGINT";
281         case java.sql.Types.BINARY:
282             return "BINARY";
283         case java.sql.Types.BIT:
284             return "BIT";
285         case java.sql.Types.CHAR:
286             return "CHAR";
287         case java.sql.Types.DATE:
288             return "DATE";
289         case java.sql.Types.DECIMAL:
290             return "DECIMAL";
291         case java.sql.Types.DOUBLE:
292             return "DOUBLE";
293         case java.sql.Types.REAL:
294             return "REAL";
295         case java.sql.Types.INTEGER:
296             return "INTEGER";
297         case java.sql.Types.LONGVARBINARY:
298             return "LONGVARBINARY";
299         case java.sql.Types.LONGVARCHAR:
300             return "LONGVARCHAR";
301         case java.sql.Types.NULL:
302             return "NULL";
303         case java.sql.Types.NUMERIC:
304             return "NUMERIC";
305         case java.sql.Types.OTHER:
306             return "OTHER";
307         case java.sql.Types.FLOAT:
308             return "FLOAT";
309         case java.sql.Types.SMALLINT:
310             return "SMALLINT";
311         case java.sql.Types.TIME:
312             return "TIME";
313         case java.sql.Types.TIMESTAMP:
314             return "TIMESTAMP";
315         case java.sql.Types.TINYINT:
316             return "TINYINT";
317         case java.sql.Types.VARBINARY:
318             return "VARBINARY";
319         case java.sql.Types.VARCHAR:
320             return "VARCHAR";
321         default:
322             return null;
323         }
324     }
325
326     public static boolean isSqlTypeNullable(int sqlType) {
327         return (sqlType | 0x01) == sqlType;
328     }
329
330     public static int getNonNullableSqlType(int sqlType) {
331         return sqlType & ~1;
332     }
333 }
334
Popular Tags