=== modified file 'src/com/mysql/jdbc/StringUtils.java' --- src/com/mysql/jdbc/StringUtils.java 2013-10-16 16:25:02 +0000 +++ src/com/mysql/jdbc/StringUtils.java 2014-03-11 11:08:05 +0000 @@ -33,6 +33,7 @@ import java.lang.reflect.Method; import java.math.BigDecimal; import java.nio.ByteBuffer; +import java.nio.CharBuffer; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.UnsupportedCharsetException; @@ -400,11 +401,11 @@ * * @return String the string with a '+' added (if needed) */ - public static final String fixDecimalExponent(String dString) { - int ePos = dString.indexOf("E"); //$NON-NLS-1$ + public static String fixDecimalExponent(String dString) { + int ePos = dString.indexOf('E'); //$NON-NLS-1$ if (ePos == -1) { - ePos = dString.indexOf("e"); //$NON-NLS-1$ + ePos = dString.indexOf('e'); //$NON-NLS-1$ } if (ePos != -1) { @@ -424,28 +425,27 @@ return dString; } - public static final byte[] getBytes(char[] c, + public static byte[] getBytes(char[] c, SingleByteCharsetConverter converter, String encoding, String serverEncoding, boolean parserKnowsUnicode, ExceptionInterceptor exceptionInterceptor) throws SQLException { try { - byte[] b = null; + byte[] b; if (converter != null) { b = converter.toBytes(c); } else if (encoding == null) { - b = new String(c).getBytes(); + b = getBytes(c, 0, c.length, platformEncoding); } else { - String s = new String(c); - - b = s.getBytes(encoding); + b = getBytes(c, 0, c.length, encoding); if (!parserKnowsUnicode && (encoding.equalsIgnoreCase("SJIS") //$NON-NLS-1$ || encoding.equalsIgnoreCase("BIG5") //$NON-NLS-1$ || encoding.equalsIgnoreCase("GBK"))) { //$NON-NLS-1$ if (!encoding.equalsIgnoreCase(serverEncoding)) { - b = escapeEasternUnicodeByteStream(b, s, 0, s.length()); + String s = new String(c); + b = escapeEasternUnicodeByteStream(b, s, 0, b.length); } } } @@ -458,38 +458,27 @@ } } - public static final byte[] getBytes(char[] c, + public static byte[] getBytes(char[] c, SingleByteCharsetConverter converter, String encoding, String serverEncoding, int offset, int length, boolean parserKnowsUnicode, ExceptionInterceptor exceptionInterceptor) throws SQLException { try { - byte[] b = null; + byte[] b; if (converter != null) { b = converter.toBytes(c, offset, length); } else if (encoding == null) { - byte[] temp = new String(c, offset, length).getBytes(); - - length = temp.length; - - b = new byte[length]; - System.arraycopy(temp, 0, b, 0, length); + b = getBytes(c, offset, length, platformEncoding); } else { - String s = new String(c, offset, length); - - byte[] temp = s.getBytes(encoding); - - length = temp.length; - - b = new byte[length]; - System.arraycopy(temp, 0, b, 0, length); + b = getBytes(c, offset, length, encoding); if (!parserKnowsUnicode && (encoding.equalsIgnoreCase("SJIS") //$NON-NLS-1$ || encoding.equalsIgnoreCase("BIG5") //$NON-NLS-1$ || encoding.equalsIgnoreCase("GBK"))) { //$NON-NLS-1$ if (!encoding.equalsIgnoreCase(serverEncoding)) { - b = escapeEasternUnicodeByteStream(b, s, offset, length); + String s = new String(c, offset, length); + b = escapeEasternUnicodeByteStream(b, s, offset, b.length); } } } @@ -502,13 +491,13 @@ } } - public static final byte[] getBytes(char[] c, String encoding, + public static byte[] getBytes(char[] c, String encoding, String serverEncoding, boolean parserKnowsUnicode, MySQLConnection conn, ExceptionInterceptor exceptionInterceptor) throws SQLException { try { - SingleByteCharsetConverter converter = null; + SingleByteCharsetConverter converter; if (conn != null) { converter = conn.getCharsetConverter(encoding); @@ -545,12 +534,12 @@ * @throws SQLException * if an encoding unsupported by the JVM is supplied. */ - public static final byte[] getBytes(String s, + public static byte[] getBytes(String s, SingleByteCharsetConverter converter, String encoding, String serverEncoding, boolean parserKnowsUnicode, ExceptionInterceptor exceptionInterceptor) throws SQLException { try { - byte[] b = null; + byte[] b; if (converter != null) { b = converter.toBytes(s); @@ -577,12 +566,12 @@ } } - public static final byte[] getBytesWrapped(String s, char beginWrap, char endWrap, + public static byte[] getBytesWrapped(String s, char beginWrap, char endWrap, SingleByteCharsetConverter converter, String encoding, String serverEncoding, boolean parserKnowsUnicode, ExceptionInterceptor exceptionInterceptor) throws SQLException { try { - byte[] b = null; + byte[] b; if (converter != null) { b = converter.toBytesWrapped(s, beginWrap, endWrap); @@ -643,38 +632,28 @@ * @throws SQLException * DOCUMENT ME! */ - public static final byte[] getBytes(String s, + public static byte[] getBytes(String s, SingleByteCharsetConverter converter, String encoding, String serverEncoding, int offset, int length, boolean parserKnowsUnicode, ExceptionInterceptor exceptionInterceptor) throws SQLException { try { - byte[] b = null; + byte[] b; if (converter != null) { b = converter.toBytes(s, offset, length); } else if (encoding == null) { - byte[] temp = s.substring(offset, offset + length).getBytes(); - - length = temp.length; - - b = new byte[length]; - System.arraycopy(temp, 0, b, 0, length); + b = s.substring(offset, offset + length).getBytes(); } else { - byte[] temp = s.substring(offset, offset + length) + b = s.substring(offset, offset + length) .getBytes(encoding); - length = temp.length; - - b = new byte[length]; - System.arraycopy(temp, 0, b, 0, length); - if (!parserKnowsUnicode && (encoding.equalsIgnoreCase("SJIS") //$NON-NLS-1$ || encoding.equalsIgnoreCase("BIG5") //$NON-NLS-1$ || encoding.equalsIgnoreCase("GBK"))) { //$NON-NLS-1$ if (!encoding.equalsIgnoreCase(serverEncoding)) { - b = escapeEasternUnicodeByteStream(b, s, offset, length); + b = escapeEasternUnicodeByteStream(b, s, offset, b.length); } } } @@ -703,12 +682,12 @@ * @throws SQLException * if an encoding unsupported by the JVM is supplied. */ - public static final byte[] getBytes(String s, String encoding, + public static byte[] getBytes(String s, String encoding, String serverEncoding, boolean parserKnowsUnicode, MySQLConnection conn, ExceptionInterceptor exceptionInterceptor) throws SQLException { try { - SingleByteCharsetConverter converter = null; + SingleByteCharsetConverter converter; if (conn != null) { converter = conn.getCharsetConverter(encoding); @@ -2078,4 +2057,22 @@ public static final boolean isValidIdChar(char c) { return VALID_ID_CHARS.indexOf(c) != -1; } + + public static byte[] getBytes(char[] value, int offset, int length, + String encoding) throws UnsupportedEncodingException + { + Charset cs = findCharset(encoding); + + // can't simply .array() this to get the bytes + // especially with variable-length charsets the + // buffer is sometimes larger than the actual encoded data + ByteBuffer buf = cs.encode(CharBuffer.wrap(value, offset, length)); + + int encodedLen = buf.limit(); + byte[] asBytes = new byte[encodedLen]; + buf.get(asBytes, 0, encodedLen); + + return asBytes; + } + }