Skip to content

Commit

Permalink
Merge pull request #26 from marschall/Charset-throughout
Browse files Browse the repository at this point in the history
Use Charset throughout
  • Loading branch information
xiangyushawn authored Nov 29, 2016
2 parents c680c40 + 108be45 commit 66bb7ae
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 171 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/microsoft/sqlserver/jdbc/DDC.java
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ static final Object convertBytesToObject(byte[] bytesValue, JDBCType jdbcType, T
*/
static final Object convertStringToObject(
String stringVal,
String charset,
Charset charset,
JDBCType jdbcType,
StreamType streamType) throws UnsupportedEncodingException, IllegalArgumentException
{
Expand Down
28 changes: 10 additions & 18 deletions src/main/java/com/microsoft/sqlserver/jdbc/IOBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.Charset;
import java.net.*;
import java.math.*;
import java.util.concurrent.*;
Expand Down Expand Up @@ -3946,7 +3947,7 @@ void writeNonUnicodeReader(
Reader reader,
long advertisedLength,
boolean isDestBinary,
String charSet) throws SQLServerException
Charset charSet) throws SQLServerException
{
assert DataTypes.UNKNOWN_STREAM_LENGTH == advertisedLength || advertisedLength >= 0;

Expand Down Expand Up @@ -3997,25 +3998,16 @@ void writeNonUnicodeReader(

for (int charsCopied = 0; charsCopied < charsToWrite; ++charsCopied)
{
try
if(null == charSet)
{
if(null == charSet)
{
streamByteBuffer[charsCopied] = (byte)(streamCharBuffer[charsCopied] & 0xFF);
}
else
{
// encoding as per collation
streamByteBuffer[charsCopied] = new String(streamCharBuffer[charsCopied] +
"")
.getBytes(charSet)[0];
}
streamByteBuffer[charsCopied] = (byte)(streamCharBuffer[charsCopied] & 0xFF);
}
catch (UnsupportedEncodingException e)
else
{
throw new SQLServerException(
SQLServerException.getErrString("R_encodingErrorWritingTDS"),
e);
// encoding as per collation
streamByteBuffer[charsCopied] = new String(streamCharBuffer[charsCopied] +
"")
.getBytes(charSet)[0];
}
}
writeBytes(streamByteBuffer, 0, charsToWrite);
Expand Down Expand Up @@ -7257,7 +7249,7 @@ final Object readGUID(int valueLength, JDBCType jdbcType, StreamType streamType)

try
{
return DDC.convertStringToObject(sb.toString(), Encoding.UNICODE.charsetName(), jdbcType, streamType);
return DDC.convertStringToObject(sb.toString(), Encoding.UNICODE.charset(), jdbcType, streamType);
}
catch (UnsupportedEncodingException e)
{
Expand Down
18 changes: 3 additions & 15 deletions src/main/java/com/microsoft/sqlserver/jdbc/ReaderInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,14 @@ class ReaderInputStream extends InputStream
private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
private ByteBuffer encodedChars = EMPTY_BUFFER;

ReaderInputStream(Reader reader, String charsetName, long readerLength) throws UnsupportedEncodingException
ReaderInputStream(Reader reader, Charset charset, long readerLength)
{
assert reader != null;
assert charsetName != null;
assert charset != null;
assert DataTypes.UNKNOWN_STREAM_LENGTH == readerLength || readerLength >= 0;

this.reader = reader;
try
{
this.charset = Charset.forName(charsetName);
}
catch (IllegalCharsetNameException e)
{
throw new UnsupportedEncodingException(e.getMessage());
}
catch (UnsupportedCharsetException e)
{
throw new UnsupportedEncodingException(e.getMessage());
}

this.charset = charset;
this.readerLength = readerLength;
}

Expand Down
30 changes: 22 additions & 8 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLCollation.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.microsoft.sqlserver.jdbc;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;
import java.text.MessageFormat;

Expand Down Expand Up @@ -49,7 +50,7 @@ final class SQLCollation implements java.io.Serializable
private final Encoding encoding;

// Utility methods for getting details of this collation's encoding
final String getCharset() { return encoding.charsetName(); }
final Charset getCharset() throws SQLServerException { return encoding.charset(); }
final boolean supportsAsciiConversion() { return encoding.supportsAsciiConversion(); }
final boolean hasAsciiCompatibleSBCS() { return encoding.hasAsciiCompatibleSBCS(); }

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

private Encoding(
String charsetName,
Expand All @@ -585,11 +587,7 @@ final Encoding checkSupported() throws UnsupportedEncodingException
{
// Checks for support by converting a java.lang.String
// This works for all of the code pages above in SE 5 and later.
try
{
" ".getBytes(charsetName);
}
catch (UnsupportedEncodingException e)
if (!Charset.isSupported(charsetName))
{
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_codePageNotSupported"));
Object[] msgArgs = {charsetName};
Expand All @@ -602,7 +600,23 @@ final Encoding checkSupported() throws UnsupportedEncodingException
return this;
}

final String charsetName() { return charsetName; }
final Charset charset() throws SQLServerException
{
try
{
checkSupported();
if (charset == null)
{
charset = Charset.forName(charsetName);
}
} catch (UnsupportedEncodingException e)
{
MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_codePageNotSupported"));
Object[] msgArgs = {charsetName};
throw new SQLServerException(form.format(msgArgs), e);
}
return charset;
}

/**
* Returns true if the collation supports conversion to ascii.
Expand Down
36 changes: 13 additions & 23 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBulkCopy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -2552,31 +2551,22 @@ else if (null != sourceBulkRecord)
}
else
{
try
tdsWriter.writeShort((short) (colValueStr.length()));
// converting string into destination collation using Charset

SQLCollation destCollation = destColumnMetadata
.get(destColOrdinal).collation;
if (null != destCollation)
{
tdsWriter.writeShort((short) (colValueStr.length()));
// converting string into destination collation using Charset
tdsWriter.writeBytes(colValueStr.getBytes(
destColumnMetadata.get(destColOrdinal).collation
.getCharset()));

SQLCollation destCollation = destColumnMetadata
.get(destColOrdinal).collation;
if (null != destCollation)
{
tdsWriter.writeBytes(colValueStr.getBytes(
destColumnMetadata.get(destColOrdinal).collation
.getCharset()));

}
else
{
tdsWriter.writeBytes(colValueStr.getBytes());
}
}
catch (UnsupportedEncodingException e)
{
throw new SQLServerException(
SQLServerException.getErrString("R_encodingErrorWritingTDS"),
e);
}
else
{
tdsWriter.writeBytes(colValueStr.getBytes());
}
}
}
}
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerClob.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.*;
import java.util.logging.*;

import static java.nio.charset.StandardCharsets.US_ASCII;

/**
* SQLServerClob represents a character LOB object and implements java.sql.Clob.
*/
Expand Down Expand Up @@ -186,16 +188,8 @@ public InputStream getAsciiStream() throws SQLException
if (null != sqlCollation && !sqlCollation.supportsAsciiConversion())
DataTypes.throwConversionError(getDisplayClassName(), "AsciiStream");

InputStream getterStream;
try
{
// Need to use a BufferedInputStream since the stream returned by this method is assumed to support mark/reset
getterStream = new BufferedInputStream(new ReaderInputStream(new StringReader(value), "US-ASCII", value.length()));
}
catch (UnsupportedEncodingException unsupportedEncodingException)
{
throw new SQLServerException(unsupportedEncodingException.getMessage(), null, 0, unsupportedEncodingException);
}
// Need to use a BufferedInputStream since the stream returned by this method is assumed to support mark/reset
InputStream getterStream = new BufferedInputStream(new ReaderInputStream(new StringReader(value), US_ASCII, value.length()));

activeStreams.add(getterStream);
return getterStream;
Expand Down
32 changes: 3 additions & 29 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerSQLXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ InputStream getValue() throws SQLServerException
ByteArrayOutputStreamToInputStream strm = new ByteArrayOutputStreamToInputStream();
// Need to beat a stream out of docValue
TransformerFactory factory;
Writer wr=null;
try
{
factory = TransformerFactory.newInstance();
Expand All @@ -126,14 +125,7 @@ InputStream getValue() throws SQLServerException
assert null ==outputStreamValue;
assert null == docValue;
assert null !=strValue;
try
{
o = new ByteArrayInputStream(strValue.getBytes(Encoding.UNICODE.charsetName()));
}
catch (UnsupportedEncodingException ex)
{
throw new SQLServerException(null, ex.getMessage(), null, 0, true);
}
o = new ByteArrayInputStream(strValue.getBytes(Encoding.UNICODE.charset()));
}
assert null != o;
isFreed = true; // we have consumed the data
Expand Down Expand Up @@ -252,16 +244,7 @@ public java.io.Writer setCharacterStream() throws SQLException
checkWriteXML();
isUsed = true;
outputStreamValue = new ByteArrayOutputStreamToInputStream();
java.io.Writer wrt=null;
try
{
wrt = new OutputStreamWriter(outputStreamValue, Encoding.UNICODE.charsetName());
}
catch (UnsupportedEncodingException ex)
{
throw new SQLServerException(null, ex.getMessage(), null, 0, true);
}
return wrt;
return new OutputStreamWriter(outputStreamValue, Encoding.UNICODE.charset());
}
public Reader getCharacterStream() throws SQLException
{
Expand Down Expand Up @@ -309,16 +292,7 @@ public String getString() throws SQLException
}

byte byteContents[] = contents.getBytes();
String ret = null;
try
{
ret = new String(byteContents,0, byteContents.length, Encoding.UNICODE.charsetName() );
}
catch (UnsupportedEncodingException ex)
{
throw new SQLServerException(null, ex.getMessage(), null, 0, true);
}
return ret;
return new String(byteContents,0, byteContents.length, Encoding.UNICODE.charset() );
}
public void setString(String value) throws SQLException
{
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/com/microsoft/sqlserver/jdbc/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -619,15 +619,7 @@ static String readUnicodeString(byte [] b, int offset, int byteLength, SQLServer
{
try
{
return new String(b, offset, byteLength, Encoding.UNICODE.charsetName());
}
catch (UnsupportedEncodingException ex)
{
String txtMsg = SQLServerException.checkAndAppendClientConnId(SQLServerException.getErrString("R_stringReadError"), conn);
MessageFormat form = new MessageFormat(txtMsg);
Object[] msgArgs = {new Integer(offset)};
// Re-throw SQLServerException if conversion fails.
throw new SQLServerException(null, form.format(msgArgs), null, 0, true);
return new String(b, offset, byteLength, Encoding.UNICODE.charset());
}
catch (IndexOutOfBoundsException ex)
{
Expand Down
Loading

0 comments on commit 66bb7ae

Please sign in to comment.