~ubuntu-branches/ubuntu/wily/libpgjava/wily

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Gybas
  • Date: 2002-02-06 23:43:06 UTC
  • Revision ID: james.westby@ubuntu.com-20020206234306-hsg7suqr8q56qg40
Tags: upstream-7.2
ImportĀ upstreamĀ versionĀ 7.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.postgresql.core;
 
2
 
 
3
import java.io.*;
 
4
import java.util.*;
 
5
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
 */
 
13
 
 
14
public class Encoding
 
15
{
 
16
 
 
17
        private static final Encoding DEFAULT_ENCODING = new Encoding(null);
 
18
 
 
19
        /*
 
20
         * Preferred JVM encodings for backend encodings.
 
21
         */
 
22
        private static final Hashtable encodings = new Hashtable();
 
23
 
 
24
        static {
 
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]);
 
59
        }
 
60
 
 
61
        private final String encoding;
 
62
 
 
63
        private Encoding(String encoding)
 
64
        {
 
65
                this.encoding = encoding;
 
66
        }
 
67
 
 
68
        /*
 
69
         * Get an Encoding for from the given database encoding and
 
70
         * the encoding passed in by the user.
 
71
         */
 
72
        public static Encoding getEncoding(String databaseEncoding,
 
73
                                                                           String passedEncoding)
 
74
        {
 
75
                if (passedEncoding != null)
 
76
                {
 
77
                        if (isAvailable(passedEncoding))
 
78
                        {
 
79
                                return new Encoding(passedEncoding);
 
80
                        }
 
81
                        else
 
82
                        {
 
83
                                return defaultEncoding();
 
84
                        }
 
85
                }
 
86
                else
 
87
                {
 
88
                        return encodingForDatabaseEncoding(databaseEncoding);
 
89
                }
 
90
        }
 
91
 
 
92
        /*
 
93
         * Get an Encoding matching the given database encoding.
 
94
         */
 
95
        private static Encoding encodingForDatabaseEncoding(String databaseEncoding)
 
96
        {
 
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.
 
100
 
 
101
                if (encodings.containsKey(databaseEncoding))
 
102
                {
 
103
                        String[] candidates = (String[]) encodings.get(databaseEncoding);
 
104
                        for (int i = 0; i < candidates.length; i++)
 
105
                        {
 
106
                                if (isAvailable(candidates[i]))
 
107
                                {
 
108
                                        return new Encoding(candidates[i]);
 
109
                                }
 
110
                        }
 
111
                }
 
112
                return defaultEncoding();
 
113
        }
 
114
 
 
115
        /*
 
116
         * Name of the (JVM) encoding used.
 
117
         */
 
118
        public String name()
 
119
        {
 
120
                return encoding;
 
121
        }
 
122
 
 
123
        /*
 
124
         * Encode a string to an array of bytes.
 
125
         */
 
126
        public byte[] encode(String s) throws SQLException
 
127
        {
 
128
                try
 
129
                {
 
130
                        if (encoding == null)
 
131
                        {
 
132
                                return s.getBytes();
 
133
                        }
 
134
                        else
 
135
                        {
 
136
                                return s.getBytes(encoding);
 
137
                        }
 
138
                }
 
139
                catch (UnsupportedEncodingException e)
 
140
                {
 
141
                        throw new PSQLException("postgresql.stream.encoding", e);
 
142
                }
 
143
        }
 
144
 
 
145
        /*
 
146
         * Decode an array of bytes into a string.
 
147
         */
 
148
        public String decode(byte[] encodedString, int offset, int length) throws SQLException
 
149
        {
 
150
                try
 
151
                {
 
152
                        if (encoding == null)
 
153
                        {
 
154
                                return new String(encodedString, offset, length);
 
155
                        }
 
156
                        else
 
157
                        {
 
158
                                return new String(encodedString, offset, length, encoding);
 
159
                        }
 
160
                }
 
161
                catch (UnsupportedEncodingException e)
 
162
                {
 
163
                        throw new PSQLException("postgresql.stream.encoding", e);
 
164
                }
 
165
        }
 
166
 
 
167
        /*
 
168
         * Decode an array of bytes into a string.
 
169
         */
 
170
        public String decode(byte[] encodedString) throws SQLException
 
171
        {
 
172
                return decode(encodedString, 0, encodedString.length);
 
173
        }
 
174
 
 
175
        /*
 
176
         * Get a Reader that decodes the given InputStream.
 
177
         */
 
178
        public Reader getDecodingReader(InputStream in) throws SQLException
 
179
        {
 
180
                try
 
181
                {
 
182
                        if (encoding == null)
 
183
                        {
 
184
                                return new InputStreamReader(in);
 
185
                        }
 
186
                        else
 
187
                        {
 
188
                                return new InputStreamReader(in, encoding);
 
189
                        }
 
190
                }
 
191
                catch (UnsupportedEncodingException e)
 
192
                {
 
193
                        throw new PSQLException("postgresql.res.encoding", e);
 
194
                }
 
195
        }
 
196
 
 
197
        /*
 
198
         * Get an Encoding using the default encoding for the JVM.
 
199
         */
 
200
        public static Encoding defaultEncoding()
 
201
        {
 
202
                return DEFAULT_ENCODING;
 
203
        }
 
204
 
 
205
        /*
 
206
         * Test if an encoding is available in the JVM.
 
207
         */
 
208
        private static boolean isAvailable(String encodingName)
 
209
        {
 
210
                try
 
211
                {
 
212
                        "DUMMY".getBytes(encodingName);
 
213
                        return true;
 
214
                }
 
215
                catch (UnsupportedEncodingException e)
 
216
                {
 
217
                        return false;
 
218
                }
 
219
        }
 
220
}