~ubuntu-branches/ubuntu/gutsy/libpgjava/gutsy

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/core/Encoding.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * Encoding.java
 
4
 *     Converts to and from the character encoding used by the backend.
 
5
 *
 
6
 * Copyright (c) 2003, PostgreSQL Global Development Group
 
7
 *
 
8
 * IDENTIFICATION
 
9
 *        $Header: /cvsroot/pgsql/src/interfaces/jdbc/org/postgresql/core/Attic/Encoding.java,v 1.12 2003/09/08 17:30:22 barry Exp $
 
10
 *
 
11
 *-------------------------------------------------------------------------
 
12
 */
1
13
package org.postgresql.core;
2
14
 
3
 
import java.io.*;
4
 
import java.util.*;
 
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.*;
7
 
 
8
 
/*
9
 
 * Converts to and from the character encoding used by the backend.
10
 
 *
11
 
 * $Id: Encoding.java,v 1.4 2001/11/19 22:33:37 momjian Exp $
12
 
 */
 
20
import java.util.Hashtable;
 
21
import org.postgresql.util.PSQLException;
 
22
import org.postgresql.util.PSQLState;
13
23
 
14
24
public class Encoding
15
25
{
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.
125
141
         */
126
142
        public byte[] encode(String s) throws SQLException
127
143
        {
 
144
                byte[] l_return;
128
145
                try
129
146
                {
130
147
                        if (encoding == null)
131
148
                        {
132
 
                                return s.getBytes();
 
149
                                l_return = s.getBytes();
133
150
                        }
134
151
                        else
135
152
                        {
136
 
                                return s.getBytes(encoding);
 
153
                                l_return = s.getBytes(encoding);
 
154
                        }
 
155
                        //Don't return null, return an empty byte[] instead
 
156
                        if (l_return == null) {
 
157
                                return new byte[0];
 
158
                        } else {
 
159
                                return l_return;
137
160
                        }
138
161
                }
139
162
                catch (UnsupportedEncodingException e)
140
163
                {
141
 
                        throw new PSQLException("postgresql.stream.encoding", e);
 
164
                        throw new PSQLException("postgresql.stream.encoding", PSQLState.DATA_ERROR, e);
142
165
                }
143
166
        }
144
167
 
155
178
                        }
156
179
                        else
157
180
                        {
 
181
                                if (encoding.equals("UTF-8")) {
 
182
                                        return decodeUTF8(encodedString, offset, length);
 
183
                                }
158
184
                                return new String(encodedString, offset, length, encoding);
159
185
                        }
160
186
                }
161
187
                catch (UnsupportedEncodingException e)
162
188
                {
163
 
                        throw new PSQLException("postgresql.stream.encoding", e);
 
189
                        throw new PSQLException("postgresql.stream.encoding", PSQLState.DATA_ERROR, e);
164
190
                }
165
191
        }
166
192
 
190
216
                }
191
217
                catch (UnsupportedEncodingException e)
192
218
                {
193
 
                        throw new PSQLException("postgresql.res.encoding", e);
 
219
                        throw new PSQLException("postgresql.res.encoding", PSQLState.DATA_ERROR, e);
194
220
                }
195
221
        }
196
222
 
217
243
                        return false;
218
244
                }
219
245
        }
 
246
 
 
247
        /**
 
248
         * custom byte[] -> String conversion routine, 3x-10x faster than
 
249
         * standard new String(byte[])
 
250
         */
 
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];
 
254
 
 
255
        private synchronized String decodeUTF8(byte data[], int offset, int length) throws SQLException {
 
256
                try {
 
257
                        char[] l_cdata = cdata;
 
258
                        if (l_cdata.length < (length)) {
 
259
                                l_cdata = new char[length];
 
260
                        }
 
261
                        int i = offset;
 
262
                        int j = 0;
 
263
                        int k = length + offset;
 
264
                        int z, y, x, val;
 
265
                        while (i < k) {
 
266
                                z = data[i] & 0xFF;
 
267
                                if (z < 0x80) {
 
268
                                        l_cdata[j++] = (char)data[i];
 
269
                                        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;
 
275
                                        i+= 3;
 
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;
 
280
                                        i+=2;
 
281
                                } 
 
282
                        }
 
283
        
 
284
                        String s = new String(l_cdata, 0, j);
 
285
                        return s;
 
286
                } catch (Exception l_e) {
 
287
                        throw new PSQLException("postgresql.con.invalidchar", l_e);
 
288
                }
 
289
        }
 
290
 
220
291
}