SingleByteCharsetConverter.java
9.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
The MySQL Connector/J is licensed under the terms of the GPLv2
<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
this software, see the FOSS License Exception
<http://www.mysql.com/about/legal/licensing/foss-exception.html>.
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; version 2
of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this
program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
Floor, Boston, MA 02110-1301 USA
*/
package com.mysql.jdbc;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
* Converter for char[]->byte[] and byte[]->char[] for single-byte character sets.
*/
public class SingleByteCharsetConverter {
private static final int BYTE_RANGE = (1 + Byte.MAX_VALUE) - Byte.MIN_VALUE;
private static byte[] allBytes = new byte[BYTE_RANGE];
private static final Map<String, SingleByteCharsetConverter> CONVERTER_MAP = new HashMap<String, SingleByteCharsetConverter>();
private final static byte[] EMPTY_BYTE_ARRAY = new byte[0];
// The initial charToByteMap, with all char mappings mapped to (byte) '?', so that unknown characters are mapped to '?' instead of '\0' (which means
// end-of-string to MySQL).
private static byte[] unknownCharsMap = new byte[65536];
static {
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
allBytes[i - Byte.MIN_VALUE] = (byte) i;
}
for (int i = 0; i < unknownCharsMap.length; i++) {
unknownCharsMap[i] = (byte) '?'; // use something 'sane' for unknown chars
}
}
/**
* Get a converter for the given encoding name
*
* @param encodingName
* the Java character encoding name
* @param conn
*
* @return a converter for the given encoding name
* @throws UnsupportedEncodingException
* if the character encoding is not supported
*/
public static synchronized SingleByteCharsetConverter getInstance(String encodingName, Connection conn) throws UnsupportedEncodingException, SQLException {
SingleByteCharsetConverter instance = CONVERTER_MAP.get(encodingName);
if (instance == null) {
instance = initCharset(encodingName);
}
return instance;
}
/**
* Initialize the shared instance of a converter for the given character
* encoding.
*
* @param javaEncodingName
* the Java name for the character set to initialize
* @return a converter for the given character set
* @throws UnsupportedEncodingException
* if the character encoding is not supported
*/
public static SingleByteCharsetConverter initCharset(String javaEncodingName) throws UnsupportedEncodingException, SQLException {
try {
if (CharsetMapping.isMultibyteCharset(javaEncodingName)) {
return null;
}
} catch (RuntimeException ex) {
SQLException sqlEx = SQLError.createSQLException(ex.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, null);
sqlEx.initCause(ex);
throw sqlEx;
}
SingleByteCharsetConverter converter = new SingleByteCharsetConverter(javaEncodingName);
CONVERTER_MAP.put(javaEncodingName, converter);
return converter;
}
/**
* Convert the byte buffer from startPos to a length of length to a string
* using the default platform encoding.
*
* @param buffer
* the bytes to convert
* @param startPos
* the index to start at
* @param length
* the number of bytes to convert
* @return the String representation of the given bytes
*/
public static String toStringDefaultEncoding(byte[] buffer, int startPos, int length) {
return new String(buffer, startPos, length);
}
private char[] byteToChars = new char[BYTE_RANGE];
private byte[] charToByteMap = new byte[65536];
/**
* Prevent instantiation, called out of static method initCharset().
*
* @param encodingName
* a JVM character encoding
* @throws UnsupportedEncodingException
* if the JVM does not support the encoding
*/
private SingleByteCharsetConverter(String encodingName) throws UnsupportedEncodingException {
String allBytesString = new String(allBytes, 0, BYTE_RANGE, encodingName);
int allBytesLen = allBytesString.length();
System.arraycopy(unknownCharsMap, 0, this.charToByteMap, 0, this.charToByteMap.length);
for (int i = 0; i < BYTE_RANGE && i < allBytesLen; i++) {
char c = allBytesString.charAt(i);
this.byteToChars[i] = c;
this.charToByteMap[c] = allBytes[i];
}
}
public final byte[] toBytes(char[] c) {
if (c == null) {
return null;
}
int length = c.length;
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
bytes[i] = this.charToByteMap[c[i]];
}
return bytes;
}
public final byte[] toBytesWrapped(char[] c, char beginWrap, char endWrap) {
if (c == null) {
return null;
}
int length = c.length + 2;
int charLength = c.length;
byte[] bytes = new byte[length];
bytes[0] = this.charToByteMap[beginWrap];
for (int i = 0; i < charLength; i++) {
bytes[i + 1] = this.charToByteMap[c[i]];
}
bytes[length - 1] = this.charToByteMap[endWrap];
return bytes;
}
public final byte[] toBytes(char[] chars, int offset, int length) {
if (chars == null) {
return null;
}
if (length == 0) {
return EMPTY_BYTE_ARRAY;
}
byte[] bytes = new byte[length];
for (int i = 0; (i < length); i++) {
bytes[i] = this.charToByteMap[chars[i + offset]];
}
return bytes;
}
/**
* Convert the given string to an array of bytes.
*
* @param s
* the String to convert
* @return the bytes that make up the String
*/
public final byte[] toBytes(String s) {
if (s == null) {
return null;
}
int length = s.length();
byte[] bytes = new byte[length];
for (int i = 0; i < length; i++) {
bytes[i] = this.charToByteMap[s.charAt(i)];
}
return bytes;
}
public final byte[] toBytesWrapped(String s, char beginWrap, char endWrap) {
if (s == null) {
return null;
}
int stringLength = s.length();
int length = stringLength + 2;
byte[] bytes = new byte[length];
bytes[0] = this.charToByteMap[beginWrap];
for (int i = 0; i < stringLength; i++) {
bytes[i + 1] = this.charToByteMap[s.charAt(i)];
}
bytes[length - 1] = this.charToByteMap[endWrap];
return bytes;
}
/**
* Convert the given string to an array of bytes.
*
* @param s
* the String to convert
* @param offset
* the offset to start at
* @param length
* length (max) to convert
*
* @return the bytes that make up the String
*/
public final byte[] toBytes(String s, int offset, int length) {
if (s == null) {
return null;
}
if (length == 0) {
return EMPTY_BYTE_ARRAY;
}
byte[] bytes = new byte[length];
for (int i = 0; (i < length); i++) {
char c = s.charAt(i + offset);
bytes[i] = this.charToByteMap[c];
}
return bytes;
}
/**
* Convert the byte buffer to a string using this instance's character
* encoding.
*
* @param buffer
* the bytes to convert to a String
* @return the converted String
*/
public final String toString(byte[] buffer) {
return toString(buffer, 0, buffer.length);
}
/**
* Convert the byte buffer from startPos to a length of length to a string
* using this instance's character encoding.
*
* @param buffer
* the bytes to convert
* @param startPos
* the index to start at
* @param length
* the number of bytes to convert
* @return the String representation of the given bytes
*/
public final String toString(byte[] buffer, int startPos, int length) {
char[] charArray = new char[length];
int readpoint = startPos;
for (int i = 0; i < length; i++) {
charArray[i] = this.byteToChars[buffer[readpoint] - Byte.MIN_VALUE];
readpoint++;
}
return new String(charArray);
}
}