|
8 | 8 | /*
|
9 | 9 | * Converts to and from the character encoding used by the backend.
|
10 | 10 | *
|
11 |
| - * $Id: Encoding.java,v 1.8 2002/11/14 05:35:45 barry Exp $ |
| 11 | + * $Id: Encoding.java,v 1.9 2003/02/09 23:14:55 barry Exp $ |
12 | 12 | */
|
13 | 13 |
|
14 | 14 | public class Encoding
|
@@ -235,36 +235,40 @@ private static boolean isAvailable(String encodingName)
|
235 | 235 | private static final int pow2_12 = 4096; // 212
|
236 | 236 | private char[] cdata = new char[50];
|
237 | 237 |
|
238 |
| - private synchronized String decodeUTF8(byte data[], int offset, int length) { |
239 |
| - char[] l_cdata = cdata; |
240 |
| - if (l_cdata.length < (length)) { |
241 |
| - l_cdata = new char[length]; |
242 |
| - } |
243 |
| - int i = offset; |
244 |
| - int j = 0; |
245 |
| - int k = length + offset; |
246 |
| - int z, y, x, val; |
247 |
| - while (i < k) { |
248 |
| - z = data[i] & 0xFF; |
249 |
| - if (z < 0x80) { |
250 |
| - l_cdata[j++] = (char)data[i]; |
251 |
| - i++; |
252 |
| - } else if (z >= 0xE0) { // length == 3 |
253 |
| - y = data[i+1] & 0xFF; |
254 |
| - x = data[i+2] & 0xFF; |
255 |
| - val = (z-0xE0)*pow2_12 + (y-0x80)*pow2_6 + (x-0x80); |
256 |
| - l_cdata[j++] = (char) val; |
257 |
| - i+= 3; |
258 |
| - } else { // length == 2 (maybe add checking for length > 3, throw exception if it is |
259 |
| - y = data[i+1] & 0xFF; |
260 |
| - val = (z - 0xC0)* (pow2_6)+(y-0x80); |
261 |
| - l_cdata[j++] = (char) val; |
262 |
| - i+=2; |
263 |
| - } |
264 |
| - } |
| 238 | + private synchronized String decodeUTF8(byte data[], int offset, int length) throws SQLException { |
| 239 | + try { |
| 240 | + char[] l_cdata = cdata; |
| 241 | + if (l_cdata.length < (length)) { |
| 242 | + l_cdata = new char[length]; |
| 243 | + } |
| 244 | + int i = offset; |
| 245 | + int j = 0; |
| 246 | + int k = length + offset; |
| 247 | + int z, y, x, val; |
| 248 | + while (i < k) { |
| 249 | + z = data[i] & 0xFF; |
| 250 | + if (z < 0x80) { |
| 251 | + l_cdata[j++] = (char)data[i]; |
| 252 | + i++; |
| 253 | + } else if (z >= 0xE0) { // length == 3 |
| 254 | + y = data[i+1] & 0xFF; |
| 255 | + x = data[i+2] & 0xFF; |
| 256 | + val = (z-0xE0)*pow2_12 + (y-0x80)*pow2_6 + (x-0x80); |
| 257 | + l_cdata[j++] = (char) val; |
| 258 | + i+= 3; |
| 259 | + } else { // length == 2 (maybe add checking for length > 3, throw exception if it is |
| 260 | + y = data[i+1] & 0xFF; |
| 261 | + val = (z - 0xC0)* (pow2_6)+(y-0x80); |
| 262 | + l_cdata[j++] = (char) val; |
| 263 | + i+=2; |
| 264 | + } |
| 265 | + } |
265 | 266 |
|
266 |
| - String s = new String(l_cdata, 0, j); |
267 |
| - return s; |
| 267 | + String s = new String(l_cdata, 0, j); |
| 268 | + return s; |
| 269 | + } catch (Exception l_e) { |
| 270 | + throw new PSQLException("postgresql.con.invalidchar", l_e); |
| 271 | + } |
268 | 272 | }
|
269 | 273 |
|
270 | 274 | }
|
0 commit comments