Skip to content

Commit 66bb7ae

Browse files
authored
Merge pull request #26 from marschall/Charset-throughout
Use Charset throughout
2 parents c680c40 + 108be45 commit 66bb7ae

File tree

9 files changed

+70
-171
lines changed

9 files changed

+70
-171
lines changed

src/main/java/com/microsoft/sqlserver/jdbc/DDC.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ static final Object convertBytesToObject(byte[] bytesValue, JDBCType jdbcType, T
468468
*/
469469
static final Object convertStringToObject(
470470
String stringVal,
471-
String charset,
471+
Charset charset,
472472
JDBCType jdbcType,
473473
StreamType streamType) throws UnsupportedEncodingException, IllegalArgumentException
474474
{

src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.*;
2222
import java.nio.*;
2323
import java.nio.channels.*;
24+
import java.nio.charset.Charset;
2425
import java.net.*;
2526
import java.math.*;
2627
import java.util.concurrent.*;
@@ -3946,7 +3947,7 @@ void writeNonUnicodeReader(
39463947
Reader reader,
39473948
long advertisedLength,
39483949
boolean isDestBinary,
3949-
String charSet) throws SQLServerException
3950+
Charset charSet) throws SQLServerException
39503951
{
39513952
assert DataTypes.UNKNOWN_STREAM_LENGTH == advertisedLength || advertisedLength >= 0;
39523953

@@ -3997,25 +3998,16 @@ void writeNonUnicodeReader(
39973998

39983999
for (int charsCopied = 0; charsCopied < charsToWrite; ++charsCopied)
39994000
{
4000-
try
4001+
if(null == charSet)
40014002
{
4002-
if(null == charSet)
4003-
{
4004-
streamByteBuffer[charsCopied] = (byte)(streamCharBuffer[charsCopied] & 0xFF);
4005-
}
4006-
else
4007-
{
4008-
// encoding as per collation
4009-
streamByteBuffer[charsCopied] = new String(streamCharBuffer[charsCopied] +
4010-
"")
4011-
.getBytes(charSet)[0];
4012-
}
4003+
streamByteBuffer[charsCopied] = (byte)(streamCharBuffer[charsCopied] & 0xFF);
40134004
}
4014-
catch (UnsupportedEncodingException e)
4005+
else
40154006
{
4016-
throw new SQLServerException(
4017-
SQLServerException.getErrString("R_encodingErrorWritingTDS"),
4018-
e);
4007+
// encoding as per collation
4008+
streamByteBuffer[charsCopied] = new String(streamCharBuffer[charsCopied] +
4009+
"")
4010+
.getBytes(charSet)[0];
40194011
}
40204012
}
40214013
writeBytes(streamByteBuffer, 0, charsToWrite);
@@ -7257,7 +7249,7 @@ final Object readGUID(int valueLength, JDBCType jdbcType, StreamType streamType)
72577249

72587250
try
72597251
{
7260-
return DDC.convertStringToObject(sb.toString(), Encoding.UNICODE.charsetName(), jdbcType, streamType);
7252+
return DDC.convertStringToObject(sb.toString(), Encoding.UNICODE.charset(), jdbcType, streamType);
72617253
}
72627254
catch (UnsupportedEncodingException e)
72637255
{

src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,14 @@ class ReaderInputStream extends InputStream
6666
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
6767
private ByteBuffer encodedChars = EMPTY_BUFFER;
6868

69-
ReaderInputStream(Reader reader, String charsetName, long readerLength) throws UnsupportedEncodingException
69+
ReaderInputStream(Reader reader, Charset charset, long readerLength)
7070
{
7171
assert reader != null;
72-
assert charsetName != null;
72+
assert charset != null;
7373
assert DataTypes.UNKNOWN_STREAM_LENGTH == readerLength || readerLength >= 0;
7474

7575
this.reader = reader;
76-
try
77-
{
78-
this.charset = Charset.forName(charsetName);
79-
}
80-
catch (IllegalCharsetNameException e)
81-
{
82-
throw new UnsupportedEncodingException(e.getMessage());
83-
}
84-
catch (UnsupportedCharsetException e)
85-
{
86-
throw new UnsupportedEncodingException(e.getMessage());
87-
}
88-
76+
this.charset = charset;
8977
this.readerLength = readerLength;
9078
}
9179

src/main/java/com/microsoft/sqlserver/jdbc/SQLCollation.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package com.microsoft.sqlserver.jdbc;
2121

2222
import java.io.*;
23+
import java.nio.charset.Charset;
2324
import java.util.*;
2425
import java.text.MessageFormat;
2526

@@ -49,7 +50,7 @@ final class SQLCollation implements java.io.Serializable
4950
private final Encoding encoding;
5051

5152
// Utility methods for getting details of this collation's encoding
52-
final String getCharset() { return encoding.charsetName(); }
53+
final Charset getCharset() throws SQLServerException { return encoding.charset(); }
5354
final boolean supportsAsciiConversion() { return encoding.supportsAsciiConversion(); }
5455
final boolean hasAsciiCompatibleSBCS() { return encoding.hasAsciiCompatibleSBCS(); }
5556

@@ -541,7 +542,7 @@ private Encoding encodingFromSortId() throws UnsupportedEncodingException
541542
/**
542543
* Enumeration of encodings that are supported by SQL Server (and hopefully the JVM).
543544
*
544-
* See, for example, http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
545+
* See, for example, https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html
545546
* for a complete list of supported encodings with their canonical names.
546547
*/
547548
enum Encoding
@@ -568,6 +569,7 @@ enum Encoding
568569
private final boolean supportsAsciiConversion;
569570
private final boolean hasAsciiCompatibleSBCS;
570571
private boolean jvmSupportConfirmed = false;
572+
private Charset charset;
571573

572574
private Encoding(
573575
String charsetName,
@@ -585,11 +587,7 @@ final Encoding checkSupported() throws UnsupportedEncodingException
585587
{
586588
// Checks for support by converting a java.lang.String
587589
// This works for all of the code pages above in SE 5 and later.
588-
try
589-
{
590-
" ".getBytes(charsetName);
591-
}
592-
catch (UnsupportedEncodingException e)
590+
if (!Charset.isSupported(charsetName))
593591
{
594592
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_codePageNotSupported"));
595593
Object[] msgArgs = {charsetName};
@@ -602,7 +600,23 @@ final Encoding checkSupported() throws UnsupportedEncodingException
602600
return this;
603601
}
604602

605-
final String charsetName() { return charsetName; }
603+
final Charset charset() throws SQLServerException
604+
{
605+
try
606+
{
607+
checkSupported();
608+
if (charset == null)
609+
{
610+
charset = Charset.forName(charsetName);
611+
}
612+
} catch (UnsupportedEncodingException e)
613+
{
614+
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_codePageNotSupported"));
615+
Object[] msgArgs = {charsetName};
616+
throw new SQLServerException(form.format(msgArgs), e);
617+
}
618+
return charset;
619+
}
606620

607621
/**
608622
* Returns true if the collation supports conversion to ascii.

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.io.InputStream;
2525
import java.io.Reader;
2626
import java.io.StringReader;
27-
import java.io.UnsupportedEncodingException;
2827
import java.math.BigDecimal;
2928
import java.nio.ByteBuffer;
3029
import java.nio.ByteOrder;
@@ -2552,31 +2551,22 @@ else if (null != sourceBulkRecord)
25522551
}
25532552
else
25542553
{
2555-
try
2554+
tdsWriter.writeShort((short) (colValueStr.length()));
2555+
// converting string into destination collation using Charset
2556+
2557+
SQLCollation destCollation = destColumnMetadata
2558+
.get(destColOrdinal).collation;
2559+
if (null != destCollation)
25562560
{
2557-
tdsWriter.writeShort((short) (colValueStr.length()));
2558-
// converting string into destination collation using Charset
2561+
tdsWriter.writeBytes(colValueStr.getBytes(
2562+
destColumnMetadata.get(destColOrdinal).collation
2563+
.getCharset()));
25592564

2560-
SQLCollation destCollation = destColumnMetadata
2561-
.get(destColOrdinal).collation;
2562-
if (null != destCollation)
2563-
{
2564-
tdsWriter.writeBytes(colValueStr.getBytes(
2565-
destColumnMetadata.get(destColOrdinal).collation
2566-
.getCharset()));
2567-
2568-
}
2569-
else
2570-
{
2571-
tdsWriter.writeBytes(colValueStr.getBytes());
2572-
}
25732565
}
2574-
catch (UnsupportedEncodingException e)
2575-
{
2576-
throw new SQLServerException(
2577-
SQLServerException.getErrString("R_encodingErrorWritingTDS"),
2578-
e);
2579-
}
2566+
else
2567+
{
2568+
tdsWriter.writeBytes(colValueStr.getBytes());
2569+
}
25802570
}
25812571
}
25822572
}

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerClob.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.*;
2525
import java.util.logging.*;
2626

27+
import static java.nio.charset.StandardCharsets.US_ASCII;
28+
2729
/**
2830
* SQLServerClob represents a character LOB object and implements java.sql.Clob.
2931
*/
@@ -186,16 +188,8 @@ public InputStream getAsciiStream() throws SQLException
186188
if (null != sqlCollation && !sqlCollation.supportsAsciiConversion())
187189
DataTypes.throwConversionError(getDisplayClassName(), "AsciiStream");
188190

189-
InputStream getterStream;
190-
try
191-
{
192-
// Need to use a BufferedInputStream since the stream returned by this method is assumed to support mark/reset
193-
getterStream = new BufferedInputStream(new ReaderInputStream(new StringReader(value), "US-ASCII", value.length()));
194-
}
195-
catch (UnsupportedEncodingException unsupportedEncodingException)
196-
{
197-
throw new SQLServerException(unsupportedEncodingException.getMessage(), null, 0, unsupportedEncodingException);
198-
}
191+
// Need to use a BufferedInputStream since the stream returned by this method is assumed to support mark/reset
192+
InputStream getterStream = new BufferedInputStream(new ReaderInputStream(new StringReader(value), US_ASCII, value.length()));
199193

200194
activeStreams.add(getterStream);
201195
return getterStream;

src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSQLXML.java

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ InputStream getValue() throws SQLServerException
101101
ByteArrayOutputStreamToInputStream strm = new ByteArrayOutputStreamToInputStream();
102102
// Need to beat a stream out of docValue
103103
TransformerFactory factory;
104-
Writer wr=null;
105104
try
106105
{
107106
factory = TransformerFactory.newInstance();
@@ -126,14 +125,7 @@ InputStream getValue() throws SQLServerException
126125
assert null ==outputStreamValue;
127126
assert null == docValue;
128127
assert null !=strValue;
129-
try
130-
{
131-
o = new ByteArrayInputStream(strValue.getBytes(Encoding.UNICODE.charsetName()));
132-
}
133-
catch (UnsupportedEncodingException ex)
134-
{
135-
throw new SQLServerException(null, ex.getMessage(), null, 0, true);
136-
}
128+
o = new ByteArrayInputStream(strValue.getBytes(Encoding.UNICODE.charset()));
137129
}
138130
assert null != o;
139131
isFreed = true; // we have consumed the data
@@ -252,16 +244,7 @@ public java.io.Writer setCharacterStream() throws SQLException
252244
checkWriteXML();
253245
isUsed = true;
254246
outputStreamValue = new ByteArrayOutputStreamToInputStream();
255-
java.io.Writer wrt=null;
256-
try
257-
{
258-
wrt = new OutputStreamWriter(outputStreamValue, Encoding.UNICODE.charsetName());
259-
}
260-
catch (UnsupportedEncodingException ex)
261-
{
262-
throw new SQLServerException(null, ex.getMessage(), null, 0, true);
263-
}
264-
return wrt;
247+
return new OutputStreamWriter(outputStreamValue, Encoding.UNICODE.charset());
265248
}
266249
public Reader getCharacterStream() throws SQLException
267250
{
@@ -309,16 +292,7 @@ public String getString() throws SQLException
309292
}
310293

311294
byte byteContents[] = contents.getBytes();
312-
String ret = null;
313-
try
314-
{
315-
ret = new String(byteContents,0, byteContents.length, Encoding.UNICODE.charsetName() );
316-
}
317-
catch (UnsupportedEncodingException ex)
318-
{
319-
throw new SQLServerException(null, ex.getMessage(), null, 0, true);
320-
}
321-
return ret;
295+
return new String(byteContents,0, byteContents.length, Encoding.UNICODE.charset() );
322296
}
323297
public void setString(String value) throws SQLException
324298
{

src/main/java/com/microsoft/sqlserver/jdbc/Util.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,7 @@ static String readUnicodeString(byte [] b, int offset, int byteLength, SQLServer
619619
{
620620
try
621621
{
622-
return new String(b, offset, byteLength, Encoding.UNICODE.charsetName());
623-
}
624-
catch (UnsupportedEncodingException ex)
625-
{
626-
String txtMsg = SQLServerException.checkAndAppendClientConnId(SQLServerException.getErrString("R_stringReadError"), conn);
627-
MessageFormat form = new MessageFormat(txtMsg);
628-
Object[] msgArgs = {new Integer(offset)};
629-
// Re-throw SQLServerException if conversion fails.
630-
throw new SQLServerException(null, form.format(msgArgs), null, 0, true);
622+
return new String(b, offset, byteLength, Encoding.UNICODE.charset());
631623
}
632624
catch (IndexOutOfBoundsException ex)
633625
{

0 commit comments

Comments
 (0)