1
/*-------------------------------------------------------------------------
4
* Converts to and from the character encoding used by the backend.
6
* Copyright (c) 2003, PostgreSQL Global Development Group
9
* $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/Encoding.java,v 1.12 2003/09/08 17:30:22 barry Exp $
11
*-------------------------------------------------------------------------
1
13
package org.postgresql.core;
15
import java.io.InputStream;
16
import java.io.InputStreamReader;
17
import java.io.Reader;
18
import java.io.UnsupportedEncodingException;
5
19
import java.sql.SQLException;
6
import org.postgresql.util.*;
9
* Converts to and from the character encoding used by the backend.
11
* $Id: Encoding.java,v 1.4 2001/11/19 22:33:37 momjian Exp $
20
import java.util.Hashtable;
21
import org.postgresql.util.PSQLException;
22
import org.postgresql.util.PSQLState;
14
24
public class Encoding
40
50
encodings.put("EUC_JP", new String[] { "EUC_JP" });
41
51
encodings.put("EUC_CN", new String[] { "EUC_CN" });
42
52
encodings.put("EUC_KR", new String[] { "EUC_KR" });
53
encodings.put("JOHAB", new String[] { "Johab" });
43
54
encodings.put("EUC_TW", new String[] { "EUC_TW" });
44
encodings.put("SJIS", new String[] { "SJIS" });
45
encodings.put("BIG5", new String[] { "Big5" });
55
encodings.put("SJIS", new String[] { "MS932", "SJIS" });
56
encodings.put("BIG5", new String[] { "Big5", "MS950", "Cp950" });
57
encodings.put("GBK", new String[] { "GBK", "MS936" });
58
encodings.put("UHC", new String[] { "MS949", "Cp949", "Cp949C" });
59
encodings.put("TCVN", new String[] { "Cp1258" });
60
encodings.put("WIN1256", new String[] { "Cp1256" });
46
61
encodings.put("WIN1250", new String[] { "Cp1250" });
62
encodings.put("WIN874", new String[] { "MS874", "Cp874" });
47
63
encodings.put("WIN", new String[] { "Cp1251" });
48
64
encodings.put("ALT", new String[] { "Cp866" });
49
65
// We prefer KOI8-U, since it is a superset of KOI8-R.
126
142
public byte[] encode(String s) throws SQLException
130
147
if (encoding == null)
149
l_return = s.getBytes();
136
return s.getBytes(encoding);
153
l_return = s.getBytes(encoding);
155
//Don't return null, return an empty byte[] instead
156
if (l_return == null) {
139
162
catch (UnsupportedEncodingException e)
141
throw new PSQLException("postgresql.stream.encoding", e);
164
throw new PSQLException("postgresql.stream.encoding", PSQLState.DATA_ERROR, e);
181
if (encoding.equals("UTF-8")) {
182
return decodeUTF8(encodedString, offset, length);
158
184
return new String(encodedString, offset, length, encoding);
161
187
catch (UnsupportedEncodingException e)
163
throw new PSQLException("postgresql.stream.encoding", e);
189
throw new PSQLException("postgresql.stream.encoding", PSQLState.DATA_ERROR, e);
191
217
catch (UnsupportedEncodingException e)
193
throw new PSQLException("postgresql.res.encoding", e);
219
throw new PSQLException("postgresql.res.encoding", PSQLState.DATA_ERROR, e);
248
* custom byte[] -> String conversion routine, 3x-10x faster than
249
* standard new String(byte[])
251
private static final int pow2_6 = 64; // 26
252
private static final int pow2_12 = 4096; // 212
253
private char[] cdata = new char[50];
255
private synchronized String decodeUTF8(byte data[], int offset, int length) throws SQLException {
257
char[] l_cdata = cdata;
258
if (l_cdata.length < (length)) {
259
l_cdata = new char[length];
263
int k = length + offset;
268
l_cdata[j++] = (char)data[i];
270
} else if (z >= 0xE0) { // length == 3
271
y = data[i+1] & 0xFF;
272
x = data[i+2] & 0xFF;
273
val = (z-0xE0)*pow2_12 + (y-0x80)*pow2_6 + (x-0x80);
274
l_cdata[j++] = (char) val;
276
} else { // length == 2 (maybe add checking for length > 3, throw exception if it is
277
y = data[i+1] & 0xFF;
278
val = (z - 0xC0)* (pow2_6)+(y-0x80);
279
l_cdata[j++] = (char) val;
284
String s = new String(l_cdata, 0, j);
286
} catch (Exception l_e) {
287
throw new PSQLException("postgresql.con.invalidchar", l_e);