📄 util.java
字号:
char[] r = new char[s.length() + n * (v2Len - v1Len)]; int rPos = 0; // for each occurance, copy v2 where v1 used to be while(-1 != (ix = s.indexOf(v1, start))) { while(start < ix) r[rPos++] = s.charAt(start++); for(int j = 0; j < v2Len; j++) { r[rPos++] = v2.charAt(j); } start += v1Len; } // ...and add all remaining chars ix = s.length(); while(start < ix) r[rPos++] = s.charAt(start++); // ..ouch. this hurts. return new String(r); } public static String getContent(File f) { DataInputStream in = null; try { in = new DataInputStream(new FileInputStream(f)); return in.readUTF(); } catch (IOException ignore) { } finally { if (in != null) { try { in.close(); } catch (IOException ignore) { } } } return null; } public static void putContent(File f, String content) throws IOException { DataOutputStream out = null; try { out = new DataOutputStream(new FileOutputStream(f)); out.writeUTF(content); } finally { if (out != null) { out.close(); } } } public interface Comparator { public int compare(Object a, Object b); } /** * Sort a vector with objects compareble using a comparison function. * * @param a Vector to sort * @param cf comparison function */ static public void sort(Vector a, Comparator cf, boolean bReverse) { sort(a, 0, a.size() - 1, cf, bReverse ? -1 : 1); } /** * Vector QSort implementation. */ static void sort(Vector a, int lo0, int hi0, Comparator cf, int k) { int lo = lo0; int hi = hi0; Object mid; if ( hi0 > lo0) { mid = a.elementAt( ( lo0 + hi0 ) / 2 ); while( lo <= hi ) { while( ( lo < hi0 ) && ( k * cf.compare(a.elementAt(lo), mid) < 0 )) { ++lo; } while( ( hi > lo0 ) && ( k * cf.compare(a.elementAt(hi), mid ) > 0 )) { --hi; } if( lo <= hi ) { swap(a, lo, hi); ++lo; --hi; } } if( lo0 < hi ) { sort( a, lo0, hi, cf, k ); } if( lo < hi0 ) { sort( a, lo, hi0, cf, k ); } } } private static void swap(Vector a, int i, int j) { Object tmp = a.elementAt(i); a.setElementAt(a.elementAt(j), i); a.setElementAt(tmp, j); } private static final byte encTab[] = { 0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x4b,0x4c,0x4d,0x4e,0x4f,0x50, 0x51,0x52,0x53,0x54,0x55,0x56,0x57,0x58,0x59,0x5a,0x61,0x62,0x63,0x64,0x65,0x66, 0x67,0x68,0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70,0x71,0x72,0x73,0x74,0x75,0x76, 0x77,0x78,0x79,0x7a,0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x2b,0x2f }; private static final byte decTab[]={ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 }; public static String base64Encode(String s) throws IOException { return encode(s.getBytes(), 0); } /** * Encode a raw byte array to a Base64 String. * * @param in Byte array to encode. */ // public static String encode(byte[] in) throws IOException { // return encode(in, 0); // } /** * Encode a raw byte array to a Base64 String. * * @param in Byte array to encode. * @param len Length of Base64 lines. 0 means no line breaks. */ public static String encode(byte[] in, int len) throws IOException { ByteArrayOutputStream baos = null; ByteArrayInputStream bais = null; try { baos = new ByteArrayOutputStream(); bais = new ByteArrayInputStream(in); encode(bais, baos, len); // ASCII byte array to String return(new String(baos.toByteArray())); } finally { if (baos != null) baos.close(); if (bais != null) bais.close(); } } public static void encode(InputStream in, OutputStream out, int len) throws IOException { // Check that length is a multiple of 4 bytes if(len%4!=0) throw new IllegalArgumentException("Length must be a multiple of 4"); // Read input stream until end of file int bits=0; int nbits=0; int nbytes=0; int b; while( (b=in.read()) != -1) { bits=(bits<<8)|b; nbits+=8; while(nbits>=6) { nbits-=6; out.write(encTab[0x3f&(bits>>nbits)]); nbytes ++; // New line if (len !=0 && nbytes>=len) { out.write(0x0d); out.write(0x0a); nbytes -= len; } } } switch(nbits) { case 2: out.write(encTab[0x3f&(bits<<4)]); out.write(0x3d); // 0x3d = '=' out.write(0x3d); break; case 4: out.write(encTab[0x3f&(bits<<2)]); out.write(0x3d); break; } if (len != 0) { if (nbytes != 0) { out.write(0x0d); out.write(0x0a); } out.write(0x0d); out.write(0x0a); } }}/** * Class for tokenize an attribute string. */class AttributeTokenizer { String s; int length; int pos = 0; AttributeTokenizer(String input) { s = input; length = s.length(); } String getWord() { skipWhite(); boolean backslash = false; boolean quote = false; StringBuffer val = new StringBuffer(); int end = 0; loop: for (; pos < length; pos++) { if (backslash) { backslash = false; val.append(s.charAt(pos)); } else { char c = s.charAt(pos); switch (c) { case '"': quote = !quote; end = val.length(); break; case '\\': backslash = true; break; case ',': case ';': case '=': if (!quote) { break loop; } // Fall through default: val.append(c); if (!Character.isWhitespace(c)) { end = val.length(); } break; } } } if (quote || backslash || end == 0) { return null; } char [] res = new char [end]; val.getChars(0, end, res, 0); return new String(res); } String getKey() { if (pos >= length) { return null; } int save = pos; if (s.charAt(pos) == ';') { pos++; } String res = getWord(); if (res != null) { if (pos == length) { return res; } char c = s.charAt(pos); if (c == ';' || c == ',') { return res; } } pos = save; return null; } String getParam() { if (pos == length || s.charAt(pos) != ';') { return null; } int save = pos++; String res = getWord(); if (res != null && pos < length && s.charAt(pos) == '=') { return res; } pos = save; return null; } String getValue() { if (s.charAt(pos) != '=') { return null; } int save = pos++; skipWhite(); String val = getWord(); if (val == null) { pos = save; return null; } return val; } boolean getEntryEnd() { int save = pos; skipWhite(); if (pos == length) { return true; } else if (s.charAt(pos) == ',') { pos++; return true; } else { pos = save; return false; } } boolean getEnd() { int save = pos; skipWhite(); if (pos == length) { return true; } else { pos = save; return false; } } String getRest() { String res = s.substring(pos).trim(); return res.length() == 0 ? "<END OF LINE>" : res; } private void skipWhite() { for (; pos < length; pos++) { if (!Character.isWhitespace(s.charAt(pos))) { break; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -