Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.eryansky.common.utils; import org.apache.commons.lang3.StringUtils; import org.apache.oro.text.regex.MalformedPatternException; import org.apache.oro.text.regex.Perl5Compiler; import org.apache.oro.text.regex.Perl5Matcher; import org.springframework.util.Assert; import com.eryansky.common.exception.SystemException; import java.awt.*; import java.io.*; import java.math.BigDecimal; import java.net.MalformedURLException; import java.net.URL; import java.net.URLDecoder; import java.net.URLEncoder; import java.sql.Blob; import java.text.DecimalFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.DataFormatException; import java.util.zip.Deflater; import java.util.zip.Inflater; /** * java * * @author &Eryan eryanwcp@gmail.com * @date 2011-12-30?2:31:16 */ public class SysUtils { private static final int DEF_DIV_SCALE = 2; public static final String FULL_DATEFORMAT = "yyyy-MM-dd HH:mm:ss"; public static final String CHS_DATEFORMAT = "yyyyMMdd"; public static final String SHORT_DATEFORMAT = "yyyy-MM-dd"; public static final String SHORT_TIMEFORMAT = "HH:mm:ss"; public static final int BUFFER_SIZE = 16 * 1024; /** * * * @param text * @param length * @param c * @param charsetName * @return * @throws Exception */ public static String rightPad(String text, int length, byte c, String charsetName) throws Exception { if (null == text) text = ""; byte[] array = new byte[length]; byte[] reference = text.getBytes(charsetName); Arrays.fill(array, reference.length, length, c); System.arraycopy(reference, 0, array, 0, reference.length); return new String(array, charsetName); } public static String rightPad(String text, int length, byte c) { if (null == text) text = ""; byte[] array = new byte[length]; byte[] reference = text.getBytes(); Arrays.fill(array, reference.length, length, c); System.arraycopy(reference, 0, array, 0, reference.length); return new String(array); } /** * * * @param o * @param length * @return * @throws Exception */ public static String decimalPad(Integer o, int length) { if (null == o) return rightPad("", length, (byte) ' '); byte[] array = new byte[length]; Arrays.fill(array, 0, length, (byte) '0'); return new DecimalFormat(new String(array)).format(o); } /** * ? * * @param o * @param length * @param scale * @return * @throws Exception */ public static String decimalPad(Double o, int length, int scale) throws Exception { if (length < (2 + scale)) throw new Exception("?4!"); if (null == o) return rightPad("", length, (byte) ' '); byte[] array = new byte[length]; Arrays.fill(array, 0, length, (byte) '0'); array[length - (1 + scale)] = (byte) '.'; return new DecimalFormat(new String(array)).format(o); } /** * ?? * * @param o * @return */ public static String moneyFormat(Double o) { return new DecimalFormat("0.00").format(null != o ? o : 0); } public static int getNowYear() { return getYear(new Date()); } public static int getYear(Date date) { return getDatePar(Calendar.YEAR, date); } public static int getNowMonth() { return getMonth(new Date()); } public static int getMonth(Date date) { return getDatePar(Calendar.MONTH, date); } public static int getNowDay() { return getDay(new Date()); } public static int getDay(Date date) { return getDatePar(Calendar.DAY_OF_MONTH, date); } public static int getDatePar(int par, Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); int result = calendar.get(par); switch (par) { case Calendar.MONTH: result++; } return result; } public static String getNowDateStr() { String result = dateFormat(getNowDate(), CHS_DATEFORMAT); Calendar c = Calendar.getInstance(); for (int i = 0; i < 8; i++) { c.add(Calendar.DAY_OF_MONTH, 1); @SuppressWarnings("unused") int e = c.get(Calendar.DAY_OF_WEEK); } result += " "; switch (Calendar.getInstance().get(Calendar.DAY_OF_WEEK)) { case Calendar.SUNDAY: return result + ""; case Calendar.MONDAY: return result + ""; case Calendar.TUESDAY: return result + ""; case Calendar.WEDNESDAY: return result + ""; case Calendar.THURSDAY: return result + ""; case Calendar.FRIDAY: return result + ""; case Calendar.SATURDAY: return result + ""; default: return result; } } /** * ?????? * * @param v * ?? * @param scale * ???? * @return ?? */ public static double round(double v, int scale) { if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); } BigDecimal b = new BigDecimal(Double.toString(v)); BigDecimal one = new BigDecimal("1"); return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue(); } /** * * * @param in_str * * @return ? */ public static byte[] zip_Str(String in_str) { byte[] input = new byte[0]; try { input = in_str.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } ArrayList<Byte> al = new ArrayList<Byte>(); byte[] output; Deflater compresser = new Deflater(); compresser.setInput(input); compresser.finish(); for (; !compresser.finished();) { output = new byte[100]; compresser.deflate(output); for (int i = 0; i < output.length; i++) { al.add(new Byte(output[i])); } } output = new byte[al.size()]; for (int i = 0; i < al.size(); i++) { output[i] = (al.get(i)).byteValue(); } return output; } /** * * * @param in * ? * @return */ public static String unZip_Str(byte[] in) { Inflater decompresser = new Inflater(); decompresser.setInput(in); ArrayList<Byte> al = new ArrayList<Byte>(); byte[] result; int count = 0; for (; !decompresser.finished();) { result = new byte[100]; try { count += decompresser.inflate(result); } catch (DataFormatException e) { e.printStackTrace(); } for (int i = 0; i < result.length; i++) { al.add(new Byte(result[i])); } } result = new byte[al.size()]; for (int i = 0; i < al.size(); i++) { result[i] = (al.get(i)).byteValue(); } decompresser.end(); try { return new String(result, 0, count, "UTF-8"); } catch (UnsupportedEncodingException e) { return ""; } } /** * ?INT * * @param expression * @return */ public static boolean isInt(Object expression) { if (expression != null) { try { Integer.parseInt(expression.toString()); } catch (Exception e) { return false; } } return true; } /** * ?DOUBLE * * @param expression * @return */ public static boolean isDuble(Object expression) { if (expression != null) { try { Double.parseDouble(expression.toString()); } catch (Exception e) { return false; } } return true; } /** * ?? * * @param array * * @return ?? */ public static boolean isIntArray(String[] array) { if (array == null) { return false; } if (array.length < 1) { return false; } for (String string : array) { if (!isInt(string)) { return false; } } return true; } /** * HTML * * @param htmlText * HTML * @return */ public static String cleanHtmlTag(String htmlText) { String reg = "</?[a-z][a-z0-9]*[^<>]*>?"; return htmlText.replaceAll(reg, ""); } /** * ? * * @return String ? */ public static String getNowTime() { return dateFormat(getNowDate(), FULL_DATEFORMAT); } /** * ????n????... * * @param str * ?? * @param num * ? * @param hasDot * ?... * @return */ public static String format(String str, int num, boolean hasDot) { if (str == null) return ""; else { if (str.getBytes().length < num * 2) return str; else { byte[] ss = str.getBytes(); byte[] bs = new byte[num * 2]; for (int i = 0; i < bs.length; i++) { bs[i] = ss[i]; } String subStr = SysUtils.substring(str, num * 2); if (hasDot) { subStr = subStr + "..."; } return subStr; } } } /** * ? * * @param s * * @param maxLength * * @return */ public static String substring(String s, int maxLength) { if (s.getBytes().length <= maxLength) return s; int i = 0; for (int k = 0; k < maxLength && i < s.length(); i++, k++) { if (s.charAt(i) > '') { k++; } } if (i < s.length()) { s = s.substring(0, i); } return s; } /** * ? * * @param s * * @return "" */ public static String null2String(Object s) { return null2String(s, ""); } /** * ??????.1lo0 * <p/> * ?1-9 a-z A-Z * * @param length * * @return ???? */ public static String getRandomString(int length) { StringBuffer bu = new StringBuffer(); String[] arr = { "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; Random random = new Random(); while (bu.length() < length) { String temp = arr[random.nextInt(57)]; if (bu.indexOf(temp) == -1) { bu.append(temp); } } return bu.toString(); } /** * ??? * * @param sek * ??? * @param min * ? * @param max * * @return */ public static int getRandomInt(int sek, int min, int max) { Random random = new Random(); int temp = 0; do { temp = random.nextInt(sek); } while (temp < min || temp > max); return temp; } public static String null2String(Object s, String def) { Assert.notNull(def, "def?"); return s == null ? "" : s.toString().trim(); } /** * ?INT * * @param s * * @return -1 */ public static int null2Int(Object s) { return null2Int(s, -1); } /** * ?INT * * @param object * * @param def * * @return INT */ public static int null2Int(Object object, int def) { if (object != null) { try { return Integer.parseInt(object.toString()); } catch (Exception e) { } } return def; } /** * ?Long * * @param object * * @param def * * @return Long */ public static Long null2Long(Object object, Long def) { if (object != null) { try { return Long.parseLong(object.toString()); } catch (Exception e) { } } return def; } /** * ?Float * * @param s * * @return -1 */ public static float null2Float(Object s) { return null2Float(s, -1); } /** * ?Float * * @param s * * @return -1 */ public static float null2Float(Object s, float defValue) { if (s != null) { try { return Float.parseFloat(s.toString()); } catch (Exception e) { } } return defValue; } /** * ?Float * * @param s * * @return -1 */ public static double null2Double(Object s, double defValue) { if (s != null) { try { return Double.parseDouble(s.toString()); } catch (Exception e) { } } return defValue; } /** * object?bool * * @param expression * ?? * @param defValue * ? * @return ??bool */ public static boolean null2Boolean(Object expression, boolean defValue) { try { return java.lang.Boolean.parseBoolean(null2String(expression)); } catch (Exception e) { return defValue; } } /** * ?? * * @param date * * @param type * ? * @return */ public static String dateFormat(Date date, String type) { SimpleDateFormat sdf = new SimpleDateFormat(type); return sdf.format(date); } public static String dateFormatTally() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, -1); return dateFormat(calendar.getTime(), "yyyyMMdd"); } public static String dateFormatTallyStr() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, -1); return dateFormat(calendar.getTime(), "yyyy-MM-dd"); } public static String dateFormatAccountMonth() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); return dateFormat(calendar.getTime(), "yyyyMM"); } public static String dateFormatTallyCN() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, -1); return dateFormat(calendar.getTime(), "yyyyMMdd"); } /** * ?? * * @param type * ? * @return */ public static String dateFormat(String type) { return dateFormat(getNowDate(), type); } public static Date getNowDate(int field, int amount) { Calendar calendar = Calendar.getInstance(); calendar.add(field, amount); return calendar.getTime(); } public static String getBeiJingDateTime() { return SysUtils.dateFormat(SysUtils.getNowDate(Calendar.HOUR_OF_DAY, 8), SysUtils.FULL_DATEFORMAT); } public static String getBeiJingDate() { return SysUtils.dateFormat(SysUtils.getNowDate(Calendar.HOUR_OF_DAY, 8), SysUtils.SHORT_DATEFORMAT); } public static long getNowDateBeginLong(int field, int amount) { Calendar calendar = Calendar.getInstance(); calendar.add(field, amount); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime().getTime(); } public static long getNowDateEndLong(int field, int amount) { Calendar calendar = Calendar.getInstance(); calendar.add(field, amount); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime().getTime(); } /** * ?date * * @return */ public static Date getNowDate() { return new Date(); } /** * ? * * @return String */ public static String getNowShortDate() { return dateFormat(getNowDate(), SHORT_DATEFORMAT); } /** * ? * * @return */ public static String getNowShortTime() { return dateFormat(getNowDate(), SHORT_TIMEFORMAT); } /** * ? * * @param date * @param format * @return */ public static boolean checkDate(String date, String format) { SimpleDateFormat df = new SimpleDateFormat(format); try { df.parse(date); } catch (Exception e) { // ??,? return false; } return true; } /** * (??,,?)<br> * s - ,m - ,h - ,d - * howLong("h","2007-08-09 10:22:26","2007-08-09 20:21:30") ///9?56 9? * * @throws java.text.ParseException */ public static long howLong(String unit, String time1, String time2) throws ParseException { // ??(?1(24?) 0)? Date date1 = new SimpleDateFormat(FULL_DATEFORMAT).parse(time1); Date date2 = new SimpleDateFormat(FULL_DATEFORMAT).parse(time2); long ltime = date1.getTime() - date2.getTime() < 0 ? date2.getTime() - date1.getTime() : date1.getTime() - date2.getTime(); if (unit.equals("s")) { return ltime / 1000;// } else if (unit.equals("m")) { return ltime / 60000;// } else if (unit.equals("h")) { return ltime / 3600000;// ? } else if (unit.equals("d")) { return ltime / 86400000;// } else { return 0; } } /** * ?date * * @param time * @param type * @return * @throws ParseException */ public static Date parseDate(String time, String type) throws ParseException { return new SimpleDateFormat(type).parse(time); } /** * ?date * * @param time * @return * @throws ParseException */ public static Date parseDate(String time) throws ParseException { return new SimpleDateFormat(FULL_DATEFORMAT).parse(time); } /** * * * @param time * * @param tim * * @param type * 'h' - ?,'m' - ,'s' - * @return * @throws ParseException */ public static int strDateDiffTimes(String time, int tim, char type) throws ParseException { if (null2String(time).equals("")) { return 1; } long diff = 1; Calendar calendar = Calendar.getInstance(); calendar.setTime(new SimpleDateFormat(FULL_DATEFORMAT).parse(time)); switch (type) { case 'h': calendar.add(Calendar.HOUR_OF_DAY, tim); break; case 'm': calendar.add(Calendar.MINUTE, tim); break; case 's': calendar.add(Calendar.SECOND, tim); break; } Date date = calendar.getTime(); long ltime = getNowDate().getTime() - date.getTime(); switch (type) { case 'h': diff = ltime / 3600000;// ? break; case 'm': diff = ltime / 60000;// break; case 's': diff = ltime / 1000;// break; } if (diff > Integer.MAX_VALUE) { return Integer.MAX_VALUE; } else if (diff < Integer.MIN_VALUE) { return Integer.MIN_VALUE; } return SysUtils.null2Int(diff); } /** * ? * * @param time * * @param hours * ? * @return * @throws ParseException */ public static int strDateDiffHours(String time, int hours) throws ParseException { return strDateDiffTimes(time, hours, 'h'); } /** * * * @param time * * @param minutes * @return * @throws ParseException */ public static int strDateDiffMinutes(String time, int minutes) throws ParseException { return strDateDiffTimes(time, minutes, 'm'); } /** * * * @param time * * @param sec * @return * @throws ParseException */ public static int strDateDiffSeconds(String time, int sec) throws ParseException { return strDateDiffTimes(time, sec, 's'); } /** * ? * * @param email * @return */ public static boolean checkEmail(String email) { Pattern pattern = Pattern.compile("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+"); Matcher matcher = pattern.matcher(email); if (matcher.matches()) return true; return false; } /** * ??? * * @param s * @param encoding * @return */ public static String stringFormat(String s, String encod, String encoding) { try { return new String(s.getBytes(encod), encoding); } catch (UnsupportedEncodingException e) { return s; } } public static String stringFormat(String s) { return stringFormat(s, "ISO8859-1", "UTF-8"); } /** * URL? * * @param s * * @return URL? */ public static String urlEncode(String s) { try { return URLEncoder.encode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { return s; } } /** * URL? * * @param s * * @return ? */ public static String urlDecode(String s) { try { return URLDecoder.decode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { return s; } } /** * IP?IP?<br> * IPIP??*IP?, 192.168.1.* * * @param ip * @param ipArry * @return */ public static boolean inIPArray(String ip, String[] ipArry) { String[] userip = ip.split("\\."); for (int ipIndex = 0; ipIndex < ipArry.length; ipIndex++) { String[] tempip = ipArry[ipIndex].split("\\."); int r = 0; for (int i = 0; i < tempip.length; i++) { if (tempip[i].equals("*")) { return true; } if (userip.length > i) { if (tempip[i].equals(userip[i])) { r++; } else { break; } } else { break; } } // end for if (r == 4) { return true; } } // end for return false; } /** * ? * * @param strSearch * * @param stringArray * * @param caseInsensetive * ???, true?, false * @return ?, ?-1 */ public static int getInArrayID(String strSearch, String[] stringArray, boolean caseInsensetive) { for (int i = 0; i < stringArray.length; i++) { if (caseInsensetive) { if (strSearch.toLowerCase().equals(stringArray[i].toLowerCase())) { return i; } } else { if (strSearch.equals(stringArray[i])) { return i; } } } return -1; } /** * ? * * @param strSearch * * @param stringArray * * @return ?, ?-1 */ public static int getInArrayID(String strSearch, String[] stringArray) { return getInArrayID(strSearch, stringArray, true); } /** * ? * * @param str * * @param stringArray * * @param caseInsensetive * ???, true?, false * @return */ public static boolean inArray(String str, String[] stringArray, boolean caseInsensetive) { return getInArrayID(str, stringArray, caseInsensetive) >= 0; } /** * ? * * @param str * * @param stringArray * * @return */ public static boolean inArray(String str, String[] stringArray) { return inArray(str, stringArray, false); } /** * ? * * @param str * * @param stringArray * ??? * @param strsplit * * @param caseInsensetive * ???, true?, false * @return */ public static boolean inArray(String str, String stringArray, String strsplit, boolean caseInsensetive) { return inArray(str, stringArray.split(strsplit), caseInsensetive); } /** * ? * * @param str * * @param stringArray * ??? * @param strsplit * * @return */ public static boolean inArray(String str, String stringArray, String strsplit) { return inArray(str, stringArray.split(strsplit), false); } /** * ? * * @param str * * @param stringArray * ??? * @return */ public static boolean inArray(String str, String stringArray) { return inArray(str, stringArray.split(","), false); } /** * ?Sql? * * @param str * ? * @return */ public static boolean isSafeSqlString(String str) { Pattern pattern = Pattern.compile("[-|;|,|/|(|)|\\[|\\]|\\}|\\{|%|@|\\*|!|\\']"); return !pattern.matcher(str).find(); } /** * ??? * * @param str * ? * @return */ public static boolean isSafeUserInfoString(String str) { String es = "^\\s*$|^c:\\con\\con$|[%,\\*\\\\s\\t\\<\\>\\&]||^Guest"; System.out.println("es = " + es); Pattern pattern = Pattern.compile(es); return !pattern.matcher(str).find(); } /** * ? * * @param filePath * ?? * @return ? */ public static boolean fileExists(String filePath) { File file = new File(filePath); return file.exists(); } /** * ??html? * * @param spacesCount * @return */ public static String getSpacesString(int spacesCount) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < spacesCount; i++) { sb.append(" "); } return sb.toString(); } /** * ?Color * * @param color * @return */ public static Color toColor(String color) { int red, green, blue = 0; char[] rgb; color = "#" + color.trim(); color = color.toLowerCase().replaceAll("[g-zG-Z]", ""); switch (color.length()) { case 3: rgb = color.toCharArray(); red = SysUtils.null2Int(rgb[0] + "" + rgb[0], 16); green = SysUtils.null2Int(rgb[1] + "" + rgb[1], 16); blue = SysUtils.null2Int(rgb[2] + "" + rgb[2], 16); return new Color(red, green, blue); case 6: rgb = color.toCharArray(); red = SysUtils.null2Int(rgb[0] + "" + rgb[1], 16); green = SysUtils.null2Int(rgb[2] + "" + rgb[3], 16); blue = SysUtils.null2Int(rgb[4] + "" + rgb[5], 16); return new Color(red, green, blue); default: return Color.decode(color); } } /** * ? * * @param str * @return */ public static String replaceStrToScript(String str) { str = str.replace("\\", "\\\\"); str = str.replace("'", "\\'"); str = str.replace("\"", "\\\""); return str; } /** * ??????? * * @param filename * ?? * @return ?? */ public static boolean isImgFilename(String filename) { filename = filename.trim(); if (filename.endsWith(".") || filename.indexOf(".") == -1) { return false; } String extname = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase(); return (extname.equals("jpg") || extname.equals("jpeg") || extname.equals("png") || extname.equals("bmp") || extname.equals("gif")); } /** * ?IP * * @param ip * @return */ public static boolean isIPSect(String ip) { return Pattern.compile( "^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){2}((2[0-4]\\d|25[0-5]|[01]?\\d\\d?|\\*)\\.)(2[0-4]\\d|25[0-5]|[01]?\\d\\d?|\\*)$") .matcher(ip).find(); } /** * ? * * @param timeval * @return */ public static boolean isTime(String timeval) { return Pattern.compile("^((([0-1]?[0-9])|(2[0-3])):([0-5]?[0-9])(:[0-5]?[0-9])?)$").matcher(timeval).find(); } public static boolean isRuleTip(Map<String, String> newHash, String ruletype, Map<Integer, String> keyMap) { keyMap.put(0, ""); ruletype = ruletype.trim().toLowerCase(); Set<Map.Entry<String, String>> entrys = newHash.entrySet(); for (Map.Entry<String, String> entry : entrys) { String[] single = entry.getValue().split("\r\n"); for (String str : single) { try { if (!str.equals("")) { if (ruletype.equals("email")) { if (!checkEmail(str)) { throw new Exception(); } } else if (ruletype.equals("ip")) { if (!isIPSect(str)) { throw new Exception(); } } else if (ruletype.equals("timesect")) { String[] splitetime = str.split("-"); if (!isTime(splitetime[1]) || !isTime(splitetime[0])) { throw new Exception(); } } } } catch (Exception e) { keyMap.put(0, entry.getKey()); return false; } } } return true; } public static boolean mkdir(String mkdirName) { try { File dirFile = new File(mkdirName); boolean bFile = dirFile.exists(); if (bFile) { return true; } else { bFile = dirFile.mkdir(); return bFile; } } catch (Exception err) { return false; } } /** * ??? * * @param strEmail * @return */ public static String getEmailHostName(String strEmail) { if (strEmail.indexOf("@") < 0) { return ""; } return strEmail.substring(strEmail.lastIndexOf("@")).toLowerCase(); } /** * HTML * * @param value * @return */ public static String dhtmlspecialchars(String value) { if (matches(value, "(&|\"|<|>)")) { value = value.replaceAll("&", "&"); value = value.replaceAll("\"", """); value = value.replaceAll("<", "<"); value = value.replaceAll(">", ">"); } return value; } /** * HTML?? * * @param value * @return */ public static String htmlspecialchars(String value) { if (matches(value, "(&|"|<|>)")) { value = value.replaceAll("&", "&"); value = value.replaceAll(""", "\""); value = value.replaceAll("<", "<"); value = value.replaceAll(">", ">"); } return value; } public static boolean matches(String content, String regex) { boolean flag = false; try { flag = new Perl5Matcher().contains(content, new Perl5Compiler().compile(regex)); } catch (MalformedPatternException e) { } return flag; } public static String combinatorial(String str) { if (null == str) return ""; str = str.trim().replaceAll(" ", ","); char ch = ' '; StringBuffer result = new StringBuffer(); for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ',') { if (ch != ',') result.append(str.charAt(i)); } else { result.append(str.charAt(i)); } ch = str.charAt(i); } return result.toString(); } public static String subStringCN(String s, int len) { if (s.toCharArray().length <= len) return s; int i = 0; for (float k = 0; k < len && i < s.length(); i++) { if (s.charAt(i) > '') { k += 1; } else { k += 0.5; } } if (i < s.length()) { s = s.substring(0, i - 1); } return s; // char[] chars = str.toCharArray(); // StringBuffer result = new StringBuffer(); // if (chars.length > len) { // for (int i = 0; i < len; i++) { // result.append(chars[i]); // } // result.append("..."); // return result.toString(); // } else { // return str; // } } public static void copy(File src, File dst) { try { InputStream in = null; OutputStream out = null; int byteread = 0; try { in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE); byte[] buffer = new byte[BUFFER_SIZE]; while ((byteread = in.read(buffer)) != -1) { out.write(buffer, 0, byteread); } // // byte[] buffer = new byte[bs]; // while (in.read(buffer) > 0) { // out.write(buffer); // buffer = new byte[BUFFER_SIZE]; // } } finally { if (null != in) { in.close(); } if (null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } public static String decode(String s) { try { return URLDecoder.decode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return s; } public static String getIpStringFromBytes(byte[] ip) { StringBuffer sb = new StringBuffer(); sb.append(ip[0] & 0xFF); sb.append('.'); sb.append(ip[1] & 0xFF); sb.append('.'); sb.append(ip[2] & 0xFF); sb.append('.'); sb.append(ip[3] & 0xFF); return sb.toString(); } public static byte[] getIpByteArrayFromString(String ip) { byte[] ret = new byte[4]; java.util.StringTokenizer st = new java.util.StringTokenizer(ip, "."); try { ret[0] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); ret[1] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); ret[2] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); ret[3] = (byte) (Integer.parseInt(st.nextToken()) & 0xFF); } catch (Exception e) { } return ret; } public static String getString(String s, String srcEncoding, String destEncoding) { try { return new String(s.getBytes(srcEncoding), destEncoding); } catch (UnsupportedEncodingException e) { return s; } } public static String getString(byte[] b, String encoding) { try { return new String(b, encoding); } catch (UnsupportedEncodingException e) { return new String(b); } } public static String getString(byte[] b, int offset, int len, String encoding) { try { return new String(b, offset, len, encoding); } catch (UnsupportedEncodingException e) { return new String(b, offset, len); } } public static String composeString(List<String> strs, String fix) { if (null == strs || null == fix) return ""; StringBuffer result = new StringBuffer(); for (String str : strs) { result.append(str).append(fix); } return result.toString(); } public static String makeFileName(String rootpath, String dir, String fileName) { String path = fileName; File dirFile = new File(rootpath + dir + path); int i = 1; while (dirFile.exists()) { if (fileName.lastIndexOf(".") == 0) { path = "[" + i++ + "]" + fileName; } else if (fileName.lastIndexOf(".") == -1) { path = fileName + "[" + i++ + "]"; } else { path = fileName.substring(0, fileName.lastIndexOf(".")) + "[" + i++ + "]" + fileName.substring(fileName.lastIndexOf(".")); } dirFile = new File(rootpath + dir + path); } return dir + path; // To change body of created methods use File | // Settings | File Templates. } public static Date getDay0(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } public static Date getDay2359(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } public static Date getMonth1D() { return getMonth1D(new Date()); } public static Date getMonth1D(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); return calendar.getTime(); } public static Date getMonthLastD() { return getMonthLastD(new Date()); } public static Date getMonthLastD(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.set(Calendar.DAY_OF_MONTH, 1); calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.SECOND, -1); return calendar.getTime(); } public static String[] toStringArray(String str) { char[] chrs = str.toCharArray(); String[] result = new String[chrs.length]; for (int i = 0; i < chrs.length; i++) { char[] tmp = new char[] { chrs[i] }; result[i] = new String(tmp); } return result; } /** * 134-?135-?136-?137-?138-?139-?150-?151-?152-?158-?159-?147-?187-?188- * 182- * <p/> * 157 - * * @param mobilephone * @return */ public static boolean checkMobilephoneByCMCC(String mobilephone) { String cmcc = new String("134,135,136,137,138,139,150,151,152,157,158,159,188,182,187,147,"); try { if (mobilephone.length() != 11) return false; String sub = mobilephone.substring(0, 3) + ","; if (cmcc.indexOf(sub) == -1) return false; @SuppressWarnings("unused") float m = Float.parseFloat(mobilephone); return isIntArray(toStringArray(mobilephone)); } catch (Exception e) { return false; } } @SuppressWarnings("unused") public static boolean checkMobilephone(String mobilephone) { String cmcc = new String("13,15,18,"); try { if (mobilephone.length() != 11) return false; String sub = mobilephone.substring(0, 2) + ","; if (cmcc.indexOf(sub) == -1) return false; float m = Float.parseFloat(mobilephone); return isIntArray(toStringArray(mobilephone)); } catch (Exception e) { return false; } } /** * * * @param hour * * @return */ public static String getAccDate(int hour) { Calendar calendar = Calendar.getInstance(); if (calendar.get(Calendar.HOUR_OF_DAY) >= hour) { calendar.add(Calendar.DAY_OF_YEAR, 1); } return dateFormat(calendar.getTime(), "yyyyMMdd"); } public static String getAccDateShort(int hour) { Calendar calendar = Calendar.getInstance(); if (calendar.get(Calendar.HOUR_OF_DAY) >= hour) { calendar.add(Calendar.DAY_OF_YEAR, 1); } return dateFormat(calendar.getTime(), "yyMMdd"); } public static String fillzero(String str, int len) { if (str == null) { return ""; } if (str.length() > len) { Throwable throwable = new Throwable("?,!"); throwable.printStackTrace(); } while (str.length() < len) { str = "0" + str; } return str; } public static byte[] str2Bcd(String asc) { int len = asc.length(); int mod = len % 2; if (mod != 0) { asc = "0" + asc; len = asc.length(); } byte abt[] = new byte[len]; if (len >= 2) { len = len / 2; } byte bbt[] = new byte[len]; abt = asc.getBytes(); int j, k; for (int p = 0; p < asc.length() / 2; p++) { if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) { j = abt[2 * p] - '0'; } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) { j = abt[2 * p] - 'a' + 0x0a; } else { j = abt[2 * p] - 'A' + 0x0a; } if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) { k = abt[2 * p + 1] - '0'; } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) { k = abt[2 * p + 1] - 'a' + 0x0a; } else { k = abt[2 * p + 1] - 'A' + 0x0a; } int a = (j << 4) + k; byte b = (byte) a; bbt[p] = b; } return bbt; } public static String bcdToAsc(byte[] bcdBytes) { char[] hex = new char[] { 'A', 'B', 'C', 'D', 'E', 'F' }; StringBuffer temp = new StringBuffer(bcdBytes.length * 2); for (int i = 0; i < bcdBytes.length; i++) { byte hi = (byte) ((bcdBytes[i] & 0xf0) >>> 4); if (hi >= 0xA) { temp.append(hex[hi - 0xA]); } else { temp.append(hi); } byte lo = (byte) (bcdBytes[i] & 0x0f); if (lo >= 0xA) { temp.append(hex[lo - 0xA]); } else { temp.append(lo); } } return temp.toString().substring(0, 1).equalsIgnoreCase("0") ? temp.toString().substring(1) : temp.toString(); } /** * json?. * * @param str * json? * @return */ public static String jsonStrConvert(String str) { if (StringUtils.isEmpty(str)) { return null; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); switch (c) { case '\"': sb.append("\\\""); break; case '\\': sb.append("\\\\"); break; case '/': sb.append("\\/"); break; case '\b': sb.append("\\b"); break; case '\f': sb.append("\\f"); break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; default: sb.append(c); } } return sb.toString(); } /** * Blobbyte[] * * @param blob * @return * @author &Eryan eryanwcp@gmail.com */ public static byte[] blobToBytes(Blob blob) { BufferedInputStream is = null; try { is = new BufferedInputStream(blob.getBinaryStream()); byte[] bytes = new byte[(int) blob.length()]; int len = bytes.length; int offset = 0; int read = 0; while (offset < len && (read = is.read(bytes, offset, len - offset)) >= 0) { offset += read; } return bytes; } catch (Exception e) { return null; } finally { try { is.close(); is = null; } catch (IOException e) { return null; } } } /** * ??? * @param hex * @return */ public static final byte[] decodeHex(String hex) { char chars[] = hex.toCharArray(); byte bytes[] = new byte[chars.length / 2]; int byteCount = 0; for (int i = 0; i < chars.length; i += 2) { int newByte = 0; newByte |= hexCharToByte(chars[i]); newByte <<= 4; newByte |= hexCharToByte(chars[i + 1]); bytes[byteCount] = (byte) newByte; byteCount++; } return bytes; } /** * ??? * @param bytes * @return */ public static final String encodeHex(byte bytes[]) { StringBuffer buf = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { if ((bytes[i] & 0xff) < 16) buf.append("0"); buf.append(Long.toString(bytes[i] & 0xff, 16)); } return buf.toString(); } private static final byte hexCharToByte(char ch) { switch (ch) { case 48: // '0' return 0; case 49: // '1' return 1; case 50: // '2' return 2; case 51: // '3' return 3; case 52: // '4' return 4; case 53: // '5' return 5; case 54: // '6' return 6; case 55: // '7' return 7; case 56: // '8' return 8; case 57: // '9' return 9; case 97: // 'a' return 10; case 98: // 'b' return 11; case 99: // 'c' return 12; case 100: // 'd' return 13; case 101: // 'e' return 14; case 102: // 'f' return 15; case 58: // ':' case 59: // ';' case 60: // '<' case 61: // '=' case 62: // '>' case 63: // '?' case 64: // '@' case 65: // 'A' case 66: // 'B' case 67: // 'C' case 68: // 'D' case 69: // 'E' case 70: // 'F' case 71: // 'G' case 72: // 'H' case 73: // 'I' case 74: // 'J' case 75: // 'K' case 76: // 'L' case 77: // 'M' case 78: // 'N' case 79: // 'O' case 80: // 'P' case 81: // 'Q' case 82: // 'R' case 83: // 'S' case 84: // 'T' case 85: // 'U' case 86: // 'V' case 87: // 'W' case 88: // 'X' case 89: // 'Y' case 90: // 'Z' case 91: // '[' case 92: // '\\' case 93: // ']' case 94: // '^' case 95: // '_' case 96: // '`' default: return 0; } } }