1
package org.postgresql.core;
5
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 $
17
private static final Encoding DEFAULT_ENCODING = new Encoding(null);
20
* Preferred JVM encodings for backend encodings.
22
private static final Hashtable encodings = new Hashtable();
25
//Note: this list should match the set of supported server
26
// encodings found in backend/util/mb/encnames.c
27
encodings.put("SQL_ASCII", new String[] { "ASCII", "us-ascii" });
28
encodings.put("UNICODE", new String[] { "UTF-8", "UTF8" });
29
encodings.put("LATIN1", new String[] { "ISO8859_1" });
30
encodings.put("LATIN2", new String[] { "ISO8859_2" });
31
encodings.put("LATIN3", new String[] { "ISO8859_3" });
32
encodings.put("LATIN4", new String[] { "ISO8859_4" });
33
encodings.put("ISO_8859_5", new String[] { "ISO8859_5" });
34
encodings.put("ISO_8859_6", new String[] { "ISO8859_6" });
35
encodings.put("ISO_8859_7", new String[] { "ISO8859_7" });
36
encodings.put("ISO_8859_8", new String[] { "ISO8859_8" });
37
encodings.put("LATIN5", new String[] { "ISO8859_9" });
38
encodings.put("LATIN7", new String[] { "ISO8859_13" });
39
encodings.put("LATIN9", new String[] { "ISO8859_15_FDIS" });
40
encodings.put("EUC_JP", new String[] { "EUC_JP" });
41
encodings.put("EUC_CN", new String[] { "EUC_CN" });
42
encodings.put("EUC_KR", new String[] { "EUC_KR" });
43
encodings.put("EUC_TW", new String[] { "EUC_TW" });
44
encodings.put("SJIS", new String[] { "SJIS" });
45
encodings.put("BIG5", new String[] { "Big5" });
46
encodings.put("WIN1250", new String[] { "Cp1250" });
47
encodings.put("WIN", new String[] { "Cp1251" });
48
encodings.put("ALT", new String[] { "Cp866" });
49
// We prefer KOI8-U, since it is a superset of KOI8-R.
50
encodings.put("KOI8", new String[] { "KOI8_U", "KOI8_R" });
51
// If the database isn't encoding-aware then we can't have
52
// any preferred encodings.
53
encodings.put("UNKNOWN", new String[0]);
54
// The following encodings do not have a java equivalent
55
encodings.put("MULE_INTERNAL", new String[0]);
56
encodings.put("LATIN6", new String[0]);
57
encodings.put("LATIN8", new String[0]);
58
encodings.put("LATIN10", new String[0]);
61
private final String encoding;
63
private Encoding(String encoding)
65
this.encoding = encoding;
69
* Get an Encoding for from the given database encoding and
70
* the encoding passed in by the user.
72
public static Encoding getEncoding(String databaseEncoding,
73
String passedEncoding)
75
if (passedEncoding != null)
77
if (isAvailable(passedEncoding))
79
return new Encoding(passedEncoding);
83
return defaultEncoding();
88
return encodingForDatabaseEncoding(databaseEncoding);
93
* Get an Encoding matching the given database encoding.
95
private static Encoding encodingForDatabaseEncoding(String databaseEncoding)
97
// If the backend encoding is known and there is a suitable
98
// encoding in the JVM we use that. Otherwise we fall back
99
// to the default encoding of the JVM.
101
if (encodings.containsKey(databaseEncoding))
103
String[] candidates = (String[]) encodings.get(databaseEncoding);
104
for (int i = 0; i < candidates.length; i++)
106
if (isAvailable(candidates[i]))
108
return new Encoding(candidates[i]);
112
return defaultEncoding();
116
* Name of the (JVM) encoding used.
124
* Encode a string to an array of bytes.
126
public byte[] encode(String s) throws SQLException
130
if (encoding == null)
136
return s.getBytes(encoding);
139
catch (UnsupportedEncodingException e)
141
throw new PSQLException("postgresql.stream.encoding", e);
146
* Decode an array of bytes into a string.
148
public String decode(byte[] encodedString, int offset, int length) throws SQLException
152
if (encoding == null)
154
return new String(encodedString, offset, length);
158
return new String(encodedString, offset, length, encoding);
161
catch (UnsupportedEncodingException e)
163
throw new PSQLException("postgresql.stream.encoding", e);
168
* Decode an array of bytes into a string.
170
public String decode(byte[] encodedString) throws SQLException
172
return decode(encodedString, 0, encodedString.length);
176
* Get a Reader that decodes the given InputStream.
178
public Reader getDecodingReader(InputStream in) throws SQLException
182
if (encoding == null)
184
return new InputStreamReader(in);
188
return new InputStreamReader(in, encoding);
191
catch (UnsupportedEncodingException e)
193
throw new PSQLException("postgresql.res.encoding", e);
198
* Get an Encoding using the default encoding for the JVM.
200
public static Encoding defaultEncoding()
202
return DEFAULT_ENCODING;
206
* Test if an encoding is available in the JVM.
208
private static boolean isAvailable(String encodingName)
212
"DUMMY".getBytes(encodingName);
215
catch (UnsupportedEncodingException e)