stringutils.java
来自「mysql的jdbc驱动」· Java 代码 · 共 1,392 行 · 第 1/3 页
JAVA
1,392 行
* @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.substring(offset, offset + length).getBytes(); length = temp.length; b = new byte[length]; System.arraycopy(temp, 0, b, 0, length); } else { byte[] temp = 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); } } } 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$ } } /** * 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, null); 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$ } } public static int getInt(byte[] buf) throws NumberFormatException { int base = 10; int s = 0; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < buf.length)) { ++s; } if (s == buf.length) { throw new NumberFormatException(new String(buf)); } /* Check for a sign. */ boolean negative = false; if ((char) buf[s] == '-') { negative = true; ++s; } else if ((char) buf[s] == '+') { ++s; } /* Save the pointer so we can check later if anything happened. */ int save = s; int cutoff = Integer.MAX_VALUE / base; int cutlim = (Integer.MAX_VALUE % base); if (negative) { cutlim++; } boolean overflow = false; int i = 0; for (; s < buf.length; s++) { char c = (char) buf[s]; if (Character.isDigit(c)) { c -= '0'; } else if (Character.isLetter(c)) { c = (char) (Character.toUpperCase(c) - 'A' + 10); } else { break; } if (c >= base) { break; } /* Check for overflow. */ if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) { overflow = true; } else { i *= base; i += c; } } if (s == save) { throw new NumberFormatException(new String(buf)); } if (overflow) { throw new NumberFormatException(new String(buf)); } /* Return the result of the appropriate sign. */ return (negative ? (-i) : i); } public static long getLong(byte[] buf) throws NumberFormatException { int base = 10; int s = 0; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < buf.length)) { ++s; } if (s == buf.length) { throw new NumberFormatException(new String(buf)); } /* Check for a sign. */ boolean negative = false; if ((char) buf[s] == '-') { negative = true; ++s; } else if ((char) buf[s] == '+') { ++s; } /* Save the pointer so we can check later if anything happened. */ int save = s; long cutoff = Long.MAX_VALUE / base; long cutlim = (int) (Long.MAX_VALUE % base); if (negative) { cutlim++; } boolean overflow = false; long i = 0; for (; s < buf.length; s++) { char c = (char) buf[s]; if (Character.isDigit(c)) { c -= '0'; } else if (Character.isLetter(c)) { c = (char) (Character.toUpperCase(c) - 'A' + 10); } else { break; } if (c >= base) { break; } /* Check for overflow. */ if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) { overflow = true; } else { i *= base; i += c; } } if (s == save) { throw new NumberFormatException(new String(buf)); } if (overflow) { throw new NumberFormatException(new String(buf)); } /* Return the result of the appropriate sign. */ return (negative ? (-i) : i); } public static short getShort(byte[] buf) throws NumberFormatException { short base = 10; int s = 0; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < buf.length)) { ++s; } if (s == buf.length) { throw new NumberFormatException(new String(buf)); } /* Check for a sign. */ boolean negative = false; if ((char) buf[s] == '-') { negative = true; ++s; } else if ((char) buf[s] == '+') { ++s; } /* Save the pointer so we can check later if anything happened. */ int save = s; short cutoff = (short) (Short.MAX_VALUE / base); short cutlim = (short) (Short.MAX_VALUE % base); if (negative) { cutlim++; } boolean overflow = false; short i = 0; for (; s < buf.length; s++) { char c = (char) buf[s]; if (Character.isDigit(c)) { c -= '0'; } else if (Character.isLetter(c)) { c = (char) (Character.toUpperCase(c) - 'A' + 10); } else { break; } if (c >= base) { break; } /* Check for overflow. */ if ((i > cutoff) || ((i == cutoff) && (c > cutlim))) { overflow = true; } else { i *= base; i += c; } } if (s == save) { throw new NumberFormatException(new String(buf)); } if (overflow) { throw new NumberFormatException(new String(buf)); } /* Return the result of the appropriate sign. */ return (negative ? (short) -i : (short) i); } public final static int indexOfIgnoreCase(int startingPosition, String searchIn, String searchFor) { if ((searchIn == null) || (searchFor == null) || startingPosition > searchIn.length()) { return -1; } int patternLength = searchFor.length(); int stringLength = searchIn.length(); int stopSearchingAt = stringLength - patternLength; int i = startingPosition; if (patternLength == 0) { return -1; } // Brute force string pattern matching // Some locales don't follow upper-case rule, so need to check both char firstCharOfPatternUc = Character.toUpperCase(searchFor.charAt(0)); char firstCharOfPatternLc = Character.toLowerCase(searchFor.charAt(0)); lookForFirstChar: while (true) { while ((i < stopSearchingAt) && (Character.toUpperCase(searchIn.charAt(i)) != firstCharOfPatternUc) && Character.toLowerCase(searchIn.charAt(i)) != firstCharOfPatternLc) { i++; } if (i > stopSearchingAt) { return -1; } int j = i + 1; int end = (j + patternLength) - 1; int k = 1; // start at second char of pattern while (j < end) { int searchInPos = j++; int searchForPos = k++; if (Character.toUpperCase(searchIn.charAt(searchInPos)) != Character .toUpperCase(searchFor.charAt(searchForPos))) { i++; // start over continue lookForFirstChar; } // Georgian and Turkish locales don't have same convention, so // need to check lowercase // too! if (Character.toLowerCase(searchIn.charAt(searchInPos)) != Character .toLowerCase(searchFor.charAt(searchForPos))) { i++; // start over continue lookForFirstChar; } } return i; // found entire pattern } } /** * DOCUMENT ME! * * @param searchIn * DOCUMENT ME! * @param searchFor * DOCUMENT ME! * * @return DOCUMENT ME! */ public final static int indexOfIgnoreCase(String searchIn, String searchFor) { return indexOfIgnoreCase(0, searchIn, searchFor); } public static int indexOfIgnoreCaseRespectMarker(int startAt, String src, String target, String marker, String markerCloses, boolean allowBackslashEscapes) { char contextMarker = Character.MIN_VALUE; boolean escaped = false; int markerTypeFound = 0; int srcLength = src.length(); int ind = 0; for (int i = startAt; i < srcLength; i++) { char c = src.charAt(i); if (allowBackslashEscapes && c == '\\') { escaped = !escaped; } else if (c == markerCloses.charAt(markerTypeFound) && !escaped) { contextMarker = Character.MIN_VALUE; } else if ((ind = marker.indexOf(c)) != -1 && !escaped
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?