📄 stringutils.java
字号:
|| 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 SQLError.createSQLException(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 SQLError.createSQLException(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, ConnectionImpl conn) throws SQLException { try { SingleByteCharsetConverter converter = null; if (conn != null) { converter = conn.getCharsetConverter(encoding); } else { converter = SingleByteCharsetConverter.getInstance(encoding, null); } return getBytes(s, converter, encoding, serverEncoding, parserKnowsUnicode); } catch (UnsupportedEncodingException uee) { throw SQLError.createSQLException(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, int offset, int endPos) throws NumberFormatException { int base = 10; int s = offset; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < endPos)) { ++s; } if (s == endPos) { 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 < endPos; 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 int getInt(byte[] buf) throws NumberFormatException { return getInt(buf, 0, buf.length); } public static long getLong(byte[] buf) throws NumberFormatException { return getLong(buf, 0, buf.length); } public static long getLong(byte[] buf, int offset, int endpos) throws NumberFormatException { int base = 10; int s = offset; /* Skip white space. */ while (Character.isWhitespace((char) buf[s]) && (s < endpos)) { ++s; } if (s == endpos) { 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 < endpos; 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 && contextMarker == Character.MIN_VALUE) { markerTypeFound = ind; contextMarker = c; } else if (c == target.charAt(0) && !escaped && contextMarker == Character.MIN_VALUE) { if (indexOfIgnoreCase(i, src, target) != -1) return i; } } return -1; } public static int indexOfIgnoreCaseRespectQuotes(int startAt, String src, String target, char quoteChar, boolean allowBackslashEscapes) { char contextMarker = Character.MIN_VALUE; boolean escaped = false; int srcLength = src.length(); for (int i = startAt; i < srcLength; i++) { char c = src.charAt(i); if (allowBackslashEscapes && c == '\\') { escaped = !escaped; } else if (c == contextMarker && !escaped) { contextMarker = Character.MIN_VALUE; } else if (c == quoteChar && !escaped && contextMarker == Character.MIN_VALUE) { contextMarker = c; // This test looks complex, but remember that in certain locales, upper case // of two different codepoints coverts to same codepoint, and vice-versa. } else if ((Character.toUpperCase(c) == Character.toUpperCase(target.charAt(0)) || Character.toLowerCase(c) == Character.toLowerCase(target.charAt(0))) && !escaped && contextMarker == Character.MIN_VALUE) { if (startsWithIgnoreCase(src, i, target)) return i; } } return -1; } /** * 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; } /** * 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 delimiter * * @throws IllegalArgumentException * DOCUMENT ME! */ public static final List split(String stringToSplit, String delimiter, String markers, String markerCloses, boolean trim) { if (stringToSplit == null) { return new ArrayList(); } if (delimiter == null) { throw new IllegalArgumentException(); } int delimPos = 0; int currentPos = 0; List splitTokens = new ArrayList(); while ((delimPos = indexOfIgnoreCaseRespectMarker(currentPos, stringToSplit, delimiter, markers, markerCloses, false)) != -1) { String token = stringToSplit.substring(currentPos, delimPos); if (trim) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -