📄 stringutils.java
字号:
/* Copyright (C) 2002-2004 MySQL AB This program is free software; you can redistribute it and/or modify it under the terms of version 2 of the GNU General Public License as published by the Free Software Foundation. There are special exceptions to the terms and conditions of the GPL as it is applied to this software. View the full text of the exception in file EXCEPTIONS-CONNECTOR-J in the directory of this software distribution. 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.mysql.jdbc;import java.io.ByteArrayOutputStream;import java.io.UnsupportedEncodingException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.math.BigDecimal;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.StringTokenizer;/** * Various utility methods for converting to/from byte arrays in the platform * encoding * * @author Mark Matthews */public class StringUtils { private static final int BYTE_RANGE = (1 + Byte.MAX_VALUE) - Byte.MIN_VALUE; private static byte[] allBytes = new byte[BYTE_RANGE]; private static char[] byteToChars = new char[BYTE_RANGE]; private static Method toPlainStringMethod; static { for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { allBytes[i - Byte.MIN_VALUE] = (byte) i; } String allBytesString = new String(allBytes, 0, Byte.MAX_VALUE - Byte.MIN_VALUE); int allBytesStringLen = allBytesString.length(); for (int i = 0; (i < (Byte.MAX_VALUE - Byte.MIN_VALUE)) && (i < allBytesStringLen); i++) { byteToChars[i] = allBytesString.charAt(i); } try { toPlainStringMethod = BigDecimal.class.getMethod("toPlainString", new Class[0]); } catch (NoSuchMethodException nsme) { // that's okay, we fallback to .toString() } } static final int WILD_COMPARE_MATCH_NO_WILD = 0; static final int WILD_COMPARE_MATCH_WITH_WILD = 1; static final int WILD_COMPARE_NO_MATCH = -1; /** * Returns the byte[] representation of the given string using given * encoding. * * @param s the string to convert * @param encoding the character encoding to use * @param parserKnowsUnicode DOCUMENT ME! * * @return byte[] representation of the string * * @throws SQLException if an encoding unsupported by the JVM is supplied. */ public static final byte[] getBytes(String s, String encoding, String serverEncoding, boolean parserKnowsUnicode) throws SQLException { try { SingleByteCharsetConverter converter = SingleByteCharsetConverter.getInstance(encoding); return getBytes(s, converter, encoding, serverEncoding, parserKnowsUnicode); } catch (UnsupportedEncodingException uee) { throw new SQLException(Messages.getString("StringUtils.0") //$NON-NLS-1$ +encoding + Messages.getString("StringUtils.1"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } } /** * Returns the byte[] representation of the given string (re)using the * given charset converter, and the given encoding. * * @param s the string to convert * @param converter the converter to reuse * @param encoding the character encoding to use * @param serverEncoding DOCUMENT ME! * @param parserKnowsUnicode DOCUMENT ME! * * @return byte[] representation of the string * * @throws SQLException if an encoding unsupported by the JVM is supplied. */ public static final byte[] getBytes(String s, SingleByteCharsetConverter converter, String encoding, String serverEncoding, boolean parserKnowsUnicode) throws SQLException { try { byte[] b = null; if (converter != null) { b = converter.toBytes(s); } else if (encoding == null) { b = s.getBytes(); } else { b = s.getBytes(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()); } } } return b; } catch (UnsupportedEncodingException uee) { throw new SQLException(Messages.getString("StringUtils.5") //$NON-NLS-1$ +encoding + Messages.getString("StringUtils.6"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } } /** * DOCUMENT ME! * * @param s DOCUMENT ME! * @param converter DOCUMENT ME! * @param encoding DOCUMENT ME! * @param serverEncoding DOCUMENT ME! * @param offset DOCUMENT ME! * @param length DOCUMENT ME! * @param parserKnowsUnicode DOCUMENT ME! * * @return DOCUMENT ME! * * @throws SQLException DOCUMENT ME! */ public static final byte[] getBytes(String s, SingleByteCharsetConverter converter, String encoding, String serverEncoding, int offset, int length, boolean parserKnowsUnicode) throws SQLException { try { byte[] b = null; if (converter != null) { b = converter.toBytes(s, offset, length); } else if (encoding == null) { byte[] temp = s.getBytes(); b = new byte[length]; System.arraycopy(temp, offset, b, 0, length); } else { byte[] temp = s.getBytes(encoding); b = new byte[length]; System.arraycopy(temp, offset, 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); } } } return b; } catch (UnsupportedEncodingException uee) { throw new SQLException(Messages.getString("StringUtils.10") //$NON-NLS-1$ +encoding + Messages.getString("StringUtils.11"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } } /** * Dumps the given bytes to STDOUT as a hex dump (up to length bytes). * * @param byteBuffer the data to print as hex * @param length the number of bytes to print * * @return ... */ public static final String dumpAsHex(byte[] byteBuffer, int length) { StringBuffer outputBuf = new StringBuffer(length * 4); int p = 0; int rows = length / 8; for (int i = 0; (i < rows) && (p < length); i++) { int ptemp = p; for (int j = 0; j < 8; j++) { String hexVal = Integer.toHexString(byteBuffer[ptemp] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; //$NON-NLS-1$ } outputBuf.append(hexVal + " "); //$NON-NLS-1$ ptemp++; } outputBuf.append(" "); //$NON-NLS-1$ for (int j = 0; j < 8; j++) { if ((byteBuffer[p] > 32) && (byteBuffer[p] < 127)) { outputBuf.append((char) byteBuffer[p] + " "); //$NON-NLS-1$ } else { outputBuf.append(". "); //$NON-NLS-1$ } p++; } outputBuf.append("\n"); //$NON-NLS-1$ } int n = 0; for (int i = p; i < length; i++) { String hexVal = Integer.toHexString(byteBuffer[i] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; //$NON-NLS-1$ } outputBuf.append(hexVal + " "); //$NON-NLS-1$ n++; } for (int i = n; i < 8; i++) { outputBuf.append(" "); //$NON-NLS-1$ } outputBuf.append(" "); //$NON-NLS-1$ for (int i = p; i < length; i++) { if ((byteBuffer[i] > 32) && (byteBuffer[i] < 127)) { outputBuf.append((char) byteBuffer[i] + " "); //$NON-NLS-1$ } else { outputBuf.append(". "); //$NON-NLS-1$ } } outputBuf.append("\n"); //$NON-NLS-1$ return outputBuf.toString(); } /** * Splits stringToSplit into a list, using the given delimitter * * @param stringToSplit the string to split * @param delimitter the string to split on * @param trim should the split strings be whitespace trimmed? * * @return the list of strings, split by delimitter * * @throws IllegalArgumentException DOCUMENT ME! */ public static final List split(String stringToSplit, String delimitter, boolean trim) { if (stringToSplit == null) { return new ArrayList(); } if (delimitter == null) { throw new IllegalArgumentException(); } StringTokenizer tokenizer = new StringTokenizer(stringToSplit, delimitter, false); List splitTokens = new ArrayList(tokenizer.countTokens()); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); if (trim) { token = token.trim(); } splitTokens.add(token); } return splitTokens; } /** * Returns the bytes as an ASCII String. * * @param buffer the bytes representing the string * * @return The ASCII String. */ public static final String toAsciiString(byte[] buffer) { return toAsciiString(buffer, 0, buffer.length); } /** * Returns the bytes as an ASCII String. * * @param buffer the bytes to convert * @param startPos the position to start converting * @param length the length of the string to convert * * @return the ASCII string */ public static final String toAsciiString(byte[] buffer, int startPos, int length) { char[] charArray = new char[length]; int readpoint = startPos; for (int i = 0; i < length; i++) { charArray[i] = (char) buffer[readpoint]; readpoint++; } return new String(charArray); } /** * Unfortunately, SJIS has 0x5c as a high byte in some of its double-byte * characters, so we need to escape it. * * @param origBytes the original bytes in SJIS format * @param origString the string that had .getBytes() called on it
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -