file_id
stringlengths
3
9
content
stringlengths
24
53.5k
repo
stringlengths
6
89
path
stringlengths
5
169
token_length
int64
18
8.19k
original_comment
stringlengths
5
9.64k
comment_type
stringclasses
2 values
detected_lang
stringclasses
1 value
masked_comment
stringlengths
11
53.4k
2_28
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
Tencent/APIJSON
APIJSONORM/src/main/java/apijson/StringUtil.java
7,931
//判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
line_comment
zh-cn
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断 <SUF> /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
2_36
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
Tencent/APIJSON
APIJSONORM/src/main/java/apijson/StringUtil.java
7,931
//判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
line_comment
zh-cn
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断 <SUF> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
2_45
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
Tencent/APIJSON
APIJSONORM/src/main/java/apijson/StringUtil.java
7,931
//判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
line_comment
zh-cn
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断 <SUF> public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
2_47
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
Tencent/APIJSON
APIJSONORM/src/main/java/apijson/StringUtil.java
7,931
//已用55个中英字符测试通过
line_comment
zh-cn
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用 <SUF> //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
2_65
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
Tencent/APIJSON
APIJSONORM/src/main/java/apijson/StringUtil.java
7,931
//判断字符类型 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
line_comment
zh-cn
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. This source code is licensed under the Apache License Version 2.0.*/ package apijson; import java.io.File; import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.Objects; import java.util.regex.Pattern; /**通用字符串(String)相关类,为null时返回"" * @author Lemon * @use StringUtil. */ public class StringUtil { private static final String TAG = "StringUtil"; public StringUtil() { } public static final String UTF_8 = "utf-8"; public static final String EMPTY = "无"; public static final String UNKNOWN = "未知"; public static final String UNLIMITED = "不限"; public static final String I = "我"; public static final String YOU = "你"; public static final String HE = "他"; public static final String SHE = "她"; public static final String IT = "它"; public static final String MALE = "男"; public static final String FEMALE = "女"; public static final String TODO = "未完成"; public static final String DONE = "已完成"; public static final String FAIL = "失败"; public static final String SUCCESS = "成功"; public static final String SUNDAY = "日"; public static final String MONDAY = "一"; public static final String TUESDAY = "二"; public static final String WEDNESDAY = "三"; public static final String THURSDAY = "四"; public static final String FRIDAY = "五"; public static final String SATURDAY = "六"; public static final String YUAN = "元"; private static String currentString = ""; /**获取刚传入处理后的string * @must 上个影响currentString的方法 和 这个方法都应该在同一线程中,否则返回值可能不对 * @return */ public static String getCurrentString() { return currentString == null ? "" : currentString; } //获取string,为null时返回"" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string,为null则返回"" * @param object * @return */ public static String getString(Object object) { return object == null ? "" : object.toString(); } /**获取string,为null则返回"" * @param cs * @return */ public static String getString(CharSequence cs) { return cs == null ? "" : cs.toString(); } /**获取string,为null则返回"" * @param s * @return */ public static String getString(String s) { return s == null ? "" : s; } /**获取string,为null则返回"" * ignoreEmptyItem = false; * split = "," * @param array * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array) { return getString(array, false); } /**获取string,为null则返回"" * split = "," * @param array * @param ignoreEmptyItem * @return {@link #getString(Object[], boolean)} */ public static String getString(Object[] array, boolean ignoreEmptyItem) { return getString(array, null, ignoreEmptyItem); } /**获取string,为null则返回"" * ignoreEmptyItem = false; * @param array * @param split * @return {@link #getString(Object[], String, boolean)} */ public static String getString(Object[] array, String split) { return getString(array, split, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取string,为null则返回"" * @param array -the str array given * @param split -the token used to split * @param ignoreEmptyItem -whether to ignore empty item or not * @return {@link #getString(Object[], String, boolean)} * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getString(Object[] array, String split, boolean ignoreEmptyItem) { StringBuilder s = new StringBuilder(""); if (array != null) { if (split == null) { split = ","; } for (int i = 0; i < array.length; i++) { if (ignoreEmptyItem && isEmpty(array[i], true)) { continue; } s.append(((i > 0 ? split : "") + array[i])); } } return getString(s.toString()); } //获取string,为null时返回"" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉前后空格后的string<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉前后空格后的string,为null则返回"" * @param object * @return */ public static String getTrimedString(Object object) { return getTrimedString(getString(object)); } /**获取去掉前后空格后的string,为null则返回"" * @param cs * @return */ public static String getTrimedString(CharSequence cs) { return getTrimedString(getString(cs)); } /**获取去掉前后空格后的string,为null则返回"" * @param s * @return */ public static String getTrimedString(String s) { return getString(s).trim(); } //获取去掉前后空格后的string>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取去掉所有空格后的string <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取去掉所有空格后的string,为null则返回"" * @param object * @return */ public static String getNoBlankString(Object object) { return getNoBlankString(getString(object)); } /**获取去掉所有空格后的string,为null则返回"" * @param cs * @return */ public static String getNoBlankString(CharSequence cs) { return getNoBlankString(getString(cs)); } /**获取去掉所有空格后的string,为null则返回"" * @param s * @return */ public static String getNoBlankString(String s) { return getString(s).replaceAll("\\s", ""); } //获取去掉所有空格后的string >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //获取string的长度<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取string的长度,为null则返回0 * @param object * @param trim * @return */ public static int getLength(Object object, boolean trim) { return getLength(getString(object), trim); } /**获取string的长度,为null则返回0 * @param cs * @param trim * @return */ public static int getLength(CharSequence cs, boolean trim) { return getLength(getString(cs), trim); } /**获取string的长度,为null则返回0 * @param s * @param trim * @return */ public static int getLength(String s, boolean trim) { s = trim ? getTrimedString(s) : s; return getString(s).length(); } //获取string的长度>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否为空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否为空 trim = true * @param obj * @return */ public static boolean isEmpty(Object obj) { return isEmpty(obj, true); } /**判断字符是否为空 * @param obj * @param trim * @return */ public static boolean isEmpty(Object obj, boolean trim) { return isEmpty(getString(obj), trim); } /**判断字符是否为空 trim = true * @param cs * @return */ public static boolean isEmpty(CharSequence cs) { return isEmpty(cs, true); } /**判断字符是否为空 * @param cs * @param trim * @return */ public static boolean isEmpty(CharSequence cs, boolean trim) { return isEmpty(getString(cs), trim); } /**判断字符是否为空 trim = true * @param s * @return */ public static boolean isEmpty(String s) { return isEmpty(s, true); } /**判断字符是否为空 * @param s * @param trim * @return */ public static boolean isEmpty(String s, boolean trim) { // Log.i(TAG, "getTrimedString s = " + s); if (s == null) { return true; } if (trim) { s = s.trim(); } if (s.isEmpty()) { return true; } currentString = s; return false; } //判断字符是否为空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符是否非空 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**判断字符是否非空 trim = true * @param object * @return */ public static boolean isNotEmpty(Object obj) { return ! isEmpty(obj); } /**判断字符是否非空 * @param obj * @param trim * @return */ public static boolean isNotEmpty(Object obj, boolean trim) { return ! isEmpty(obj, trim); } /**判断字符是否非空 trim = true * @param cs * @return */ public static boolean isNotEmpty(CharSequence cs) { return ! isEmpty(cs); } /**判断字符是否非空 * @param cs * @param trim * @return */ public static boolean isNotEmpty(CharSequence cs, boolean trim) { return ! isEmpty(cs, trim); } /**判断字符是否非空 trim = true * @param s * @return */ public static boolean isNotEmpty(String s) { return ! isEmpty(s); } /**判断字符是否非空 * @param s * @param trim * @return */ public static boolean isNotEmpty(String s, boolean trim) { return ! isEmpty(s, trim); } //判断字符是否非空 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //判断字符类型 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< public static final Pattern PATTERN_NUMBER; public static final Pattern PATTERN_PHONE; public static final Pattern PATTERN_EMAIL; public static final Pattern PATTERN_ID_CARD; public static final Pattern PATTERN_ALPHA; public static final Pattern PATTERN_PASSWORD; //TODO public static final Pattern PATTERN_NAME; public static final Pattern PATTERN_ALPHA_BIG; public static final Pattern PATTERN_ALPHA_SMALL; public static final Pattern PATTERN_BRANCH_URL; static { PATTERN_NUMBER = Pattern.compile("^[0-9]+$"); PATTERN_ALPHA = Pattern.compile("^[a-zA-Z]+$"); PATTERN_ALPHA_BIG = Pattern.compile("^[A-Z]+$"); PATTERN_ALPHA_SMALL = Pattern.compile("^[a-z]+$"); PATTERN_NAME = Pattern.compile("^[0-9a-zA-Z_.:]+$");//已用55个中英字符测试通过 //newest phone regex expression reference https://github.com/VincentSit/ChinaMobilePhoneNumberRegex PATTERN_PHONE = Pattern.compile("^1(?:3\\d{3}|5[^4\\D]\\d{2}|8\\d{3}|7(?:[0-35-9]\\d{2}|4(?:0\\d|1[0-2]|9\\d))|9[0-35-9]\\d{2}|6[2567]\\d{2}|4(?:(?:10|4[01])\\d{3}|[68]\\d{4}|[579]\\d{2}))\\d{6}$"); PATTERN_EMAIL = Pattern.compile("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"); PATTERN_ID_CARD = Pattern.compile("(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)"); PATTERN_PASSWORD = Pattern.compile("^[0-9a-zA-Z]+$"); PATTERN_BRANCH_URL = Pattern.compile("^[0-9a-zA-Z-_/]+$"); } /**判断手机格式是否正确 * @param phone * @return */ public static boolean isPhone(String phone) { if (isNotEmpty(phone, true) == false) { return false; } currentString = phone; return PATTERN_PHONE.matcher(phone).matches(); } /**判断手机格式是否正确 * @param s * @return */ public static boolean isPassword(String s) { return getLength(s, false) >= 6 && PATTERN_PASSWORD.matcher(s).matches(); } /**判断是否全是数字密码 * @param s * @return */ public static boolean isNumberPassword(String s) { return getLength(s, false) == 6 && isNumer(s); } /**判断email格式是否正确 * @param email * @return */ public static boolean isEmail(String email) { if (isEmpty(email, true)) { return false; } currentString = email; return PATTERN_EMAIL.matcher(email).matches(); } /**判断是否全是验证码 * @param s * @return */ public static boolean isVerify(String s) { return getLength(s, false) >= 4 && isNumer(s); } /**判断是否全是数字 * @param s * @return */ public static boolean isNumer(String s) { if (isNotEmpty(s, true) == false) { return false; } currentString = s; return PATTERN_NUMBER.matcher(s).matches(); } /**判断是否全是字母 * @param s * @return */ public static boolean isAlpha(String s) { if (isEmpty(s, true)) { return false; } currentString = s; return PATTERN_ALPHA.matcher(s).matches(); } /**判断是否全是数字或字母 * @param s * @return */ public static boolean isNumberOrAlpha(String s) { return isNumer(s) || isAlpha(s); } /**判断是否为代码名称,只能包含字母,数字或下划线 * @param s * @return */ public static boolean isName(String s) { if (s == null || s.isEmpty()) { return false; } String first = s.substring(0, 1); if ("_".equals(first) == false && PATTERN_ALPHA.matcher(first).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母大写的代码名称 * @param s * @return */ public static boolean isBigName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_BIG.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断是否为首字母小写的代码名称 * @param s * @return */ public static boolean isSmallName(String s) { if (s == null || s.isEmpty() || PATTERN_ALPHA_SMALL.matcher(s.substring(0, 1)).matches() == false) { return false; } return s.length() <= 1 ? true : PATTERN_NAME.matcher(s.substring(1)).matches(); } /**判断字符类型是否是身份证号 * @param number * @return */ public static boolean isIDCard(String number) { if (isNumberOrAlpha(number) == false) { return false; } number = getString(number); if (number.length() == 15) { Log.i(TAG, "isIDCard number.length() == 15 old IDCard"); currentString = number; return true; } if (number.length() == 18) { currentString = number; return true; } return false; } public static final String HTTP = "http"; public static final String URL_PREFIX = "http://"; public static final String URL_PREFIXs = "https://"; public static final String URL_STAFFIX = URL_PREFIX; public static final String URL_STAFFIXs = URL_PREFIXs; /**判断字符类型是否是网址 * @param url * @return */ public static boolean isUrl(String url) { if (isNotEmpty(url, true) == false) { return false; } if (! url.startsWith(URL_PREFIX) && ! url.startsWith(URL_PREFIXs)) { return false; } currentString = url; return true; } public static boolean isBranchUrl(String branchUrl) { if (isEmpty(branchUrl, false)) { return false; } return PATTERN_BRANCH_URL.matcher(branchUrl).matches(); } public static final String FILE_PATH_PREFIX = "file://"; /**判断文件路径是否存在 * @param path * @return */ public static boolean isFilePathExist(String path) { return StringUtil.isFilePath(path) && new File(path).exists(); } public static final String SEPARATOR = "/"; /**判断是否为路径 * @param path * @return */ public static boolean isPath(String path) { return StringUtil.isNotEmpty(path, true) && path.contains(SEPARATOR) && path.contains(SEPARATOR + SEPARATOR) == false && path.endsWith(SEPARATOR) == false; } /**判断字符类型是否是路径 * @param path * @return */ public static boolean isFilePath(String path) { if (isNotEmpty(path, true) == false) { return false; } if (! path.contains(".") || path.endsWith(".")) { return false; } currentString = path; return true; } //判断 <SUF> //提取特殊字符<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**去掉string内所有非数字类型字符 * @param object * @return */ public static String getNumber(Object object) { return getNumber(getString(object)); } /**去掉string内所有非数字类型字符 * @param cs * @return */ public static String getNumber(CharSequence cs) { return getNumber(getString(cs)); } /**去掉string内所有非数字类型字符 * @param s * @return */ public static String getNumber(String s) { return getNumber(s, false); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**去掉string内所有非数字类型字符 * @param s -string passed in * @param onlyStart 中间有非数字时只获取前面的数字 * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getNumber(String s, boolean onlyStart) { if (isNotEmpty(s, true) == false) { return ""; } StringBuilder numberString = new StringBuilder(""); String single; for (int i = 0; i < s.length(); i++) { single = s.substring(i, i + 1); if (isNumer(single)) { numberString.append(single); } else { if (onlyStart) { return numberString.toString(); } } } return numberString.toString(); } //提取特殊字符>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //校正(自动补全等)字符串<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< /**获取网址,自动补全 * @param url * @return */ public static String getCorrectUrl(String url) { Log.i(TAG, "getCorrectUrl : \n" + url); if (isNotEmpty(url, true) == false) { return ""; } // if (! url.endsWith("/") && ! url.endsWith(".html")) { // url = url + "/"; // } if (isUrl(url) == false) { return URL_PREFIX + url; } return url; } /**获取去掉所有 空格 、"-" 、"+86" 后的phone * @param phone * @return */ public static String getCorrectPhone(String phone) { if (isNotEmpty(phone, true) == false) { return ""; } phone = getNoBlankString(phone); phone = phone.replaceAll("-", ""); if (phone.startsWith("+86")) { phone = phone.substring(3); } return phone; } /**获取邮箱,自动补全 * @param email * @return */ public static String getCorrectEmail(String email) { if (isNotEmpty(email, true) == false) { return ""; } email = getNoBlankString(email); if (isEmail(email) == false && ! email.endsWith(".com")) { email += ".com"; } return email; } public static final int PRICE_FORMAT_DEFAULT = 0; public static final int PRICE_FORMAT_PREFIX = 1; public static final int PRICE_FORMAT_SUFFIX = 2; public static final int PRICE_FORMAT_PREFIX_WITH_BLANK = 3; public static final int PRICE_FORMAT_SUFFIX_WITH_BLANK = 4; public static final String[] PRICE_FORMATS = { "", "¥", "元", "¥ ", " 元" }; /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(String price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } //CS304 Issue link: https://github.com/Tencent/APIJSON/issues/182 /**获取价格,保留两位小数 * @param price -price passed in * @param formatType 添加单位(元) * @return limit String * <p>Here we replace the simple "+" way of concatenating with Stringbuilder 's append</p> */ public static String getPrice(String price, int formatType) { if (isNotEmpty(price, true) == false) { return getPrice(0, formatType); } //单独写到getCorrectPrice? <<<<<<<<<<<<<<<<<<<<<< String correctPrice; StringBuilder correctPriceBuilder = new StringBuilder(""); String s; for (int i = 0; i < price.length(); i++) { s = price.substring(i, i + 1); if (".".equals(s) || isNumer(s)) { correctPriceBuilder.append(s); } } correctPrice = correctPriceBuilder.toString(); //单独写到getCorrectPrice? >>>>>>>>>>>>>>>>>>>>>> Log.i(TAG, "getPrice <<<<<<<<<<<<<<<<<< correctPrice = " + correctPrice); if (correctPrice.contains(".")) { // if (correctPrice.startsWith(".")) { // correctPrice = 0 + correctPrice; // } if (correctPrice.endsWith(".")) { correctPrice = correctPrice.replaceAll(".", ""); } } Log.i(TAG, "getPrice correctPrice = " + correctPrice + " >>>>>>>>>>>>>>>>"); return isNotEmpty(correctPrice, true) ? getPrice(new BigDecimal(0 + correctPrice), formatType) : getPrice(0, formatType); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(BigDecimal price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @return */ public static String getPrice(double price) { return getPrice(price, PRICE_FORMAT_DEFAULT); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(BigDecimal price, int formatType) { return getPrice(price == null ? 0 : price.doubleValue(), formatType); } /**获取价格,保留两位小数 * @param price * @param formatType 添加单位(元) * @return */ public static String getPrice(double price, int formatType) { String s = new DecimalFormat("#########0.00").format(price); switch (formatType) { case PRICE_FORMAT_PREFIX: return PRICE_FORMATS[PRICE_FORMAT_PREFIX] + s; case PRICE_FORMAT_SUFFIX: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX]; case PRICE_FORMAT_PREFIX_WITH_BLANK: return PRICE_FORMATS[PRICE_FORMAT_PREFIX_WITH_BLANK] + s; case PRICE_FORMAT_SUFFIX_WITH_BLANK: return s + PRICE_FORMATS[PRICE_FORMAT_SUFFIX_WITH_BLANK]; default: return s; } } public static String join(String[] arr) { return join(arr); } /** 数组以指定分隔s拼接 * @param arr * @param s * @return */ public static String join(String[] arr, String s) { if (s == null) { s = ","; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < arr.length; i++) { sb.append(arr[i]); if (i < arr.length-1) { sb.append(s); } } return sb.toString(); } /**分割路径 * @param path * @return */ public static String[] splitPath(String path) { if (StringUtil.isNotEmpty(path, true) == false) { return null; } return isPath(path) ? split(path, SEPARATOR) : new String[] {path}; } /**将s分割成String[] * @param s * @return */ public static String[] split(String s) { return split(s, null); } /**将s用split分割成String[] * trim = true; * @param s * @param split * @return */ public static String[] split(String s, String split) { return split(s, split, true); } /**将s用split分割成String[] * @param s * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, boolean trim) { return split(s, null, trim); } /**将s用split分割成String[] * @param s * @param split * @param trim 去掉前后两端的split * @return */ public static String[] split(String s, String split, boolean trim) { s = getString(s); if (s.isEmpty()) { return null; } if (isEmpty(split, false)) { split = ","; } if (trim) { while (s.startsWith(split)) { s = s.substring(split.length()); } while (s.endsWith(split)) { s = s.substring(0, s.length() - split.length()); } } return s.contains(split) ? s.split(split) : new String[]{s}; } /** * @param key * @param suffix * @return key + suffix,第一个字母小写 */ public static String addSuffix(String key, String suffix) { key = getNoBlankString(key); if (key.isEmpty()) { return firstCase(suffix); } return firstCase(key) + firstCase(suffix, true); } /** * @param key */ public static String firstCase(String key) { return firstCase(key, false); } /** * @param key * @param upper * @return */ public static String firstCase(String key, boolean upper) { key = getString(key); if (key.isEmpty()) { return ""; } String first = key.substring(0, 1); key = (upper ? first.toUpperCase() : first.toLowerCase()) + key.substring(1, key.length()); return key; } /**全部大写 * @param s * @return */ public static String toUpperCase(String s) { return toUpperCase(s, false); } /**全部大写 * @param s * @param trim * @return */ public static String toUpperCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toUpperCase(); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s) { return toLowerCase(s, false); } /**全部小写 * @param s * @return */ public static String toLowerCase(String s, boolean trim) { s = trim ? getTrimedString(s) : getString(s); return s.toLowerCase(); } public static String concat(String left, String right) { return concat(left, right, null); } public static String concat(String left, String right, String split) { return concat(left, right, split, true); } public static String concat(String left, String right, boolean trim) { return concat(left, right, null, trim); } public static String concat(String left, String right, String split, boolean trim) { if (isEmpty(left, trim)) { return right; } if (isEmpty(right, trim)) { return left; } if (split == null) { split = ","; } return left + split + right; } //校正(自动补全等)字符串>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> public static boolean equals(Object s1, Object s2) { return Objects.equals(s1, s2); } public static boolean equalsIgnoreCase(String s1, String s2) { if (s1 == s2) { return true; } if (s1 == null || s2 == null) { return false; } return s1.equalsIgnoreCase(s2); } }
3_1
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
/** * 判断两个输入流是否严格相等 */
block_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两 <SUF>*/ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_7
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 还可以写入的字节数
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可 <SUF> if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_8
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 减去
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 <SUF> else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_9
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 写够了
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够 <SUF> if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_11
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 啥都没写,强制触发一下写
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都 <SUF> // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_12
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 这是考虑到 walnut 的输出流实现,比如你写一个空文件
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是 <SUF> // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_13
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么 <SUF> // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_14
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人 <SUF> // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_15
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
// 所以这里触发一个写,它就知道,喔你要写个空喔。
line_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以 <SUF> if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_29
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
/** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */
block_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个 <SUF>*/ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_35
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
/** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */
block_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个 <SUF>*/ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_37
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
/** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */
block_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个 <SUF>*/ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_44
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
/** * 判断并移除UTF-8的BOM头 */
block_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并 <SUF>*/ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
3_50
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
nutzam/nutz
src/org/nutz/lang/Streams.java
5,467
/** * 获取File对象输入流,即使在Jar文件中一样工作良好!! <b>强烈推荐</b> * */
block_comment
zh-cn
package org.nutz.lang; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.Flushable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PushbackInputStream; import java.io.Reader; import java.io.Writer; import org.nutz.lang.stream.FileChannelInputStream; import org.nutz.lang.stream.FileChannelOutputStream; import org.nutz.lang.stream.VoidInputStream; import org.nutz.resource.NutResource; import org.nutz.resource.Scans; /** * 提供了一组创建 Reader/Writer/InputStream/OutputStream 的便利函数 * * @author zozoh(zozohtnt@gmail.com) * @author Wendal(wendal1985@gmail.com) * @author bonyfish(mc02cxj@gmail.com) */ public abstract class Streams { private static final int BUF_SIZE = 8192; /** * 判断两个输入流是否严格相等 */ public static boolean equals(InputStream sA, InputStream sB) throws IOException { int dA; while ((dA = sA.read()) != -1) { int dB = sB.read(); if (dA != dB) return false; } return sB.read() == -1; } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param writer * * @param cs * 文本 * @throws IOException */ public static void write(Writer writer, CharSequence cs) throws IOException { if (null != cs && null != writer) { writer.write(cs.toString()); writer.flush(); } } /** * 将一段文本全部写入一个writer。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param writer * 输出流 * @param cs * 文本 */ public static void writeAndClose(Writer writer, CharSequence cs) { try { write(writer, cs); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); } } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * * @return 写入的字节数 * @throws IOException */ public static long write(OutputStream ops, InputStream ins) throws IOException { return write(ops, ins, BUF_SIZE); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, int bufferSize) throws IOException { return write(ops, ins, -1, bufferSize); } /** * 将输入流写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @param limit * 最多写入多少字节,0 或负数表示不限 * @param bufferSize * 缓冲块大小 * * @return 写入的字节数 * * @throws IOException */ public static long write(OutputStream ops, InputStream ins, long limit, int bufferSize) throws IOException { if (null == ops || null == ins) return 0; byte[] buf = new byte[bufferSize]; int len; long bytesCount = 0; if (limit > 0) { long remain = limit; while (-1 != (len = ins.read(buf))) { // 还可以写入的字节数 if (len > remain) { len = (int) remain; remain = 0; } // 减去 else { remain -= len; } bytesCount += len; ops.write(buf, 0, len); // 写够了 if (remain <= 0) { break; } } } // 全写 else { while (-1 != (len = ins.read(buf))) { bytesCount += len; ops.write(buf, 0, len); } } // 啥都没写,强制触发一下写 // 这是考虑到 walnut 的输出流实现,比如你写一个空文件 // 那么输入流就是空的,但是 walnut 的包裹输出流并不知道你写过了 // 它人你就是打开一个输出流,然后再关上,所以自然不会对内容做改动 // 所以这里触发一个写,它就知道,喔你要写个空喔。 if (0 == bytesCount) { ops.write(buf, 0, 0); } ops.flush(); return bytesCount; } /** * 将输入流写入一个输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param ops * 输出流 * @param ins * 输入流 * @return 写入的字节数 */ public static long writeAndClose(OutputStream ops, InputStream ins) { try { return write(ops, ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它并不会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 * @throws IOException */ public static long write(Writer writer, Reader reader) throws IOException { if (null == writer || null == reader) return 0; char[] cbuf = new char[BUF_SIZE]; int len, count = 0; while (true) { len = reader.read(cbuf); if (len == -1) break; writer.write(cbuf, 0, len); count += len; } return count; } /** * 将文本输入流写入一个文本输出流。块大小为 8192 * <p> * <b style=color:red>注意</b>,它会关闭输入/出流 * * @param writer * 输出流 * @param reader * 输入流 */ public static long writeAndClose(Writer writer, Reader reader) { try { return write(writer, reader); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(writer); safeClose(writer); safeClose(reader); } } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 * @throws IOException */ public static void write(OutputStream ops, byte[] bytes) throws IOException { if (null == ops || null == bytes || bytes.length == 0) return; ops.write(bytes); } /** * 将一个字节数组写入一个输出流。 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param ops * 输出流 * @param bytes * 字节数组 */ public static void writeAndClose(OutputStream ops, byte[] bytes) { try { write(ops, bytes); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); } } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @return 文本内容 * @throws IOException */ public static StringBuilder read(Reader reader) throws IOException { StringBuilder sb = new StringBuilder(); read(reader, sb); return sb; } /** * 从一个文本流中读取全部内容并返回 * <p> * <b style=color:red>注意</b>,它会关闭输入流 * * @param reader * 文本输入流 * @return 文本内容 * @throws IOException */ public static String readAndClose(Reader reader) { try { return read(reader).toString(); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它并不会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 * @throws IOException */ public static int read(Reader reader, StringBuilder sb) throws IOException { char[] cbuf = new char[BUF_SIZE]; int count = 0; int len; while (-1 != (len = reader.read(cbuf))) { sb.append(cbuf, 0, len); count += len; } return count; } /** * 从一个文本流中读取全部内容并写入缓冲 * <p> * <b style=color:red>注意</b>,它会关闭输出流 * * @param reader * 文本输出流 * @param sb * 输出的文本缓冲 * @return 读取的字符数量 */ public static int readAndClose(InputStreamReader reader, StringBuilder sb) { try { return read(reader, sb); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(reader); } } /** * 读取一个输入流中所有的字节 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytes(InputStream ins) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); write(out, ins); return out.toByteArray(); } /** * 读取一个输入流中所有的字节,并关闭输入流 * * @param ins * 输入流,必须支持 available() * @return 一个字节数组 * @throws IOException */ public static byte[] readBytesAndClose(InputStream ins) { byte[] bytes = null; try { bytes = readBytes(ins); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { Streams.safeClose(ins); } return bytes; } /** * 关闭一个可关闭对象,可以接受 null。如果成功关闭,返回 true,发生异常 返回 false * * @param cb * 可关闭对象 * @return 是否成功关闭 */ public static boolean safeClose(Closeable cb) { if (null != cb) try { cb.close(); } catch (IOException e) { return false; } return true; } /** * 安全刷新一个可刷新的对象,可接受 null * * @param fa * 可刷新对象 */ public static void safeFlush(Flushable fa) { if (null != fa) try { fa.flush(); } catch (IOException e) {} } /** * 为一个输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param ins * 输入流。 * @return 缓冲输入流 */ public static BufferedInputStream buff(InputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); if (ins instanceof BufferedInputStream) return (BufferedInputStream) ins; // BufferedInputStream的构造方法,竟然是允许null参数的!! 我&$#^$&% return new BufferedInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输入流 * * @param f * 文件对象 * @return 管道文件数据流 * * @throws FileNotFoundException */ public static FileChannelInputStream chanIn(File f) throws FileNotFoundException { return chan(new FileInputStream(f)); } /** * 包裹采用 nio 方式更快速的文件输入流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelInputStream chan(FileInputStream ins) { if (ins == null) throw new NullPointerException("ins is null!"); return new FileChannelInputStream(ins); } /** * 创建采用 nio 方式更快速的文件输出流 * * @param f * 文件对象 * @param append * true 为末尾附加模式,false 表示从开头开始写 * * @return 管道文件数据流 * @throws FileNotFoundException */ public static FileChannelOutputStream chanOps(File f, boolean append) throws FileNotFoundException { return chan(new FileOutputStream(f, append)); } /** * 包裹采用 nio 方式更快速的文件输出流 * * @param ins * 文件输入流 * @return 管道文件数据流 */ public static FileChannelOutputStream chan(FileOutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); return new FileChannelOutputStream(ops); } /** * 为一个输出流包裹一个缓冲流。如果这个输出流本身就是缓冲流,则直接返回 * * @param ops * 输出流。 * @return 缓冲输出流 */ public static BufferedOutputStream buff(OutputStream ops) { if (ops == null) throw new NullPointerException("ops is null!"); if (ops instanceof BufferedOutputStream) return (BufferedOutputStream) ops; return new BufferedOutputStream(ops); } /** * 为一个文本输入流包裹一个缓冲流。如果这个输入流本身就是缓冲流,则直接返回 * * @param reader * 文本输入流。 * @return 缓冲文本输入流 */ public static BufferedReader buffr(Reader reader) { if (reader instanceof BufferedReader) return (BufferedReader) reader; return new BufferedReader(reader); } /** * 为一个文本输出流包裹一个缓冲流。如果这个文本输出流本身就是缓冲流,则直接返回 * * @param ops * 文本输出流。 * @return 缓冲文本输出流 */ public static BufferedWriter buffw(Writer ops) { if (ops instanceof BufferedWriter) return (BufferedWriter) ops; return new BufferedWriter(ops); } /** * 根据一个文件路径建立一个输入流 * * @param path * 文件路径 * @return 输入流 */ public static InputStream fileIn(String path) { InputStream ins = Files.findFileAsStream(path); if (null == ins) { File f = Files.findFile(path); if (null != f) try { ins = Streams._input(f); } catch (IOException e) {} } if (null == ins) { // TODO 考虑一下,应该抛异常呢?还是返回null呢? throw new RuntimeException(new FileNotFoundException(path)); // return null; } return buff(ins); } /** * 根据一个文件路径建立一个输入流 * * @param file * 文件 * @return 输入流 */ public static InputStream fileIn(File file) { try { return buff(Streams._input(file)); } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param path * 文件路径 * @return 文本输入流 */ public static Reader fileInr(String path) { return utf8r(fileIn(path)); } /** * 根据一个文件路径建立一个 UTF-8 文本输入流 <b>警告!! 本方法会预先读取3个字节以判断该文件是否存在BOM头</b> * <p/> * <b>警告!! 如果存在BOM头,则自动跳过</b> * <p/> * * @param file * 文件 * @return 文本输入流 */ public static Reader fileInr(File file) { return utf8r(fileIn(file)); } private static final byte[] UTF_BOM = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}; /** * 判断并移除UTF-8的BOM头 */ public static InputStream utf8filte(InputStream in) { try { if (in.available() == -1) return in; PushbackInputStream pis = new PushbackInputStream(in, 3); byte[] header = new byte[3]; int len = pis.read(header, 0, 3); if (len < 1) return in; if (header[0] != UTF_BOM[0] || header[1] != UTF_BOM[1] || header[2] != UTF_BOM[2]) { pis.unread(header, 0, len); } return pis; } catch (IOException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个输出流 * * @param path * 文件路径 * @return 输出流 */ public static OutputStream fileOut(String path) { return fileOut(Files.findFile(path)); } /** * 根据一个文件建立一个输出流 * * @param file * 文件 * @return 输出流 */ public static OutputStream fileOut(File file) { try { return buff(new FileOutputStream(file)); } catch (FileNotFoundException e) { throw Lang.wrapThrow(e); } } /** * 根据一个文件路径建立一个 UTF-8 文本输出流 * * @param path * 文件路径 * @return 文本输出流 */ public static Writer fileOutw(String path) { return fileOutw(Files.findFile(path)); } /** * 根据一个文件建立一个 UTF-8 文本输出流 * * @param file * 文件 * @return 输出流 */ public static Writer fileOutw(File file) { return utf8w(fileOut(file)); } public static Reader utf8r(InputStream is) { return new InputStreamReader(utf8filte(is), Encoding.CHARSET_UTF8); } public static Writer utf8w(OutputStream os) { return new OutputStreamWriter(os, Encoding.CHARSET_UTF8); } public static InputStream nullInputStream() { return new VoidInputStream(); } public static InputStream wrap(byte[] bytes) { return new ByteArrayInputStream(bytes); } /** * 对一个文本输入流迭代每一行,并将其关闭 * * @param r * 文本输入流 * @param callback * 回调 * @return 迭代的行数 */ public static int eachLine(Reader r, Each<String> callback) { if (null == callback || null == r) return 0; BufferedReader br = null; try { br = Streams.buffr(r); String line; int index = 0; while (null != (line = br.readLine())) { try { callback.invoke(index++, line, -1); } catch (ExitLoop e) { break; } catch (ContinueLoop e) { continue; } } return index; } catch (IOException e2) { throw Lang.wrapThrow(e2); } finally { Streams.safeClose(br); } } /** * 获取F <SUF>*/ protected static InputStream _input(File file) throws IOException { if (file.exists()) return new FileInputStream(file); if (Scans.isInJar(file)) { NutResource nutResource = Scans.makeJarNutResource(file); if (nutResource != null) return nutResource.getInputStream(); } throw new FileNotFoundException(file.toString()); } public static void appendWriteAndClose(File f, String text) { FileWriter fw = null; try { fw = new FileWriter(f, true); fw.write(text); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeClose(fw); } } public static String nextLineTrim(BufferedReader br) throws IOException { String line = null; while (br.ready()) { line = br.readLine(); if (line == null) break; if (Strings.isBlank(line)) continue; return line.trim(); } return line; } public static long writeAndClose(OutputStream ops, InputStream ins, int buf) { try { return write(ops, ins, buf); } catch (IOException e) { throw Lang.wrapThrow(e); } finally { safeFlush(ops); safeClose(ops); safeClose(ins); } } }
4_1
package stack; /** * 基于链表实现的栈。 * * Author: Zheng */ public class StackBasedOnLinkedList { private Node top = null; public void push(int value) { Node newNode = new Node(value, null); // 判断是否栈空 if (top == null) { top = newNode; } else { newNode.next = top; top = newNode; } } /** * 我用-1表示栈中没有数据。 */ public int pop() { if (top == null) return -1; int value = top.data; top = top.next; return value; } public void printAll() { Node p = top; while (p != null) { System.out.print(p.data + " "); p = p.next; } System.out.println(); } private static class Node { private int data; private Node next; public Node(int data, Node next) { this.data = data; this.next = next; } public int getData() { return data; } } }
wangzheng0822/algo
java/08_stack/StackBasedOnLinkedList.java
267
// 判断是否栈空
line_comment
zh-cn
package stack; /** * 基于链表实现的栈。 * * Author: Zheng */ public class StackBasedOnLinkedList { private Node top = null; public void push(int value) { Node newNode = new Node(value, null); // 判断 <SUF> if (top == null) { top = newNode; } else { newNode.next = top; top = newNode; } } /** * 我用-1表示栈中没有数据。 */ public int pop() { if (top == null) return -1; int value = top.data; top = top.next; return value; } public void printAll() { Node p = top; while (p != null) { System.out.print(p.data + " "); p = p.next; } System.out.println(); } private static class Node { private int data; private Node next; public Node(int data, Node next) { this.data = data; this.next = next; } public int getData() { return data; } } }
4_2
package stack; /** * 基于链表实现的栈。 * * Author: Zheng */ public class StackBasedOnLinkedList { private Node top = null; public void push(int value) { Node newNode = new Node(value, null); // 判断是否栈空 if (top == null) { top = newNode; } else { newNode.next = top; top = newNode; } } /** * 我用-1表示栈中没有数据。 */ public int pop() { if (top == null) return -1; int value = top.data; top = top.next; return value; } public void printAll() { Node p = top; while (p != null) { System.out.print(p.data + " "); p = p.next; } System.out.println(); } private static class Node { private int data; private Node next; public Node(int data, Node next) { this.data = data; this.next = next; } public int getData() { return data; } } }
wangzheng0822/algo
java/08_stack/StackBasedOnLinkedList.java
267
/** * 我用-1表示栈中没有数据。 */
block_comment
zh-cn
package stack; /** * 基于链表实现的栈。 * * Author: Zheng */ public class StackBasedOnLinkedList { private Node top = null; public void push(int value) { Node newNode = new Node(value, null); // 判断是否栈空 if (top == null) { top = newNode; } else { newNode.next = top; top = newNode; } } /** * 我用- <SUF>*/ public int pop() { if (top == null) return -1; int value = top.data; top = top.next; return value; } public void printAll() { Node p = top; while (p != null) { System.out.print(p.data + " "); p = p.next; } System.out.println(); } private static class Node { private int data; private Node next; public Node(int data, Node next) { this.data = data; this.next = next; } public int getData() { return data; } } }
5_2
package com.crossoverjie.guava.callback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Function: * * @author crossoverJie * Date: 2018/7/15 13:52 * @since JDK 1.8 */ public class Caller { private final static Logger LOGGER = LoggerFactory.getLogger(Caller.class); private CallBackListener callBackListener ; private Notifier notifier ; private String question ; /** * 使用 */ public void call(){ LOGGER.info("开始提问"); //新建线程,达到异步效果 new Thread(new Runnable() { @Override public void run() { try { notifier.execute(Caller.this,question); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); LOGGER.info("提问完毕,我去干其他事了"); } public Notifier getNotifier() { return notifier; } public void setNotifier(Notifier notifier) { this.notifier = notifier; } public CallBackListener getCallBackListener() { return callBackListener; } public void setCallBackListener(CallBackListener callBackListener) { this.callBackListener = callBackListener; } public String getQuestion() { return question; } public void setQuestion(String question) { this.question = question; } @Override public String toString() { return "Caller{" + "callBackListener=" + callBackListener + ", notifier=" + notifier + ", question='" + question + '\'' + '}'; } }
crossoverJie/JCSprout
src/main/java/com/crossoverjie/guava/callback/Caller.java
391
//新建线程,达到异步效果
line_comment
zh-cn
package com.crossoverjie.guava.callback; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Function: * * @author crossoverJie * Date: 2018/7/15 13:52 * @since JDK 1.8 */ public class Caller { private final static Logger LOGGER = LoggerFactory.getLogger(Caller.class); private CallBackListener callBackListener ; private Notifier notifier ; private String question ; /** * 使用 */ public void call(){ LOGGER.info("开始提问"); //新建 <SUF> new Thread(new Runnable() { @Override public void run() { try { notifier.execute(Caller.this,question); } catch (InterruptedException e) { e.printStackTrace(); } } }).start(); LOGGER.info("提问完毕,我去干其他事了"); } public Notifier getNotifier() { return notifier; } public void setNotifier(Notifier notifier) { this.notifier = notifier; } public CallBackListener getCallBackListener() { return callBackListener; } public void setCallBackListener(CallBackListener callBackListener) { this.callBackListener = callBackListener; } public String getQuestion() { return question; } public void setQuestion(String question) { this.question = question; } @Override public String toString() { return "Caller{" + "callBackListener=" + callBackListener + ", notifier=" + notifier + ", question='" + question + '\'' + '}'; } }
6_1
package org.yousharp.julycoding.string; /** * 1.2 字符串包含 * (july github链接:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.02.md) * * User: lingguo * Date: 14-6-29 */ public class StringContain { /** * * 问题描述: * 给定两个分别由字母组成的字符串A和字符串B,字符串B的长度比字符串A短。请问,如何最快地判断字符串B * 中所有字母是否都在字符串A里?为了简单起见,我们规定输入的字符串只包含大写英文字母。比如String A:ABCD,String B:BAD, * 返回true;string A:ABCD,string B:BCE,返回false;String A:ABCD,String B:AA,返回true。 * * 思路: * 思路一:遍历字符串B,判断每一个字符是否出现在字符串A中,时间复杂度O(n*m),空间复杂度O(1); * 思路二:先对两个字符串排序,然后同时遍历字符串A和B,判断B中的每一个字符是否都在字符串A中。时间复杂度O(nlogn),空间复杂度O(1); * 思路三:将每一个字符映射到一个素数上,对字符串A中的每一个字符表示的素数,求累积;然后遍历字符串B,用每一个字符表示的素 * 数去除字符串A的累积,判断余数是否为0。时间复杂度:O(n),空间复杂度O(1)。可能存在的问题:乘积时可能会溢出。 * 思路四:如果可以使用Java中的数据结构,HashMap和Set可以很方便地解决问题;如果不能,我们可以构造一个“签名”,将每一个字 * 符映射为整数(范围:0到26),然后遍历A中的每一个字符,将32位整数的对应位置1(整数初始为0),最后遍历B中的每一个字符,判断 * 每一个字符代表的整数在整数中是否已置位。时间复杂度O(n),空间复杂度O(1),思路四为最优算法。 * * 这里仅给出思路四的示例代码。 * * @param s1 * @param s2 * @return */ public static boolean hashCheck(char[] s1, char[] s2) { int mask = 0; for (char c: s1) { mask = mask | (1 << (c - 'A')); } for (char c: s2) { if ((mask & (1 << (c - 'A'))) == 0) { return false; } } return true; } }
julycoding/The-Art-Of-Programming-By-July-2nd
ebook/code/java/chapter1/1.2:字符串包含.java
686
/** * * 问题描述: * 给定两个分别由字母组成的字符串A和字符串B,字符串B的长度比字符串A短。请问,如何最快地判断字符串B * 中所有字母是否都在字符串A里?为了简单起见,我们规定输入的字符串只包含大写英文字母。比如String A:ABCD,String B:BAD, * 返回true;string A:ABCD,string B:BCE,返回false;String A:ABCD,String B:AA,返回true。 * * 思路: * 思路一:遍历字符串B,判断每一个字符是否出现在字符串A中,时间复杂度O(n*m),空间复杂度O(1); * 思路二:先对两个字符串排序,然后同时遍历字符串A和B,判断B中的每一个字符是否都在字符串A中。时间复杂度O(nlogn),空间复杂度O(1); * 思路三:将每一个字符映射到一个素数上,对字符串A中的每一个字符表示的素数,求累积;然后遍历字符串B,用每一个字符表示的素 * 数去除字符串A的累积,判断余数是否为0。时间复杂度:O(n),空间复杂度O(1)。可能存在的问题:乘积时可能会溢出。 * 思路四:如果可以使用Java中的数据结构,HashMap和Set可以很方便地解决问题;如果不能,我们可以构造一个“签名”,将每一个字 * 符映射为整数(范围:0到26),然后遍历A中的每一个字符,将32位整数的对应位置1(整数初始为0),最后遍历B中的每一个字符,判断 * 每一个字符代表的整数在整数中是否已置位。时间复杂度O(n),空间复杂度O(1),思路四为最优算法。 * * 这里仅给出思路四的示例代码。 * * @param s1 * @param s2 * @return */
block_comment
zh-cn
package org.yousharp.julycoding.string; /** * 1.2 字符串包含 * (july github链接:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.02.md) * * User: lingguo * Date: 14-6-29 */ public class StringContain { /** * * 问题描 <SUF>*/ public static boolean hashCheck(char[] s1, char[] s2) { int mask = 0; for (char c: s1) { mask = mask | (1 << (c - 'A')); } for (char c: s2) { if ((mask & (1 << (c - 'A'))) == 0) { return false; } } return true; } }
7_0
/** * DFA全称为:Deterministic Finite Automaton,即确定有穷自动机。<br> * 解释起来原理其实也不难,就是用所有关键字构造一棵树,然后用正文遍历这棵树,遍历到叶子节点即表示文章中存在这个关键字。<br> * 我们暂且忽略构建关键词树的时间,每次查找正文只需要O(n)复杂度就可以搞定。<br> * * @author looly * */ package cn.hutool.dfa;
dromara/hutool
hutool-dfa/src/main/java/cn/hutool/dfa/package-info.java
128
/** * DFA全称为:Deterministic Finite Automaton,即确定有穷自动机。<br> * 解释起来原理其实也不难,就是用所有关键字构造一棵树,然后用正文遍历这棵树,遍历到叶子节点即表示文章中存在这个关键字。<br> * 我们暂且忽略构建关键词树的时间,每次查找正文只需要O(n)复杂度就可以搞定。<br> * * @author looly * */
block_comment
zh-cn
/** * DFA <SUF>*/ package cn.hutool.dfa;
9_0
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
NLPchina/ansj_seg
src/main/java/org/ansj/util/AnsjReader.java
1,362
/** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */
block_comment
zh-cn
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽 <SUF>*/ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
9_4
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
NLPchina/ansj_seg
src/main/java/org/ansj/util/AnsjReader.java
1,362
/** * 为了功能的单一性我还是不实现了 */
block_comment
zh-cn
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功 <SUF>*/ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
9_5
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
NLPchina/ansj_seg
src/main/java/org/ansj/util/AnsjReader.java
1,362
/** * 读取一行数据。ps 读取结果会忽略 \n \r */
block_comment
zh-cn
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一 <SUF>*/ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
9_8
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
NLPchina/ansj_seg
src/main/java/org/ansj/util/AnsjReader.java
1,362
// 如果不是需要读状态,那么返回
line_comment
zh-cn
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果 <SUF> tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
9_9
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果是需要读状态那么读取 if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
NLPchina/ansj_seg
src/main/java/org/ansj/util/AnsjReader.java
1,362
// 如果是需要读状态那么读取
line_comment
zh-cn
package org.ansj.util; import java.io.IOException; import java.io.Reader; /** * 我又剽窃了下jdk...职业嫖客 为了效率这个流的操作是不支持多线程的,要么就是长时间不写这种东西了。发现好费劲啊 这个reader的特点。。只会输入 * 句子不会输出\r\n .会有一个start来记录当前返回字符串。起始偏移量 * * @author ansj * */ public class AnsjReader extends Reader { private Reader in; private char cb[]; private static int defaultCharBufferSize = 8192; /** * Creates a buffering character-input stream that uses an input buffer of * the specified size. * * @param in * A Reader * @param sz * Input-buffer size * * @exception IllegalArgumentException * If {@code sz <= 0} */ public AnsjReader(Reader in, int sz) { super(in); if (sz <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } this.in = in; cb = new char[sz]; } /** * Creates a buffering character-input stream that uses a default-sized * input buffer. * * @param in * A Reader */ public AnsjReader(Reader in) { this(in, defaultCharBufferSize); } /** Checks to make sure that the stream has not been closed */ private void ensureOpen() throws IOException { if (in == null) { throw new IOException("Stream closed"); } } /** * 为了功能的单一性我还是不实现了 */ @Override public int read(char cbuf[], int off, int len) throws IOException { throw new IOException("AnsjBufferedReader not support this interface! "); } private int start = 0; private int tempStart = 0; /** * 读取一行数据。ps 读取结果会忽略 \n \r */ public String readLine() throws IOException { ensureOpen(); StringBuilder sb = null; start = tempStart; firstRead = true; while (true) { tempLen = 0; ok = false; readString(); // if (tempLen != 0) // System.out.println(new String(cb, tempOffe, tempLen)); if (!isRead && (tempLen == 0 || len == 0)) { if (sb != null) { return sb.toString(); } return null; } if (!isRead) { // 如果不是需要读状态,那么返回 tempStart += tempLen; if (sb == null) { return new String(cb, tempOffe, tempLen); } else { sb.append(cb, tempOffe, tempLen); return sb.toString(); } } if (tempLen == 0) { continue; } // 如果 <SUF> if (sb == null) { sb = new StringBuilder(); } sb.append(cb, tempOffe, tempLen); tempStart += tempLen; } } int offe = 0; int len = 0; boolean isRead = false; boolean ok = false; boolean firstRead = true; int tempOffe; int tempLen; private void readString() throws IOException { if (offe <= 0) { if (offe == -1) { isRead = false; return; } len = in.read(cb); if (len <= 0) { // 说明到结尾了 isRead = false; return; } } isRead = true; char c = 0; int i = offe; for (; i < len; i++) { c = cb[i]; if (c != '\r' && c != '\n') { break; } if (!firstRead) { i++; tempStart++; offe = i; tempOffe = offe; isRead = false; return; } tempStart++; start++; } if (i == len) { isRead = true; offe = 0; return; } firstRead = false; offe = i; for (; i < len; i++) { c = cb[i]; if (c == '\n' || c == '\r') { isRead = false; break; } } tempOffe = offe; tempLen = i - offe; if (i == len) { if (len < cb.length) { // 说明到结尾了 isRead = false; offe = -1; } else { offe = 0; } } else { offe = i; } } @Override public void close() throws IOException { synchronized (lock) { if (in == null) { return; } try { in.close(); } finally { in = null; cb = null; } } } public int getStart() { return this.start; } }
11_3
package com.xkcoding.rbac.security.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * <p> * JWT 配置 * </p> * * @author yangkai.shen * @date Created in 2018-12-07 13:42 */ @ConfigurationProperties(prefix = "jwt.config") @Data public class JwtConfig { /** * jwt 加密 key,默认值:xkcoding. */ private String key = "xkcoding"; /** * jwt 过期时间,默认值:600000 {@code 10 分钟}. */ private Long ttl = 600000L; /** * 开启 记住我 之后 jwt 过期时间,默认值 604800000 {@code 7 天} */ private Long remember = 604800000L; }
xkcoding/spring-boot-demo
demo-rbac-security/src/main/java/com/xkcoding/rbac/security/config/JwtConfig.java
237
/** * 开启 记住我 之后 jwt 过期时间,默认值 604800000 {@code 7 天} */
block_comment
zh-cn
package com.xkcoding.rbac.security.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; /** * <p> * JWT 配置 * </p> * * @author yangkai.shen * @date Created in 2018-12-07 13:42 */ @ConfigurationProperties(prefix = "jwt.config") @Data public class JwtConfig { /** * jwt 加密 key,默认值:xkcoding. */ private String key = "xkcoding"; /** * jwt 过期时间,默认值:600000 {@code 10 分钟}. */ private Long ttl = 600000L; /** * 开启 <SUF>*/ private Long remember = 604800000L; }
12_2
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 设置 log 总开关,包括输出到控制台和文件,默认开
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 <SUF> .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_3
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 设置是否输出到控制台开关,默认开
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置 <SUF> .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_4
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 设置 log 全局标签,默认为空
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 <SUF> // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_5
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 当全局标签不为空时,我们输出的 log 全部为该 tag,
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全 <SUF> // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_6
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空 <SUF> .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_7
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 设置 log 头信息开关,默认为开
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 <SUF> .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_8
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 打印 log 时是否存到文件的开关,默认关
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 <SUF> .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_9
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 当自定义路径为空时,写入应用的/cache/log/目录中
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自 <SUF> .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_11
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 设置日志文件后缀
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置 <SUF> .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_12
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 输出日志是否带边框开关,默认开
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出 <SUF> .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_13
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条 <SUF> .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_16
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// log 栈深度,默认为 1
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// lo <SUF> .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_17
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 设置栈偏移,比如二次封装的话就需要设置,默认为 0
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置 <SUF> .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
12_18
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置日志可保留天数,默认为 -1 表示无限时长 // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
Blankj/AndroidUtilCode
lib/base/src/main/java/com/blankj/base/BaseApplication.java
996
// 设置日志可保留天数,默认为 -1 表示无限时长
line_comment
zh-cn
package com.blankj.base; import android.app.Application; import android.content.Context; import androidx.multidex.MultiDex; import com.blankj.utilcode.util.AppUtils; import com.blankj.utilcode.util.CrashUtils; import com.blankj.utilcode.util.LogUtils; import com.blankj.utilcode.util.ProcessUtils; import com.blankj.utildebug.DebugUtils; import com.blankj.utildebug.debug.IDebug; import java.util.ArrayList; /** * <pre> * author: Blankj * blog : http://blankj.com * time : 2018/11/16 * desc : base about application * </pre> */ public class BaseApplication extends Application { private static BaseApplication sInstance; public static BaseApplication getInstance() { return sInstance; } private Boolean isDebug; private Boolean isMainProcess; @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } @Override public void onCreate() { super.onCreate(); sInstance = this; initLog(); initCrash(); initDebugMenu(); } // init it in ur application public void initLog() { LogUtils.Config config = LogUtils.getConfig() .setLogSwitch(isDebug())// 设置 log 总开关,包括输出到控制台和文件,默认开 .setConsoleSwitch(isDebug())// 设置是否输出到控制台开关,默认开 .setGlobalTag(null)// 设置 log 全局标签,默认为空 // 当全局标签不为空时,我们输出的 log 全部为该 tag, // 为空时,如果传入的 tag 为空那就显示类名,否则显示 tag .setLogHeadSwitch(true)// 设置 log 头信息开关,默认为开 .setLog2FileSwitch(false)// 打印 log 时是否存到文件的开关,默认关 .setDir("")// 当自定义路径为空时,写入应用的/cache/log/目录中 .setFilePrefix("")// 当文件前缀为空时,默认为"util",即写入文件为"util-yyyy-MM-dd$fileExtension" .setFileExtension(".log")// 设置日志文件后缀 .setBorderSwitch(true)// 输出日志是否带边框开关,默认开 .setSingleTagSwitch(true)// 一条日志仅输出一条,默认开,为美化 AS 3.1 的 Logcat .setConsoleFilter(LogUtils.V)// log 的控制台过滤器,和 logcat 过滤器同理,默认 Verbose .setFileFilter(LogUtils.V)// log 文件过滤器,和 logcat 过滤器同理,默认 Verbose .setStackDeep(1)// log 栈深度,默认为 1 .setStackOffset(0)// 设置栈偏移,比如二次封装的话就需要设置,默认为 0 .setSaveDays(3)// 设置 <SUF> // 新增 ArrayList 格式化器,默认已支持 Array, Throwable, Bundle, Intent 的格式化输出 .addFormatter(new LogUtils.IFormatter<ArrayList>() { @Override public String format(ArrayList arrayList) { return "LogUtils Formatter ArrayList { " + arrayList.toString() + " }"; } }) .addFileExtraHead("ExtraKey", "ExtraValue"); LogUtils.i(config.toString()); } private void initCrash() { CrashUtils.init(new CrashUtils.OnCrashListener() { @Override public void onCrash(CrashUtils.CrashInfo crashInfo) { crashInfo.addExtraHead("extraKey", "extraValue"); LogUtils.e(crashInfo.toString()); AppUtils.relaunchApp(); } }); } private void initDebugMenu() { DebugUtils.addDebugs(new ArrayList<IDebug>()); } private boolean isDebug() { if (isDebug == null) isDebug = AppUtils.isAppDebug(); return isDebug; } public boolean isMainProcess() { if (isMainProcess == null) isMainProcess = ProcessUtils.isMainProcess(); return isMainProcess; } }
15_11
package dshomewrok; import java.util.*; /*ZigZagging on a Tree */ public class Zigzagtree { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } private static int find(int[] array, int v) { for (int i = 0; i < array.length; i++) { if (array[i] == v) { return i; } } return -1; } public static void main(String[] args) { int n = 0; int i = 0,j = 0; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int[] arr1,arr2; arr1 = new int[n]; // inorder arr2 = new int[n]; // postorder for(i = 0;i < arr1.length;i++) { arr1[i] = sc.nextInt(); } for(i = 0;i < arr2.length;i++) { arr2[i] = sc.nextInt(); } sc.close(); TreeNode root1 = BuildTree(arr1,arr2); } /*build a Tree from postorder and inorder 后序遍历的最后一个数为根结点,根据这个根结点来划分中序遍历,将其分为左子树和右子树 ②确定左右子树的中序遍历和后遍历中的界限,中序从0 - root为左, root+1 到最后为右. 后序0-root为左,root到postOrder.length-1 为右. ③递归调用 */ public static TreeNode BuildTree(int[] inOrder, int[] postOrder ) { if(postOrder.length == 0) { return null; } if(postOrder.length == 1) { TreeNode tmp = new TreeNode(postOrder[postOrder.length-1]); return tmp; } TreeNode root = new TreeNode(postOrder[postOrder.length-1]); //先判断特殊,再new 一个. int rootpos = find(inOrder,root.val); int[] leftInorder = Arrays.copyOfRange(inOrder, 0, rootpos); //copyOfRange 包括0 不包括rootpos. int[] rightInorder = Arrays.copyOfRange(inOrder, rootpos+1, inOrder.length); //System.out.println(rootpos); //System.out.println(Arrays.toString(rightInorder)); //System.out.println(Arrays.toString(leftInorder)); //不能直接打印数组要tostring int[] leftPostorder = Arrays.copyOfRange(postOrder, 0, rootpos); int[] rightPostorder = Arrays.copyOfRange(postOrder, rootpos, postOrder.length-1);; //System.out.println(Arrays.toString(leftPostorder)); //System.out.println(Arrays.toString(rightPostorder)); root.left = BuildTree(leftInorder,leftPostorder); root.right = BuildTree(rightInorder,rightPostorder); return root; } /*要进行层次遍历,需要建立一个循环队列。先将二叉树头结点入队列,然后出队列,访问该结点,如果它有左子树 则将左子树的根结点入队:如果它有右子树,则将右子树的根结点入队。然后出队列,对出队结点访问,如此反复,直到队列为空为止。 z字形层次遍历是对层次遍历加上了一个限制条件(即相邻层,从左到右的遍历顺序相反), 我们取queue大小,就是上一层的结点数. for循环把这一层都加入进去,如果奇数层就第一个结点开始(因为上一层是偶数)先加右再加左. 如果偶数层就最后结点开始(因为上一层是奇数)先加左再加右. 一层结束深度增加,同时用一个栈,因为遍历下一层的数据 和输出这一层的数据 是刚好相反的.. data arraylist是因为他最后不要空格, 所以全部保存了再输出. 不然stack其实就有正确答案了,最后多一个空格. */ public static void zigzagorder(JudgeRedBlackTree.TreeNode root ) { ArrayDeque<JudgeRedBlackTree.TreeNode> queue = new ArrayDeque<JudgeRedBlackTree.TreeNode>(32); ArrayList<Integer> data = new ArrayList<Integer>(32); //no int but Integer JudgeRedBlackTree.TreeNode temp = root; Stack<JudgeRedBlackTree.TreeNode> res = new Stack<JudgeRedBlackTree.TreeNode>(); // 就是把他 queue.add(temp); int depth = 0 ,i = 0; while(!queue.isEmpty()){ int size = queue.size(); for(i =0;i<size;i++) { if(depth %2 == 0) { temp = queue.pollLast(); if(temp.left != null) { queue.addFirst(temp.left); } if(temp.right != null){ queue.addFirst(temp.right); } } else { temp = queue.poll(); if(temp.right != null){ queue.add(temp.right); } if(temp.left != null) { queue.add(temp.left); } } res.push(temp); } while(!res.empty()) data.add(res.pop().val); depth ++; } for(i = 0;i<data.size()-1;i++) { System.out.print(data.get(i)+" "); } System.out.print(data.get(i)); } }
QSCTech/zju-icicles
数据结构基础/作业/dsHomework/tree/Zigzagtree.java
1,463
/*要进行层次遍历,需要建立一个循环队列。先将二叉树头结点入队列,然后出队列,访问该结点,如果它有左子树 则将左子树的根结点入队:如果它有右子树,则将右子树的根结点入队。然后出队列,对出队结点访问,如此反复,直到队列为空为止。 z字形层次遍历是对层次遍历加上了一个限制条件(即相邻层,从左到右的遍历顺序相反), 我们取queue大小,就是上一层的结点数. for循环把这一层都加入进去,如果奇数层就第一个结点开始(因为上一层是偶数)先加右再加左. 如果偶数层就最后结点开始(因为上一层是奇数)先加左再加右. 一层结束深度增加,同时用一个栈,因为遍历下一层的数据 和输出这一层的数据 是刚好相反的.. data arraylist是因为他最后不要空格, 所以全部保存了再输出. 不然stack其实就有正确答案了,最后多一个空格. */
block_comment
zh-cn
package dshomewrok; import java.util.*; /*ZigZagging on a Tree */ public class Zigzagtree { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } private static int find(int[] array, int v) { for (int i = 0; i < array.length; i++) { if (array[i] == v) { return i; } } return -1; } public static void main(String[] args) { int n = 0; int i = 0,j = 0; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int[] arr1,arr2; arr1 = new int[n]; // inorder arr2 = new int[n]; // postorder for(i = 0;i < arr1.length;i++) { arr1[i] = sc.nextInt(); } for(i = 0;i < arr2.length;i++) { arr2[i] = sc.nextInt(); } sc.close(); TreeNode root1 = BuildTree(arr1,arr2); } /*build a Tree from postorder and inorder 后序遍历的最后一个数为根结点,根据这个根结点来划分中序遍历,将其分为左子树和右子树 ②确定左右子树的中序遍历和后遍历中的界限,中序从0 - root为左, root+1 到最后为右. 后序0-root为左,root到postOrder.length-1 为右. ③递归调用 */ public static TreeNode BuildTree(int[] inOrder, int[] postOrder ) { if(postOrder.length == 0) { return null; } if(postOrder.length == 1) { TreeNode tmp = new TreeNode(postOrder[postOrder.length-1]); return tmp; } TreeNode root = new TreeNode(postOrder[postOrder.length-1]); //先判断特殊,再new 一个. int rootpos = find(inOrder,root.val); int[] leftInorder = Arrays.copyOfRange(inOrder, 0, rootpos); //copyOfRange 包括0 不包括rootpos. int[] rightInorder = Arrays.copyOfRange(inOrder, rootpos+1, inOrder.length); //System.out.println(rootpos); //System.out.println(Arrays.toString(rightInorder)); //System.out.println(Arrays.toString(leftInorder)); //不能直接打印数组要tostring int[] leftPostorder = Arrays.copyOfRange(postOrder, 0, rootpos); int[] rightPostorder = Arrays.copyOfRange(postOrder, rootpos, postOrder.length-1);; //System.out.println(Arrays.toString(leftPostorder)); //System.out.println(Arrays.toString(rightPostorder)); root.left = BuildTree(leftInorder,leftPostorder); root.right = BuildTree(rightInorder,rightPostorder); return root; } /*要进行 <SUF>*/ public static void zigzagorder(JudgeRedBlackTree.TreeNode root ) { ArrayDeque<JudgeRedBlackTree.TreeNode> queue = new ArrayDeque<JudgeRedBlackTree.TreeNode>(32); ArrayList<Integer> data = new ArrayList<Integer>(32); //no int but Integer JudgeRedBlackTree.TreeNode temp = root; Stack<JudgeRedBlackTree.TreeNode> res = new Stack<JudgeRedBlackTree.TreeNode>(); // 就是把他 queue.add(temp); int depth = 0 ,i = 0; while(!queue.isEmpty()){ int size = queue.size(); for(i =0;i<size;i++) { if(depth %2 == 0) { temp = queue.pollLast(); if(temp.left != null) { queue.addFirst(temp.left); } if(temp.right != null){ queue.addFirst(temp.right); } } else { temp = queue.poll(); if(temp.right != null){ queue.add(temp.right); } if(temp.left != null) { queue.add(temp.left); } } res.push(temp); } while(!res.empty()) data.add(res.pop().val); depth ++; } for(i = 0;i<data.size()-1;i++) { System.out.print(data.get(i)+" "); } System.out.print(data.get(i)); } }
15_13
package dshomewrok; import java.util.*; /*ZigZagging on a Tree */ public class Zigzagtree { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } private static int find(int[] array, int v) { for (int i = 0; i < array.length; i++) { if (array[i] == v) { return i; } } return -1; } public static void main(String[] args) { int n = 0; int i = 0,j = 0; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int[] arr1,arr2; arr1 = new int[n]; // inorder arr2 = new int[n]; // postorder for(i = 0;i < arr1.length;i++) { arr1[i] = sc.nextInt(); } for(i = 0;i < arr2.length;i++) { arr2[i] = sc.nextInt(); } sc.close(); TreeNode root1 = BuildTree(arr1,arr2); } /*build a Tree from postorder and inorder 后序遍历的最后一个数为根结点,根据这个根结点来划分中序遍历,将其分为左子树和右子树 ②确定左右子树的中序遍历和后遍历中的界限,中序从0 - root为左, root+1 到最后为右. 后序0-root为左,root到postOrder.length-1 为右. ③递归调用 */ public static TreeNode BuildTree(int[] inOrder, int[] postOrder ) { if(postOrder.length == 0) { return null; } if(postOrder.length == 1) { TreeNode tmp = new TreeNode(postOrder[postOrder.length-1]); return tmp; } TreeNode root = new TreeNode(postOrder[postOrder.length-1]); //先判断特殊,再new 一个. int rootpos = find(inOrder,root.val); int[] leftInorder = Arrays.copyOfRange(inOrder, 0, rootpos); //copyOfRange 包括0 不包括rootpos. int[] rightInorder = Arrays.copyOfRange(inOrder, rootpos+1, inOrder.length); //System.out.println(rootpos); //System.out.println(Arrays.toString(rightInorder)); //System.out.println(Arrays.toString(leftInorder)); //不能直接打印数组要tostring int[] leftPostorder = Arrays.copyOfRange(postOrder, 0, rootpos); int[] rightPostorder = Arrays.copyOfRange(postOrder, rootpos, postOrder.length-1);; //System.out.println(Arrays.toString(leftPostorder)); //System.out.println(Arrays.toString(rightPostorder)); root.left = BuildTree(leftInorder,leftPostorder); root.right = BuildTree(rightInorder,rightPostorder); return root; } /*要进行层次遍历,需要建立一个循环队列。先将二叉树头结点入队列,然后出队列,访问该结点,如果它有左子树 则将左子树的根结点入队:如果它有右子树,则将右子树的根结点入队。然后出队列,对出队结点访问,如此反复,直到队列为空为止。 z字形层次遍历是对层次遍历加上了一个限制条件(即相邻层,从左到右的遍历顺序相反), 我们取queue大小,就是上一层的结点数. for循环把这一层都加入进去,如果奇数层就第一个结点开始(因为上一层是偶数)先加右再加左. 如果偶数层就最后结点开始(因为上一层是奇数)先加左再加右. 一层结束深度增加,同时用一个栈,因为遍历下一层的数据 和输出这一层的数据 是刚好相反的.. data arraylist是因为他最后不要空格, 所以全部保存了再输出. 不然stack其实就有正确答案了,最后多一个空格. */ public static void zigzagorder(JudgeRedBlackTree.TreeNode root ) { ArrayDeque<JudgeRedBlackTree.TreeNode> queue = new ArrayDeque<JudgeRedBlackTree.TreeNode>(32); ArrayList<Integer> data = new ArrayList<Integer>(32); //no int but Integer JudgeRedBlackTree.TreeNode temp = root; Stack<JudgeRedBlackTree.TreeNode> res = new Stack<JudgeRedBlackTree.TreeNode>(); // 就是把他 queue.add(temp); int depth = 0 ,i = 0; while(!queue.isEmpty()){ int size = queue.size(); for(i =0;i<size;i++) { if(depth %2 == 0) { temp = queue.pollLast(); if(temp.left != null) { queue.addFirst(temp.left); } if(temp.right != null){ queue.addFirst(temp.right); } } else { temp = queue.poll(); if(temp.right != null){ queue.add(temp.right); } if(temp.left != null) { queue.add(temp.left); } } res.push(temp); } while(!res.empty()) data.add(res.pop().val); depth ++; } for(i = 0;i<data.size()-1;i++) { System.out.print(data.get(i)+" "); } System.out.print(data.get(i)); } }
QSCTech/zju-icicles
数据结构基础/作业/dsHomework/tree/Zigzagtree.java
1,463
// 就是把他
line_comment
zh-cn
package dshomewrok; import java.util.*; /*ZigZagging on a Tree */ public class Zigzagtree { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } private static int find(int[] array, int v) { for (int i = 0; i < array.length; i++) { if (array[i] == v) { return i; } } return -1; } public static void main(String[] args) { int n = 0; int i = 0,j = 0; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int[] arr1,arr2; arr1 = new int[n]; // inorder arr2 = new int[n]; // postorder for(i = 0;i < arr1.length;i++) { arr1[i] = sc.nextInt(); } for(i = 0;i < arr2.length;i++) { arr2[i] = sc.nextInt(); } sc.close(); TreeNode root1 = BuildTree(arr1,arr2); } /*build a Tree from postorder and inorder 后序遍历的最后一个数为根结点,根据这个根结点来划分中序遍历,将其分为左子树和右子树 ②确定左右子树的中序遍历和后遍历中的界限,中序从0 - root为左, root+1 到最后为右. 后序0-root为左,root到postOrder.length-1 为右. ③递归调用 */ public static TreeNode BuildTree(int[] inOrder, int[] postOrder ) { if(postOrder.length == 0) { return null; } if(postOrder.length == 1) { TreeNode tmp = new TreeNode(postOrder[postOrder.length-1]); return tmp; } TreeNode root = new TreeNode(postOrder[postOrder.length-1]); //先判断特殊,再new 一个. int rootpos = find(inOrder,root.val); int[] leftInorder = Arrays.copyOfRange(inOrder, 0, rootpos); //copyOfRange 包括0 不包括rootpos. int[] rightInorder = Arrays.copyOfRange(inOrder, rootpos+1, inOrder.length); //System.out.println(rootpos); //System.out.println(Arrays.toString(rightInorder)); //System.out.println(Arrays.toString(leftInorder)); //不能直接打印数组要tostring int[] leftPostorder = Arrays.copyOfRange(postOrder, 0, rootpos); int[] rightPostorder = Arrays.copyOfRange(postOrder, rootpos, postOrder.length-1);; //System.out.println(Arrays.toString(leftPostorder)); //System.out.println(Arrays.toString(rightPostorder)); root.left = BuildTree(leftInorder,leftPostorder); root.right = BuildTree(rightInorder,rightPostorder); return root; } /*要进行层次遍历,需要建立一个循环队列。先将二叉树头结点入队列,然后出队列,访问该结点,如果它有左子树 则将左子树的根结点入队:如果它有右子树,则将右子树的根结点入队。然后出队列,对出队结点访问,如此反复,直到队列为空为止。 z字形层次遍历是对层次遍历加上了一个限制条件(即相邻层,从左到右的遍历顺序相反), 我们取queue大小,就是上一层的结点数. for循环把这一层都加入进去,如果奇数层就第一个结点开始(因为上一层是偶数)先加右再加左. 如果偶数层就最后结点开始(因为上一层是奇数)先加左再加右. 一层结束深度增加,同时用一个栈,因为遍历下一层的数据 和输出这一层的数据 是刚好相反的.. data arraylist是因为他最后不要空格, 所以全部保存了再输出. 不然stack其实就有正确答案了,最后多一个空格. */ public static void zigzagorder(JudgeRedBlackTree.TreeNode root ) { ArrayDeque<JudgeRedBlackTree.TreeNode> queue = new ArrayDeque<JudgeRedBlackTree.TreeNode>(32); ArrayList<Integer> data = new ArrayList<Integer>(32); //no int but Integer JudgeRedBlackTree.TreeNode temp = root; Stack<JudgeRedBlackTree.TreeNode> res = new Stack<JudgeRedBlackTree.TreeNode>(); // 就是 <SUF> queue.add(temp); int depth = 0 ,i = 0; while(!queue.isEmpty()){ int size = queue.size(); for(i =0;i<size;i++) { if(depth %2 == 0) { temp = queue.pollLast(); if(temp.left != null) { queue.addFirst(temp.left); } if(temp.right != null){ queue.addFirst(temp.right); } } else { temp = queue.poll(); if(temp.right != null){ queue.add(temp.right); } if(temp.left != null) { queue.add(temp.left); } } res.push(temp); } while(!res.empty()) data.add(res.pop().val); depth ++; } for(i = 0;i<data.size()-1;i++) { System.out.print(data.get(i)+" "); } System.out.print(data.get(i)); } }
16_7
package decaf.frontend; import java.util.List; import decaf.Location; import decaf.tree.Tree; import decaf.tree.Tree.ClassDef; import decaf.tree.Tree.Expr; import decaf.tree.Tree.GuardedES; import decaf.tree.Tree.MethodDef; import decaf.tree.Tree.LValue; import decaf.tree.Tree.TopLevel; import decaf.tree.Tree.VarDef; import decaf.tree.Tree.TypeLiteral; import decaf.utils.MiscUtils; public class SemValue { public int code; public Location loc; public int typeTag; public Object literal; public String ident; public List<ClassDef> clist; /** * field list */ public List<Tree> flist; public List<VarDef> vlist; /** * statement list */ public List<Tree> slist; public List<Expr> elist; // no.4 public List<GuardedES> myList; public TopLevel prog; public ClassDef cdef; public VarDef vdef; public MethodDef fdef; public TypeLiteral type; public Tree stmt; public Expr expr; public LValue lvalue; public GuardedES guardedES; /** * 创建一个关键字的语义值 * * @param code * 关键字的代表码 * @return 对应关键字的语义值 */ public static SemValue createKeyword(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个操作符的语义值 * * @param code * 操作符的代表码 * @return 对应操作符的语义值 */ public static SemValue createOperator(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个常量的语义值 * * @param value * 常量的值 * @return 对应的语义值 */ public static SemValue createLiteral(int tag, Object value) { SemValue v = new SemValue(); v.code = Parser.LITERAL; v.typeTag = tag; v.literal = value; return v; } /** * 创建一个标识符的语义值 * * @param name * 标识符的名字 * @return 对应的语义值(标识符名字存放在sval域) */ public static SemValue createIdentifier(String name) { SemValue v = new SemValue(); v.code = Parser.IDENTIFIER; v.ident = name; return v; } /** * 获取这个语义值的字符串表示<br> * * 我们建议你在构造词法分析器之前先阅读一下这个函数。 */ public String toString() { String msg; switch (code) { // 关键字 case Parser.BOOL: msg = "keyword : bool"; break; case Parser.BREAK: msg = "keyword : break"; break; case Parser.CLASS: msg = "keyword : class"; break; case Parser.ELSE: msg = "keyword : else"; break; case Parser.EXTENDS: msg = "keyword : extends"; break; case Parser.FOR: msg = "keyword : for"; break; case Parser.IF: msg = "keyword : if"; break; case Parser.INT: msg = "keyword : int"; break; case Parser.INSTANCEOF: msg = "keyword : instanceof"; break; case Parser.NEW: msg = "keyword : new"; break; case Parser.NULL: msg = "keyword : null"; break; case Parser.PRINT: msg = "keyword : Print"; break; case Parser.READ_INTEGER: msg = "keyword : ReadInteger"; break; case Parser.READ_LINE: msg = "keyword : ReadLine"; break; case Parser.RETURN: msg = "keyword : return"; break; case Parser.STRING: msg = "keyword : string"; break; case Parser.THIS: msg = "keyword : this"; break; case Parser.VOID: msg = "keyword : void"; break; case Parser.WHILE: msg = "keyword : while"; break; case Parser.STATIC: msg = "keyword : static"; break; // 常量 case Parser.LITERAL: switch (typeTag) { case Tree.INT: case Tree.BOOL: msg = "constant : " + literal; break; default: msg = "constant : " + MiscUtils.quote((String)literal); } break; // 标识符 case Parser.IDENTIFIER: msg = "identifier: " + ident; break; // 操作符 case Parser.AND: msg = "operator : &&"; break; case Parser.EQUAL: msg = "operator : =="; break; case Parser.GREATER_EQUAL: msg = "operator : >="; break; case Parser.LESS_EQUAL: msg = "operator : <="; break; case Parser.NOT_EQUAL: msg = "operator : !="; break; case Parser.OR: msg = "operator : ||"; break; default: msg = "operator : " + (char) code; break; } return (String.format("%-15s%s", loc, msg)); } }
PKUanonym/REKCARC-TSC-UHT
大三上/编译原理/hw/2015_刘智峰_PA/PA2/src/decaf/frontend/SemValue.java
1,474
/** * 获取这个语义值的字符串表示<br> * * 我们建议你在构造词法分析器之前先阅读一下这个函数。 */
block_comment
zh-cn
package decaf.frontend; import java.util.List; import decaf.Location; import decaf.tree.Tree; import decaf.tree.Tree.ClassDef; import decaf.tree.Tree.Expr; import decaf.tree.Tree.GuardedES; import decaf.tree.Tree.MethodDef; import decaf.tree.Tree.LValue; import decaf.tree.Tree.TopLevel; import decaf.tree.Tree.VarDef; import decaf.tree.Tree.TypeLiteral; import decaf.utils.MiscUtils; public class SemValue { public int code; public Location loc; public int typeTag; public Object literal; public String ident; public List<ClassDef> clist; /** * field list */ public List<Tree> flist; public List<VarDef> vlist; /** * statement list */ public List<Tree> slist; public List<Expr> elist; // no.4 public List<GuardedES> myList; public TopLevel prog; public ClassDef cdef; public VarDef vdef; public MethodDef fdef; public TypeLiteral type; public Tree stmt; public Expr expr; public LValue lvalue; public GuardedES guardedES; /** * 创建一个关键字的语义值 * * @param code * 关键字的代表码 * @return 对应关键字的语义值 */ public static SemValue createKeyword(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个操作符的语义值 * * @param code * 操作符的代表码 * @return 对应操作符的语义值 */ public static SemValue createOperator(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个常量的语义值 * * @param value * 常量的值 * @return 对应的语义值 */ public static SemValue createLiteral(int tag, Object value) { SemValue v = new SemValue(); v.code = Parser.LITERAL; v.typeTag = tag; v.literal = value; return v; } /** * 创建一个标识符的语义值 * * @param name * 标识符的名字 * @return 对应的语义值(标识符名字存放在sval域) */ public static SemValue createIdentifier(String name) { SemValue v = new SemValue(); v.code = Parser.IDENTIFIER; v.ident = name; return v; } /** * 获取这 <SUF>*/ public String toString() { String msg; switch (code) { // 关键字 case Parser.BOOL: msg = "keyword : bool"; break; case Parser.BREAK: msg = "keyword : break"; break; case Parser.CLASS: msg = "keyword : class"; break; case Parser.ELSE: msg = "keyword : else"; break; case Parser.EXTENDS: msg = "keyword : extends"; break; case Parser.FOR: msg = "keyword : for"; break; case Parser.IF: msg = "keyword : if"; break; case Parser.INT: msg = "keyword : int"; break; case Parser.INSTANCEOF: msg = "keyword : instanceof"; break; case Parser.NEW: msg = "keyword : new"; break; case Parser.NULL: msg = "keyword : null"; break; case Parser.PRINT: msg = "keyword : Print"; break; case Parser.READ_INTEGER: msg = "keyword : ReadInteger"; break; case Parser.READ_LINE: msg = "keyword : ReadLine"; break; case Parser.RETURN: msg = "keyword : return"; break; case Parser.STRING: msg = "keyword : string"; break; case Parser.THIS: msg = "keyword : this"; break; case Parser.VOID: msg = "keyword : void"; break; case Parser.WHILE: msg = "keyword : while"; break; case Parser.STATIC: msg = "keyword : static"; break; // 常量 case Parser.LITERAL: switch (typeTag) { case Tree.INT: case Tree.BOOL: msg = "constant : " + literal; break; default: msg = "constant : " + MiscUtils.quote((String)literal); } break; // 标识符 case Parser.IDENTIFIER: msg = "identifier: " + ident; break; // 操作符 case Parser.AND: msg = "operator : &&"; break; case Parser.EQUAL: msg = "operator : =="; break; case Parser.GREATER_EQUAL: msg = "operator : >="; break; case Parser.LESS_EQUAL: msg = "operator : <="; break; case Parser.NOT_EQUAL: msg = "operator : !="; break; case Parser.OR: msg = "operator : ||"; break; default: msg = "operator : " + (char) code; break; } return (String.format("%-15s%s", loc, msg)); } }
16_8
package decaf.frontend; import java.util.List; import decaf.Location; import decaf.tree.Tree; import decaf.tree.Tree.ClassDef; import decaf.tree.Tree.Expr; import decaf.tree.Tree.GuardedES; import decaf.tree.Tree.MethodDef; import decaf.tree.Tree.LValue; import decaf.tree.Tree.TopLevel; import decaf.tree.Tree.VarDef; import decaf.tree.Tree.TypeLiteral; import decaf.utils.MiscUtils; public class SemValue { public int code; public Location loc; public int typeTag; public Object literal; public String ident; public List<ClassDef> clist; /** * field list */ public List<Tree> flist; public List<VarDef> vlist; /** * statement list */ public List<Tree> slist; public List<Expr> elist; // no.4 public List<GuardedES> myList; public TopLevel prog; public ClassDef cdef; public VarDef vdef; public MethodDef fdef; public TypeLiteral type; public Tree stmt; public Expr expr; public LValue lvalue; public GuardedES guardedES; /** * 创建一个关键字的语义值 * * @param code * 关键字的代表码 * @return 对应关键字的语义值 */ public static SemValue createKeyword(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个操作符的语义值 * * @param code * 操作符的代表码 * @return 对应操作符的语义值 */ public static SemValue createOperator(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个常量的语义值 * * @param value * 常量的值 * @return 对应的语义值 */ public static SemValue createLiteral(int tag, Object value) { SemValue v = new SemValue(); v.code = Parser.LITERAL; v.typeTag = tag; v.literal = value; return v; } /** * 创建一个标识符的语义值 * * @param name * 标识符的名字 * @return 对应的语义值(标识符名字存放在sval域) */ public static SemValue createIdentifier(String name) { SemValue v = new SemValue(); v.code = Parser.IDENTIFIER; v.ident = name; return v; } /** * 获取这个语义值的字符串表示<br> * * 我们建议你在构造词法分析器之前先阅读一下这个函数。 */ public String toString() { String msg; switch (code) { // 关键字 case Parser.BOOL: msg = "keyword : bool"; break; case Parser.BREAK: msg = "keyword : break"; break; case Parser.CLASS: msg = "keyword : class"; break; case Parser.ELSE: msg = "keyword : else"; break; case Parser.EXTENDS: msg = "keyword : extends"; break; case Parser.FOR: msg = "keyword : for"; break; case Parser.IF: msg = "keyword : if"; break; case Parser.INT: msg = "keyword : int"; break; case Parser.INSTANCEOF: msg = "keyword : instanceof"; break; case Parser.NEW: msg = "keyword : new"; break; case Parser.NULL: msg = "keyword : null"; break; case Parser.PRINT: msg = "keyword : Print"; break; case Parser.READ_INTEGER: msg = "keyword : ReadInteger"; break; case Parser.READ_LINE: msg = "keyword : ReadLine"; break; case Parser.RETURN: msg = "keyword : return"; break; case Parser.STRING: msg = "keyword : string"; break; case Parser.THIS: msg = "keyword : this"; break; case Parser.VOID: msg = "keyword : void"; break; case Parser.WHILE: msg = "keyword : while"; break; case Parser.STATIC: msg = "keyword : static"; break; // 常量 case Parser.LITERAL: switch (typeTag) { case Tree.INT: case Tree.BOOL: msg = "constant : " + literal; break; default: msg = "constant : " + MiscUtils.quote((String)literal); } break; // 标识符 case Parser.IDENTIFIER: msg = "identifier: " + ident; break; // 操作符 case Parser.AND: msg = "operator : &&"; break; case Parser.EQUAL: msg = "operator : =="; break; case Parser.GREATER_EQUAL: msg = "operator : >="; break; case Parser.LESS_EQUAL: msg = "operator : <="; break; case Parser.NOT_EQUAL: msg = "operator : !="; break; case Parser.OR: msg = "operator : ||"; break; default: msg = "operator : " + (char) code; break; } return (String.format("%-15s%s", loc, msg)); } }
PKUanonym/REKCARC-TSC-UHT
大三上/编译原理/hw/2015_刘智峰_PA/PA2/src/decaf/frontend/SemValue.java
1,474
// 关键字
line_comment
zh-cn
package decaf.frontend; import java.util.List; import decaf.Location; import decaf.tree.Tree; import decaf.tree.Tree.ClassDef; import decaf.tree.Tree.Expr; import decaf.tree.Tree.GuardedES; import decaf.tree.Tree.MethodDef; import decaf.tree.Tree.LValue; import decaf.tree.Tree.TopLevel; import decaf.tree.Tree.VarDef; import decaf.tree.Tree.TypeLiteral; import decaf.utils.MiscUtils; public class SemValue { public int code; public Location loc; public int typeTag; public Object literal; public String ident; public List<ClassDef> clist; /** * field list */ public List<Tree> flist; public List<VarDef> vlist; /** * statement list */ public List<Tree> slist; public List<Expr> elist; // no.4 public List<GuardedES> myList; public TopLevel prog; public ClassDef cdef; public VarDef vdef; public MethodDef fdef; public TypeLiteral type; public Tree stmt; public Expr expr; public LValue lvalue; public GuardedES guardedES; /** * 创建一个关键字的语义值 * * @param code * 关键字的代表码 * @return 对应关键字的语义值 */ public static SemValue createKeyword(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个操作符的语义值 * * @param code * 操作符的代表码 * @return 对应操作符的语义值 */ public static SemValue createOperator(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个常量的语义值 * * @param value * 常量的值 * @return 对应的语义值 */ public static SemValue createLiteral(int tag, Object value) { SemValue v = new SemValue(); v.code = Parser.LITERAL; v.typeTag = tag; v.literal = value; return v; } /** * 创建一个标识符的语义值 * * @param name * 标识符的名字 * @return 对应的语义值(标识符名字存放在sval域) */ public static SemValue createIdentifier(String name) { SemValue v = new SemValue(); v.code = Parser.IDENTIFIER; v.ident = name; return v; } /** * 获取这个语义值的字符串表示<br> * * 我们建议你在构造词法分析器之前先阅读一下这个函数。 */ public String toString() { String msg; switch (code) { // 关键 <SUF> case Parser.BOOL: msg = "keyword : bool"; break; case Parser.BREAK: msg = "keyword : break"; break; case Parser.CLASS: msg = "keyword : class"; break; case Parser.ELSE: msg = "keyword : else"; break; case Parser.EXTENDS: msg = "keyword : extends"; break; case Parser.FOR: msg = "keyword : for"; break; case Parser.IF: msg = "keyword : if"; break; case Parser.INT: msg = "keyword : int"; break; case Parser.INSTANCEOF: msg = "keyword : instanceof"; break; case Parser.NEW: msg = "keyword : new"; break; case Parser.NULL: msg = "keyword : null"; break; case Parser.PRINT: msg = "keyword : Print"; break; case Parser.READ_INTEGER: msg = "keyword : ReadInteger"; break; case Parser.READ_LINE: msg = "keyword : ReadLine"; break; case Parser.RETURN: msg = "keyword : return"; break; case Parser.STRING: msg = "keyword : string"; break; case Parser.THIS: msg = "keyword : this"; break; case Parser.VOID: msg = "keyword : void"; break; case Parser.WHILE: msg = "keyword : while"; break; case Parser.STATIC: msg = "keyword : static"; break; // 常量 case Parser.LITERAL: switch (typeTag) { case Tree.INT: case Tree.BOOL: msg = "constant : " + literal; break; default: msg = "constant : " + MiscUtils.quote((String)literal); } break; // 标识符 case Parser.IDENTIFIER: msg = "identifier: " + ident; break; // 操作符 case Parser.AND: msg = "operator : &&"; break; case Parser.EQUAL: msg = "operator : =="; break; case Parser.GREATER_EQUAL: msg = "operator : >="; break; case Parser.LESS_EQUAL: msg = "operator : <="; break; case Parser.NOT_EQUAL: msg = "operator : !="; break; case Parser.OR: msg = "operator : ||"; break; default: msg = "operator : " + (char) code; break; } return (String.format("%-15s%s", loc, msg)); } }
16_9
package decaf.frontend; import java.util.List; import decaf.Location; import decaf.tree.Tree; import decaf.tree.Tree.ClassDef; import decaf.tree.Tree.Expr; import decaf.tree.Tree.GuardedES; import decaf.tree.Tree.MethodDef; import decaf.tree.Tree.LValue; import decaf.tree.Tree.TopLevel; import decaf.tree.Tree.VarDef; import decaf.tree.Tree.TypeLiteral; import decaf.utils.MiscUtils; public class SemValue { public int code; public Location loc; public int typeTag; public Object literal; public String ident; public List<ClassDef> clist; /** * field list */ public List<Tree> flist; public List<VarDef> vlist; /** * statement list */ public List<Tree> slist; public List<Expr> elist; // no.4 public List<GuardedES> myList; public TopLevel prog; public ClassDef cdef; public VarDef vdef; public MethodDef fdef; public TypeLiteral type; public Tree stmt; public Expr expr; public LValue lvalue; public GuardedES guardedES; /** * 创建一个关键字的语义值 * * @param code * 关键字的代表码 * @return 对应关键字的语义值 */ public static SemValue createKeyword(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个操作符的语义值 * * @param code * 操作符的代表码 * @return 对应操作符的语义值 */ public static SemValue createOperator(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个常量的语义值 * * @param value * 常量的值 * @return 对应的语义值 */ public static SemValue createLiteral(int tag, Object value) { SemValue v = new SemValue(); v.code = Parser.LITERAL; v.typeTag = tag; v.literal = value; return v; } /** * 创建一个标识符的语义值 * * @param name * 标识符的名字 * @return 对应的语义值(标识符名字存放在sval域) */ public static SemValue createIdentifier(String name) { SemValue v = new SemValue(); v.code = Parser.IDENTIFIER; v.ident = name; return v; } /** * 获取这个语义值的字符串表示<br> * * 我们建议你在构造词法分析器之前先阅读一下这个函数。 */ public String toString() { String msg; switch (code) { // 关键字 case Parser.BOOL: msg = "keyword : bool"; break; case Parser.BREAK: msg = "keyword : break"; break; case Parser.CLASS: msg = "keyword : class"; break; case Parser.ELSE: msg = "keyword : else"; break; case Parser.EXTENDS: msg = "keyword : extends"; break; case Parser.FOR: msg = "keyword : for"; break; case Parser.IF: msg = "keyword : if"; break; case Parser.INT: msg = "keyword : int"; break; case Parser.INSTANCEOF: msg = "keyword : instanceof"; break; case Parser.NEW: msg = "keyword : new"; break; case Parser.NULL: msg = "keyword : null"; break; case Parser.PRINT: msg = "keyword : Print"; break; case Parser.READ_INTEGER: msg = "keyword : ReadInteger"; break; case Parser.READ_LINE: msg = "keyword : ReadLine"; break; case Parser.RETURN: msg = "keyword : return"; break; case Parser.STRING: msg = "keyword : string"; break; case Parser.THIS: msg = "keyword : this"; break; case Parser.VOID: msg = "keyword : void"; break; case Parser.WHILE: msg = "keyword : while"; break; case Parser.STATIC: msg = "keyword : static"; break; // 常量 case Parser.LITERAL: switch (typeTag) { case Tree.INT: case Tree.BOOL: msg = "constant : " + literal; break; default: msg = "constant : " + MiscUtils.quote((String)literal); } break; // 标识符 case Parser.IDENTIFIER: msg = "identifier: " + ident; break; // 操作符 case Parser.AND: msg = "operator : &&"; break; case Parser.EQUAL: msg = "operator : =="; break; case Parser.GREATER_EQUAL: msg = "operator : >="; break; case Parser.LESS_EQUAL: msg = "operator : <="; break; case Parser.NOT_EQUAL: msg = "operator : !="; break; case Parser.OR: msg = "operator : ||"; break; default: msg = "operator : " + (char) code; break; } return (String.format("%-15s%s", loc, msg)); } }
PKUanonym/REKCARC-TSC-UHT
大三上/编译原理/hw/2015_刘智峰_PA/PA2/src/decaf/frontend/SemValue.java
1,474
// 标识符
line_comment
zh-cn
package decaf.frontend; import java.util.List; import decaf.Location; import decaf.tree.Tree; import decaf.tree.Tree.ClassDef; import decaf.tree.Tree.Expr; import decaf.tree.Tree.GuardedES; import decaf.tree.Tree.MethodDef; import decaf.tree.Tree.LValue; import decaf.tree.Tree.TopLevel; import decaf.tree.Tree.VarDef; import decaf.tree.Tree.TypeLiteral; import decaf.utils.MiscUtils; public class SemValue { public int code; public Location loc; public int typeTag; public Object literal; public String ident; public List<ClassDef> clist; /** * field list */ public List<Tree> flist; public List<VarDef> vlist; /** * statement list */ public List<Tree> slist; public List<Expr> elist; // no.4 public List<GuardedES> myList; public TopLevel prog; public ClassDef cdef; public VarDef vdef; public MethodDef fdef; public TypeLiteral type; public Tree stmt; public Expr expr; public LValue lvalue; public GuardedES guardedES; /** * 创建一个关键字的语义值 * * @param code * 关键字的代表码 * @return 对应关键字的语义值 */ public static SemValue createKeyword(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个操作符的语义值 * * @param code * 操作符的代表码 * @return 对应操作符的语义值 */ public static SemValue createOperator(int code) { SemValue v = new SemValue(); v.code = code; return v; } /** * 创建一个常量的语义值 * * @param value * 常量的值 * @return 对应的语义值 */ public static SemValue createLiteral(int tag, Object value) { SemValue v = new SemValue(); v.code = Parser.LITERAL; v.typeTag = tag; v.literal = value; return v; } /** * 创建一个标识符的语义值 * * @param name * 标识符的名字 * @return 对应的语义值(标识符名字存放在sval域) */ public static SemValue createIdentifier(String name) { SemValue v = new SemValue(); v.code = Parser.IDENTIFIER; v.ident = name; return v; } /** * 获取这个语义值的字符串表示<br> * * 我们建议你在构造词法分析器之前先阅读一下这个函数。 */ public String toString() { String msg; switch (code) { // 关键字 case Parser.BOOL: msg = "keyword : bool"; break; case Parser.BREAK: msg = "keyword : break"; break; case Parser.CLASS: msg = "keyword : class"; break; case Parser.ELSE: msg = "keyword : else"; break; case Parser.EXTENDS: msg = "keyword : extends"; break; case Parser.FOR: msg = "keyword : for"; break; case Parser.IF: msg = "keyword : if"; break; case Parser.INT: msg = "keyword : int"; break; case Parser.INSTANCEOF: msg = "keyword : instanceof"; break; case Parser.NEW: msg = "keyword : new"; break; case Parser.NULL: msg = "keyword : null"; break; case Parser.PRINT: msg = "keyword : Print"; break; case Parser.READ_INTEGER: msg = "keyword : ReadInteger"; break; case Parser.READ_LINE: msg = "keyword : ReadLine"; break; case Parser.RETURN: msg = "keyword : return"; break; case Parser.STRING: msg = "keyword : string"; break; case Parser.THIS: msg = "keyword : this"; break; case Parser.VOID: msg = "keyword : void"; break; case Parser.WHILE: msg = "keyword : while"; break; case Parser.STATIC: msg = "keyword : static"; break; // 常量 case Parser.LITERAL: switch (typeTag) { case Tree.INT: case Tree.BOOL: msg = "constant : " + literal; break; default: msg = "constant : " + MiscUtils.quote((String)literal); } break; // 标识 <SUF> case Parser.IDENTIFIER: msg = "identifier: " + ident; break; // 操作符 case Parser.AND: msg = "operator : &&"; break; case Parser.EQUAL: msg = "operator : =="; break; case Parser.GREATER_EQUAL: msg = "operator : >="; break; case Parser.LESS_EQUAL: msg = "operator : <="; break; case Parser.NOT_EQUAL: msg = "operator : !="; break; case Parser.OR: msg = "operator : ||"; break; default: msg = "operator : " + (char) code; break; } return (String.format("%-15s%s", loc, msg)); } }
17_12
/* * Copyright (c) 2020, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software;Designed and Developed mainly by many Chinese * opensource volunteers. you can redistribute it and/or modify it under the * terms of the GNU General Public License version 2 only, as published by the * Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Any questions about this component can be directed to it's project Web address * https://code.google.com/p/opencloudb/. * */ package io.mycat.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import io.mycat.config.loader.zkprocess.comm.ZkConfig; import io.mycat.config.loader.zkprocess.comm.ZkParamCfg; import org.apache.log4j.Logger; import io.mycat.backend.datasource.PhysicalDBNode; import io.mycat.backend.datasource.PhysicalDBPool; import io.mycat.backend.datasource.PhysicalDatasource; import io.mycat.backend.jdbc.JDBCDatasource; import io.mycat.backend.mysql.nio.MySQLDataSource; import io.mycat.backend.postgresql.PostgreSQLDataSource; import io.mycat.config.loader.ConfigLoader; import io.mycat.config.loader.SchemaLoader; import io.mycat.config.loader.xml.XMLConfigLoader; import io.mycat.config.loader.xml.XMLSchemaLoader; import io.mycat.config.model.DBHostConfig; import io.mycat.config.model.DataHostConfig; import io.mycat.config.model.DataNodeConfig; import io.mycat.config.model.FirewallConfig; import io.mycat.config.model.SchemaConfig; import io.mycat.config.model.SystemConfig; import io.mycat.config.model.UserConfig; import io.mycat.config.util.ConfigException; import io.mycat.route.sequence.handler.DistributedSequenceHandler; import io.mycat.route.sequence.handler.IncrSequenceMySQLHandler; import io.mycat.route.sequence.handler.IncrSequenceTimeHandler; import io.mycat.route.sequence.handler.IncrSequenceZKHandler; /** * @author mycat */ public class ConfigInitializer { private static final Logger LOGGER = Logger.getLogger( ConfigInitializer.class ); private volatile SystemConfig system; private volatile MycatCluster cluster; private volatile FirewallConfig firewall; private volatile Map<String, UserConfig> users; private volatile Map<String, SchemaConfig> schemas; private volatile Map<String, PhysicalDBNode> dataNodes; private volatile Map<String, PhysicalDBPool> dataHosts; public ConfigInitializer(boolean loadDataHost) { //读取rule.xml和schema.xml SchemaLoader schemaLoader = new XMLSchemaLoader(); //读取server.xml XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader); schemaLoader = null; //加载配置 this.system = configLoader.getSystemConfig(); this.users = configLoader.getUserConfigs(); this.schemas = configLoader.getSchemaConfigs(); //是否重新加载DataHost和对应的DataNode if (loadDataHost) { this.dataHosts = initDataHosts(configLoader); this.dataNodes = initDataNodes(configLoader); } //权限管理 this.firewall = configLoader.getFirewallConfig(); this.cluster = initCobarCluster(configLoader); //不同类型的全局序列处理器的配置加载 if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) { IncrSequenceMySQLHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) { IncrSequenceTimeHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) { DistributedSequenceHandler.getInstance(system).load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) { IncrSequenceZKHandler.getInstance().load(); } /** * 配置文件初始化, 自检 */ this.selfChecking0(); } private void selfChecking0() throws ConfigException { // 检查user与schema配置对应以及schema配置不为空 if (users == null || users.isEmpty()) { throw new ConfigException("SelfCheck### user all node is empty!"); } else { for (UserConfig uc : users.values()) { if (uc == null) { throw new ConfigException("SelfCheck### users node within the item is empty!"); } Set<String> authSchemas = uc.getSchemas(); if (authSchemas == null) { throw new ConfigException("SelfCheck### user " + uc.getName() + "refered schemas is empty!"); } for (String schema : authSchemas) { if ( !schemas.containsKey(schema) ) { String errMsg = "SelfCheck### schema " + schema + " refered by user " + uc.getName() + " is not exist!"; throw new ConfigException(errMsg); } } } } // schema 配置检测 for (SchemaConfig sc : schemas.values()) { if (null == sc) { throw new ConfigException("SelfCheck### schema all node is empty!"); } else { // check dataNode / dataHost 节点 if ( this.dataNodes != null && this.dataHosts != null ) { Set<String> dataNodeNames = sc.getAllDataNodes(); for(String dataNodeName: dataNodeNames) { PhysicalDBNode node = this.dataNodes.get(dataNodeName); if ( node == null ) { throw new ConfigException("SelfCheck### schema dbnode is empty!"); } } } } } } public void testConnection() { // 实际链路的连接测试 if ( this.dataNodes != null && this.dataHosts != null ) { Map<String, Boolean> map = new HashMap<String, Boolean>(); for(PhysicalDBNode dataNode: dataNodes.values() ) { String database = dataNode.getDatabase(); PhysicalDBPool pool = dataNode.getDbPool(); for (PhysicalDatasource ds : pool.getAllDataSources()) { String key = ds.getName() + "_" + database; if ( map.get( key ) == null ) { map.put( key, false ); boolean isConnected = false; try { isConnected = ds.testConnection( database ); map.put( key, isConnected ); } catch (IOException e) { LOGGER.warn("test conn error:", e); } } } } // boolean isConnectivity = true; for (Map.Entry<String, Boolean> entry : map.entrySet()) { String key = entry.getKey(); Boolean value = entry.getValue(); if ( !value && isConnectivity ) { LOGGER.warn("SelfCheck### test " + key + " database connection failed "); isConnectivity = false; } else { LOGGER.info("SelfCheck### test " + key + " database connection success "); } } if ( !isConnectivity ) { throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!"); } } } public SystemConfig getSystem() { return system; } public MycatCluster getCluster() { return cluster; } public FirewallConfig getFirewall() { return firewall; } public Map<String, UserConfig> getUsers() { return users; } public Map<String, SchemaConfig> getSchemas() { return schemas; } public Map<String, PhysicalDBNode> getDataNodes() { return dataNodes; } public Map<String, PhysicalDBPool> getDataHosts() { return this.dataHosts; } private MycatCluster initCobarCluster(ConfigLoader configLoader) { return new MycatCluster(configLoader.getClusterConfig()); } private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) { Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts(); boolean isBooster="booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE) ) ; //根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>( nodeConfs.size()); for (DataHostConfig conf : nodeConfs.values()) { if(isBooster){ conf.setMinCon(2); } //建立PhysicalDBPool PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader); nodes.put(pool.getHostName(), pool); } return nodes; } private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) { PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length]; if (dbType.equals("mysql") && dbDriver.equals("native")) { for (int i = 0; i < nodes.length; i++) { //设置最大idle时间,默认为30分钟 nodes[i].setIdleTimeout(system.getIdleTimeout()); MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if (dbDriver.equals("jdbc")) { for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")){ for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); PostgreSQLDataSource ds = new PostgreSQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else{ throw new ConfigException("not supported yet !" + hostName); } return dataSources; } private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) { String name = conf.getName(); //数据库类型,我们这里只讨论MySQL String dbType = conf.getDbType(); //连接数据库驱动,我们这里只讨论MyCat自己实现的native String dbDriver = conf.getDbDriver(); //针对所有写节点创建PhysicalDatasource PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false); Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts(); Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>( readHostsMap.size()); //对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表 for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) { PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true); readSourcesMap.put(entry.getKey(), readSources); } PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType()); pool.setSlaveIDs(conf.getSlaveIDs()); return pool; } private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) { Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes(); Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>( nodeConfs.size()); for (DataNodeConfig conf : nodeConfs.values()) { PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost()); if (pool == null) { throw new ConfigException("dataHost not exists " + conf.getDataHost()); } PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool); nodes.put(dataNode.getName(), dataNode); } return nodes; } }
MyCATApache/Mycat-Server
src/main/java/io/mycat/config/ConfigInitializer.java
3,290
// 实际链路的连接测试
line_comment
zh-cn
/* * Copyright (c) 2020, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software;Designed and Developed mainly by many Chinese * opensource volunteers. you can redistribute it and/or modify it under the * terms of the GNU General Public License version 2 only, as published by the * Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Any questions about this component can be directed to it's project Web address * https://code.google.com/p/opencloudb/. * */ package io.mycat.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import io.mycat.config.loader.zkprocess.comm.ZkConfig; import io.mycat.config.loader.zkprocess.comm.ZkParamCfg; import org.apache.log4j.Logger; import io.mycat.backend.datasource.PhysicalDBNode; import io.mycat.backend.datasource.PhysicalDBPool; import io.mycat.backend.datasource.PhysicalDatasource; import io.mycat.backend.jdbc.JDBCDatasource; import io.mycat.backend.mysql.nio.MySQLDataSource; import io.mycat.backend.postgresql.PostgreSQLDataSource; import io.mycat.config.loader.ConfigLoader; import io.mycat.config.loader.SchemaLoader; import io.mycat.config.loader.xml.XMLConfigLoader; import io.mycat.config.loader.xml.XMLSchemaLoader; import io.mycat.config.model.DBHostConfig; import io.mycat.config.model.DataHostConfig; import io.mycat.config.model.DataNodeConfig; import io.mycat.config.model.FirewallConfig; import io.mycat.config.model.SchemaConfig; import io.mycat.config.model.SystemConfig; import io.mycat.config.model.UserConfig; import io.mycat.config.util.ConfigException; import io.mycat.route.sequence.handler.DistributedSequenceHandler; import io.mycat.route.sequence.handler.IncrSequenceMySQLHandler; import io.mycat.route.sequence.handler.IncrSequenceTimeHandler; import io.mycat.route.sequence.handler.IncrSequenceZKHandler; /** * @author mycat */ public class ConfigInitializer { private static final Logger LOGGER = Logger.getLogger( ConfigInitializer.class ); private volatile SystemConfig system; private volatile MycatCluster cluster; private volatile FirewallConfig firewall; private volatile Map<String, UserConfig> users; private volatile Map<String, SchemaConfig> schemas; private volatile Map<String, PhysicalDBNode> dataNodes; private volatile Map<String, PhysicalDBPool> dataHosts; public ConfigInitializer(boolean loadDataHost) { //读取rule.xml和schema.xml SchemaLoader schemaLoader = new XMLSchemaLoader(); //读取server.xml XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader); schemaLoader = null; //加载配置 this.system = configLoader.getSystemConfig(); this.users = configLoader.getUserConfigs(); this.schemas = configLoader.getSchemaConfigs(); //是否重新加载DataHost和对应的DataNode if (loadDataHost) { this.dataHosts = initDataHosts(configLoader); this.dataNodes = initDataNodes(configLoader); } //权限管理 this.firewall = configLoader.getFirewallConfig(); this.cluster = initCobarCluster(configLoader); //不同类型的全局序列处理器的配置加载 if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) { IncrSequenceMySQLHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) { IncrSequenceTimeHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) { DistributedSequenceHandler.getInstance(system).load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) { IncrSequenceZKHandler.getInstance().load(); } /** * 配置文件初始化, 自检 */ this.selfChecking0(); } private void selfChecking0() throws ConfigException { // 检查user与schema配置对应以及schema配置不为空 if (users == null || users.isEmpty()) { throw new ConfigException("SelfCheck### user all node is empty!"); } else { for (UserConfig uc : users.values()) { if (uc == null) { throw new ConfigException("SelfCheck### users node within the item is empty!"); } Set<String> authSchemas = uc.getSchemas(); if (authSchemas == null) { throw new ConfigException("SelfCheck### user " + uc.getName() + "refered schemas is empty!"); } for (String schema : authSchemas) { if ( !schemas.containsKey(schema) ) { String errMsg = "SelfCheck### schema " + schema + " refered by user " + uc.getName() + " is not exist!"; throw new ConfigException(errMsg); } } } } // schema 配置检测 for (SchemaConfig sc : schemas.values()) { if (null == sc) { throw new ConfigException("SelfCheck### schema all node is empty!"); } else { // check dataNode / dataHost 节点 if ( this.dataNodes != null && this.dataHosts != null ) { Set<String> dataNodeNames = sc.getAllDataNodes(); for(String dataNodeName: dataNodeNames) { PhysicalDBNode node = this.dataNodes.get(dataNodeName); if ( node == null ) { throw new ConfigException("SelfCheck### schema dbnode is empty!"); } } } } } } public void testConnection() { // 实际 <SUF> if ( this.dataNodes != null && this.dataHosts != null ) { Map<String, Boolean> map = new HashMap<String, Boolean>(); for(PhysicalDBNode dataNode: dataNodes.values() ) { String database = dataNode.getDatabase(); PhysicalDBPool pool = dataNode.getDbPool(); for (PhysicalDatasource ds : pool.getAllDataSources()) { String key = ds.getName() + "_" + database; if ( map.get( key ) == null ) { map.put( key, false ); boolean isConnected = false; try { isConnected = ds.testConnection( database ); map.put( key, isConnected ); } catch (IOException e) { LOGGER.warn("test conn error:", e); } } } } // boolean isConnectivity = true; for (Map.Entry<String, Boolean> entry : map.entrySet()) { String key = entry.getKey(); Boolean value = entry.getValue(); if ( !value && isConnectivity ) { LOGGER.warn("SelfCheck### test " + key + " database connection failed "); isConnectivity = false; } else { LOGGER.info("SelfCheck### test " + key + " database connection success "); } } if ( !isConnectivity ) { throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!"); } } } public SystemConfig getSystem() { return system; } public MycatCluster getCluster() { return cluster; } public FirewallConfig getFirewall() { return firewall; } public Map<String, UserConfig> getUsers() { return users; } public Map<String, SchemaConfig> getSchemas() { return schemas; } public Map<String, PhysicalDBNode> getDataNodes() { return dataNodes; } public Map<String, PhysicalDBPool> getDataHosts() { return this.dataHosts; } private MycatCluster initCobarCluster(ConfigLoader configLoader) { return new MycatCluster(configLoader.getClusterConfig()); } private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) { Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts(); boolean isBooster="booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE) ) ; //根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>( nodeConfs.size()); for (DataHostConfig conf : nodeConfs.values()) { if(isBooster){ conf.setMinCon(2); } //建立PhysicalDBPool PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader); nodes.put(pool.getHostName(), pool); } return nodes; } private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) { PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length]; if (dbType.equals("mysql") && dbDriver.equals("native")) { for (int i = 0; i < nodes.length; i++) { //设置最大idle时间,默认为30分钟 nodes[i].setIdleTimeout(system.getIdleTimeout()); MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if (dbDriver.equals("jdbc")) { for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")){ for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); PostgreSQLDataSource ds = new PostgreSQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else{ throw new ConfigException("not supported yet !" + hostName); } return dataSources; } private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) { String name = conf.getName(); //数据库类型,我们这里只讨论MySQL String dbType = conf.getDbType(); //连接数据库驱动,我们这里只讨论MyCat自己实现的native String dbDriver = conf.getDbDriver(); //针对所有写节点创建PhysicalDatasource PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false); Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts(); Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>( readHostsMap.size()); //对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表 for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) { PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true); readSourcesMap.put(entry.getKey(), readSources); } PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType()); pool.setSlaveIDs(conf.getSlaveIDs()); return pool; } private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) { Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes(); Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>( nodeConfs.size()); for (DataNodeConfig conf : nodeConfs.values()) { PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost()); if (pool == null) { throw new ConfigException("dataHost not exists " + conf.getDataHost()); } PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool); nodes.put(dataNode.getName(), dataNode); } return nodes; } }
17_15
/* * Copyright (c) 2020, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software;Designed and Developed mainly by many Chinese * opensource volunteers. you can redistribute it and/or modify it under the * terms of the GNU General Public License version 2 only, as published by the * Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Any questions about this component can be directed to it's project Web address * https://code.google.com/p/opencloudb/. * */ package io.mycat.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import io.mycat.config.loader.zkprocess.comm.ZkConfig; import io.mycat.config.loader.zkprocess.comm.ZkParamCfg; import org.apache.log4j.Logger; import io.mycat.backend.datasource.PhysicalDBNode; import io.mycat.backend.datasource.PhysicalDBPool; import io.mycat.backend.datasource.PhysicalDatasource; import io.mycat.backend.jdbc.JDBCDatasource; import io.mycat.backend.mysql.nio.MySQLDataSource; import io.mycat.backend.postgresql.PostgreSQLDataSource; import io.mycat.config.loader.ConfigLoader; import io.mycat.config.loader.SchemaLoader; import io.mycat.config.loader.xml.XMLConfigLoader; import io.mycat.config.loader.xml.XMLSchemaLoader; import io.mycat.config.model.DBHostConfig; import io.mycat.config.model.DataHostConfig; import io.mycat.config.model.DataNodeConfig; import io.mycat.config.model.FirewallConfig; import io.mycat.config.model.SchemaConfig; import io.mycat.config.model.SystemConfig; import io.mycat.config.model.UserConfig; import io.mycat.config.util.ConfigException; import io.mycat.route.sequence.handler.DistributedSequenceHandler; import io.mycat.route.sequence.handler.IncrSequenceMySQLHandler; import io.mycat.route.sequence.handler.IncrSequenceTimeHandler; import io.mycat.route.sequence.handler.IncrSequenceZKHandler; /** * @author mycat */ public class ConfigInitializer { private static final Logger LOGGER = Logger.getLogger( ConfigInitializer.class ); private volatile SystemConfig system; private volatile MycatCluster cluster; private volatile FirewallConfig firewall; private volatile Map<String, UserConfig> users; private volatile Map<String, SchemaConfig> schemas; private volatile Map<String, PhysicalDBNode> dataNodes; private volatile Map<String, PhysicalDBPool> dataHosts; public ConfigInitializer(boolean loadDataHost) { //读取rule.xml和schema.xml SchemaLoader schemaLoader = new XMLSchemaLoader(); //读取server.xml XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader); schemaLoader = null; //加载配置 this.system = configLoader.getSystemConfig(); this.users = configLoader.getUserConfigs(); this.schemas = configLoader.getSchemaConfigs(); //是否重新加载DataHost和对应的DataNode if (loadDataHost) { this.dataHosts = initDataHosts(configLoader); this.dataNodes = initDataNodes(configLoader); } //权限管理 this.firewall = configLoader.getFirewallConfig(); this.cluster = initCobarCluster(configLoader); //不同类型的全局序列处理器的配置加载 if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) { IncrSequenceMySQLHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) { IncrSequenceTimeHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) { DistributedSequenceHandler.getInstance(system).load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) { IncrSequenceZKHandler.getInstance().load(); } /** * 配置文件初始化, 自检 */ this.selfChecking0(); } private void selfChecking0() throws ConfigException { // 检查user与schema配置对应以及schema配置不为空 if (users == null || users.isEmpty()) { throw new ConfigException("SelfCheck### user all node is empty!"); } else { for (UserConfig uc : users.values()) { if (uc == null) { throw new ConfigException("SelfCheck### users node within the item is empty!"); } Set<String> authSchemas = uc.getSchemas(); if (authSchemas == null) { throw new ConfigException("SelfCheck### user " + uc.getName() + "refered schemas is empty!"); } for (String schema : authSchemas) { if ( !schemas.containsKey(schema) ) { String errMsg = "SelfCheck### schema " + schema + " refered by user " + uc.getName() + " is not exist!"; throw new ConfigException(errMsg); } } } } // schema 配置检测 for (SchemaConfig sc : schemas.values()) { if (null == sc) { throw new ConfigException("SelfCheck### schema all node is empty!"); } else { // check dataNode / dataHost 节点 if ( this.dataNodes != null && this.dataHosts != null ) { Set<String> dataNodeNames = sc.getAllDataNodes(); for(String dataNodeName: dataNodeNames) { PhysicalDBNode node = this.dataNodes.get(dataNodeName); if ( node == null ) { throw new ConfigException("SelfCheck### schema dbnode is empty!"); } } } } } } public void testConnection() { // 实际链路的连接测试 if ( this.dataNodes != null && this.dataHosts != null ) { Map<String, Boolean> map = new HashMap<String, Boolean>(); for(PhysicalDBNode dataNode: dataNodes.values() ) { String database = dataNode.getDatabase(); PhysicalDBPool pool = dataNode.getDbPool(); for (PhysicalDatasource ds : pool.getAllDataSources()) { String key = ds.getName() + "_" + database; if ( map.get( key ) == null ) { map.put( key, false ); boolean isConnected = false; try { isConnected = ds.testConnection( database ); map.put( key, isConnected ); } catch (IOException e) { LOGGER.warn("test conn error:", e); } } } } // boolean isConnectivity = true; for (Map.Entry<String, Boolean> entry : map.entrySet()) { String key = entry.getKey(); Boolean value = entry.getValue(); if ( !value && isConnectivity ) { LOGGER.warn("SelfCheck### test " + key + " database connection failed "); isConnectivity = false; } else { LOGGER.info("SelfCheck### test " + key + " database connection success "); } } if ( !isConnectivity ) { throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!"); } } } public SystemConfig getSystem() { return system; } public MycatCluster getCluster() { return cluster; } public FirewallConfig getFirewall() { return firewall; } public Map<String, UserConfig> getUsers() { return users; } public Map<String, SchemaConfig> getSchemas() { return schemas; } public Map<String, PhysicalDBNode> getDataNodes() { return dataNodes; } public Map<String, PhysicalDBPool> getDataHosts() { return this.dataHosts; } private MycatCluster initCobarCluster(ConfigLoader configLoader) { return new MycatCluster(configLoader.getClusterConfig()); } private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) { Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts(); boolean isBooster="booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE) ) ; //根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>( nodeConfs.size()); for (DataHostConfig conf : nodeConfs.values()) { if(isBooster){ conf.setMinCon(2); } //建立PhysicalDBPool PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader); nodes.put(pool.getHostName(), pool); } return nodes; } private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) { PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length]; if (dbType.equals("mysql") && dbDriver.equals("native")) { for (int i = 0; i < nodes.length; i++) { //设置最大idle时间,默认为30分钟 nodes[i].setIdleTimeout(system.getIdleTimeout()); MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if (dbDriver.equals("jdbc")) { for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")){ for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); PostgreSQLDataSource ds = new PostgreSQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else{ throw new ConfigException("not supported yet !" + hostName); } return dataSources; } private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) { String name = conf.getName(); //数据库类型,我们这里只讨论MySQL String dbType = conf.getDbType(); //连接数据库驱动,我们这里只讨论MyCat自己实现的native String dbDriver = conf.getDbDriver(); //针对所有写节点创建PhysicalDatasource PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false); Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts(); Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>( readHostsMap.size()); //对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表 for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) { PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true); readSourcesMap.put(entry.getKey(), readSources); } PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType()); pool.setSlaveIDs(conf.getSlaveIDs()); return pool; } private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) { Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes(); Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>( nodeConfs.size()); for (DataNodeConfig conf : nodeConfs.values()) { PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost()); if (pool == null) { throw new ConfigException("dataHost not exists " + conf.getDataHost()); } PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool); nodes.put(dataNode.getName(), dataNode); } return nodes; } }
MyCATApache/Mycat-Server
src/main/java/io/mycat/config/ConfigInitializer.java
3,290
//设置最大idle时间,默认为30分钟
line_comment
zh-cn
/* * Copyright (c) 2020, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software;Designed and Developed mainly by many Chinese * opensource volunteers. you can redistribute it and/or modify it under the * terms of the GNU General Public License version 2 only, as published by the * Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Any questions about this component can be directed to it's project Web address * https://code.google.com/p/opencloudb/. * */ package io.mycat.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import io.mycat.config.loader.zkprocess.comm.ZkConfig; import io.mycat.config.loader.zkprocess.comm.ZkParamCfg; import org.apache.log4j.Logger; import io.mycat.backend.datasource.PhysicalDBNode; import io.mycat.backend.datasource.PhysicalDBPool; import io.mycat.backend.datasource.PhysicalDatasource; import io.mycat.backend.jdbc.JDBCDatasource; import io.mycat.backend.mysql.nio.MySQLDataSource; import io.mycat.backend.postgresql.PostgreSQLDataSource; import io.mycat.config.loader.ConfigLoader; import io.mycat.config.loader.SchemaLoader; import io.mycat.config.loader.xml.XMLConfigLoader; import io.mycat.config.loader.xml.XMLSchemaLoader; import io.mycat.config.model.DBHostConfig; import io.mycat.config.model.DataHostConfig; import io.mycat.config.model.DataNodeConfig; import io.mycat.config.model.FirewallConfig; import io.mycat.config.model.SchemaConfig; import io.mycat.config.model.SystemConfig; import io.mycat.config.model.UserConfig; import io.mycat.config.util.ConfigException; import io.mycat.route.sequence.handler.DistributedSequenceHandler; import io.mycat.route.sequence.handler.IncrSequenceMySQLHandler; import io.mycat.route.sequence.handler.IncrSequenceTimeHandler; import io.mycat.route.sequence.handler.IncrSequenceZKHandler; /** * @author mycat */ public class ConfigInitializer { private static final Logger LOGGER = Logger.getLogger( ConfigInitializer.class ); private volatile SystemConfig system; private volatile MycatCluster cluster; private volatile FirewallConfig firewall; private volatile Map<String, UserConfig> users; private volatile Map<String, SchemaConfig> schemas; private volatile Map<String, PhysicalDBNode> dataNodes; private volatile Map<String, PhysicalDBPool> dataHosts; public ConfigInitializer(boolean loadDataHost) { //读取rule.xml和schema.xml SchemaLoader schemaLoader = new XMLSchemaLoader(); //读取server.xml XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader); schemaLoader = null; //加载配置 this.system = configLoader.getSystemConfig(); this.users = configLoader.getUserConfigs(); this.schemas = configLoader.getSchemaConfigs(); //是否重新加载DataHost和对应的DataNode if (loadDataHost) { this.dataHosts = initDataHosts(configLoader); this.dataNodes = initDataNodes(configLoader); } //权限管理 this.firewall = configLoader.getFirewallConfig(); this.cluster = initCobarCluster(configLoader); //不同类型的全局序列处理器的配置加载 if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) { IncrSequenceMySQLHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) { IncrSequenceTimeHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) { DistributedSequenceHandler.getInstance(system).load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) { IncrSequenceZKHandler.getInstance().load(); } /** * 配置文件初始化, 自检 */ this.selfChecking0(); } private void selfChecking0() throws ConfigException { // 检查user与schema配置对应以及schema配置不为空 if (users == null || users.isEmpty()) { throw new ConfigException("SelfCheck### user all node is empty!"); } else { for (UserConfig uc : users.values()) { if (uc == null) { throw new ConfigException("SelfCheck### users node within the item is empty!"); } Set<String> authSchemas = uc.getSchemas(); if (authSchemas == null) { throw new ConfigException("SelfCheck### user " + uc.getName() + "refered schemas is empty!"); } for (String schema : authSchemas) { if ( !schemas.containsKey(schema) ) { String errMsg = "SelfCheck### schema " + schema + " refered by user " + uc.getName() + " is not exist!"; throw new ConfigException(errMsg); } } } } // schema 配置检测 for (SchemaConfig sc : schemas.values()) { if (null == sc) { throw new ConfigException("SelfCheck### schema all node is empty!"); } else { // check dataNode / dataHost 节点 if ( this.dataNodes != null && this.dataHosts != null ) { Set<String> dataNodeNames = sc.getAllDataNodes(); for(String dataNodeName: dataNodeNames) { PhysicalDBNode node = this.dataNodes.get(dataNodeName); if ( node == null ) { throw new ConfigException("SelfCheck### schema dbnode is empty!"); } } } } } } public void testConnection() { // 实际链路的连接测试 if ( this.dataNodes != null && this.dataHosts != null ) { Map<String, Boolean> map = new HashMap<String, Boolean>(); for(PhysicalDBNode dataNode: dataNodes.values() ) { String database = dataNode.getDatabase(); PhysicalDBPool pool = dataNode.getDbPool(); for (PhysicalDatasource ds : pool.getAllDataSources()) { String key = ds.getName() + "_" + database; if ( map.get( key ) == null ) { map.put( key, false ); boolean isConnected = false; try { isConnected = ds.testConnection( database ); map.put( key, isConnected ); } catch (IOException e) { LOGGER.warn("test conn error:", e); } } } } // boolean isConnectivity = true; for (Map.Entry<String, Boolean> entry : map.entrySet()) { String key = entry.getKey(); Boolean value = entry.getValue(); if ( !value && isConnectivity ) { LOGGER.warn("SelfCheck### test " + key + " database connection failed "); isConnectivity = false; } else { LOGGER.info("SelfCheck### test " + key + " database connection success "); } } if ( !isConnectivity ) { throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!"); } } } public SystemConfig getSystem() { return system; } public MycatCluster getCluster() { return cluster; } public FirewallConfig getFirewall() { return firewall; } public Map<String, UserConfig> getUsers() { return users; } public Map<String, SchemaConfig> getSchemas() { return schemas; } public Map<String, PhysicalDBNode> getDataNodes() { return dataNodes; } public Map<String, PhysicalDBPool> getDataHosts() { return this.dataHosts; } private MycatCluster initCobarCluster(ConfigLoader configLoader) { return new MycatCluster(configLoader.getClusterConfig()); } private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) { Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts(); boolean isBooster="booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE) ) ; //根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>( nodeConfs.size()); for (DataHostConfig conf : nodeConfs.values()) { if(isBooster){ conf.setMinCon(2); } //建立PhysicalDBPool PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader); nodes.put(pool.getHostName(), pool); } return nodes; } private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) { PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length]; if (dbType.equals("mysql") && dbDriver.equals("native")) { for (int i = 0; i < nodes.length; i++) { //设置 <SUF> nodes[i].setIdleTimeout(system.getIdleTimeout()); MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if (dbDriver.equals("jdbc")) { for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")){ for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); PostgreSQLDataSource ds = new PostgreSQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else{ throw new ConfigException("not supported yet !" + hostName); } return dataSources; } private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) { String name = conf.getName(); //数据库类型,我们这里只讨论MySQL String dbType = conf.getDbType(); //连接数据库驱动,我们这里只讨论MyCat自己实现的native String dbDriver = conf.getDbDriver(); //针对所有写节点创建PhysicalDatasource PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false); Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts(); Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>( readHostsMap.size()); //对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表 for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) { PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true); readSourcesMap.put(entry.getKey(), readSources); } PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType()); pool.setSlaveIDs(conf.getSlaveIDs()); return pool; } private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) { Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes(); Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>( nodeConfs.size()); for (DataNodeConfig conf : nodeConfs.values()) { PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost()); if (pool == null) { throw new ConfigException("dataHost not exists " + conf.getDataHost()); } PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool); nodes.put(dataNode.getName(), dataNode); } return nodes; } }
17_16
/* * Copyright (c) 2020, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software;Designed and Developed mainly by many Chinese * opensource volunteers. you can redistribute it and/or modify it under the * terms of the GNU General Public License version 2 only, as published by the * Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Any questions about this component can be directed to it's project Web address * https://code.google.com/p/opencloudb/. * */ package io.mycat.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import io.mycat.config.loader.zkprocess.comm.ZkConfig; import io.mycat.config.loader.zkprocess.comm.ZkParamCfg; import org.apache.log4j.Logger; import io.mycat.backend.datasource.PhysicalDBNode; import io.mycat.backend.datasource.PhysicalDBPool; import io.mycat.backend.datasource.PhysicalDatasource; import io.mycat.backend.jdbc.JDBCDatasource; import io.mycat.backend.mysql.nio.MySQLDataSource; import io.mycat.backend.postgresql.PostgreSQLDataSource; import io.mycat.config.loader.ConfigLoader; import io.mycat.config.loader.SchemaLoader; import io.mycat.config.loader.xml.XMLConfigLoader; import io.mycat.config.loader.xml.XMLSchemaLoader; import io.mycat.config.model.DBHostConfig; import io.mycat.config.model.DataHostConfig; import io.mycat.config.model.DataNodeConfig; import io.mycat.config.model.FirewallConfig; import io.mycat.config.model.SchemaConfig; import io.mycat.config.model.SystemConfig; import io.mycat.config.model.UserConfig; import io.mycat.config.util.ConfigException; import io.mycat.route.sequence.handler.DistributedSequenceHandler; import io.mycat.route.sequence.handler.IncrSequenceMySQLHandler; import io.mycat.route.sequence.handler.IncrSequenceTimeHandler; import io.mycat.route.sequence.handler.IncrSequenceZKHandler; /** * @author mycat */ public class ConfigInitializer { private static final Logger LOGGER = Logger.getLogger( ConfigInitializer.class ); private volatile SystemConfig system; private volatile MycatCluster cluster; private volatile FirewallConfig firewall; private volatile Map<String, UserConfig> users; private volatile Map<String, SchemaConfig> schemas; private volatile Map<String, PhysicalDBNode> dataNodes; private volatile Map<String, PhysicalDBPool> dataHosts; public ConfigInitializer(boolean loadDataHost) { //读取rule.xml和schema.xml SchemaLoader schemaLoader = new XMLSchemaLoader(); //读取server.xml XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader); schemaLoader = null; //加载配置 this.system = configLoader.getSystemConfig(); this.users = configLoader.getUserConfigs(); this.schemas = configLoader.getSchemaConfigs(); //是否重新加载DataHost和对应的DataNode if (loadDataHost) { this.dataHosts = initDataHosts(configLoader); this.dataNodes = initDataNodes(configLoader); } //权限管理 this.firewall = configLoader.getFirewallConfig(); this.cluster = initCobarCluster(configLoader); //不同类型的全局序列处理器的配置加载 if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) { IncrSequenceMySQLHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) { IncrSequenceTimeHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) { DistributedSequenceHandler.getInstance(system).load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) { IncrSequenceZKHandler.getInstance().load(); } /** * 配置文件初始化, 自检 */ this.selfChecking0(); } private void selfChecking0() throws ConfigException { // 检查user与schema配置对应以及schema配置不为空 if (users == null || users.isEmpty()) { throw new ConfigException("SelfCheck### user all node is empty!"); } else { for (UserConfig uc : users.values()) { if (uc == null) { throw new ConfigException("SelfCheck### users node within the item is empty!"); } Set<String> authSchemas = uc.getSchemas(); if (authSchemas == null) { throw new ConfigException("SelfCheck### user " + uc.getName() + "refered schemas is empty!"); } for (String schema : authSchemas) { if ( !schemas.containsKey(schema) ) { String errMsg = "SelfCheck### schema " + schema + " refered by user " + uc.getName() + " is not exist!"; throw new ConfigException(errMsg); } } } } // schema 配置检测 for (SchemaConfig sc : schemas.values()) { if (null == sc) { throw new ConfigException("SelfCheck### schema all node is empty!"); } else { // check dataNode / dataHost 节点 if ( this.dataNodes != null && this.dataHosts != null ) { Set<String> dataNodeNames = sc.getAllDataNodes(); for(String dataNodeName: dataNodeNames) { PhysicalDBNode node = this.dataNodes.get(dataNodeName); if ( node == null ) { throw new ConfigException("SelfCheck### schema dbnode is empty!"); } } } } } } public void testConnection() { // 实际链路的连接测试 if ( this.dataNodes != null && this.dataHosts != null ) { Map<String, Boolean> map = new HashMap<String, Boolean>(); for(PhysicalDBNode dataNode: dataNodes.values() ) { String database = dataNode.getDatabase(); PhysicalDBPool pool = dataNode.getDbPool(); for (PhysicalDatasource ds : pool.getAllDataSources()) { String key = ds.getName() + "_" + database; if ( map.get( key ) == null ) { map.put( key, false ); boolean isConnected = false; try { isConnected = ds.testConnection( database ); map.put( key, isConnected ); } catch (IOException e) { LOGGER.warn("test conn error:", e); } } } } // boolean isConnectivity = true; for (Map.Entry<String, Boolean> entry : map.entrySet()) { String key = entry.getKey(); Boolean value = entry.getValue(); if ( !value && isConnectivity ) { LOGGER.warn("SelfCheck### test " + key + " database connection failed "); isConnectivity = false; } else { LOGGER.info("SelfCheck### test " + key + " database connection success "); } } if ( !isConnectivity ) { throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!"); } } } public SystemConfig getSystem() { return system; } public MycatCluster getCluster() { return cluster; } public FirewallConfig getFirewall() { return firewall; } public Map<String, UserConfig> getUsers() { return users; } public Map<String, SchemaConfig> getSchemas() { return schemas; } public Map<String, PhysicalDBNode> getDataNodes() { return dataNodes; } public Map<String, PhysicalDBPool> getDataHosts() { return this.dataHosts; } private MycatCluster initCobarCluster(ConfigLoader configLoader) { return new MycatCluster(configLoader.getClusterConfig()); } private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) { Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts(); boolean isBooster="booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE) ) ; //根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>( nodeConfs.size()); for (DataHostConfig conf : nodeConfs.values()) { if(isBooster){ conf.setMinCon(2); } //建立PhysicalDBPool PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader); nodes.put(pool.getHostName(), pool); } return nodes; } private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) { PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length]; if (dbType.equals("mysql") && dbDriver.equals("native")) { for (int i = 0; i < nodes.length; i++) { //设置最大idle时间,默认为30分钟 nodes[i].setIdleTimeout(system.getIdleTimeout()); MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if (dbDriver.equals("jdbc")) { for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")){ for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); PostgreSQLDataSource ds = new PostgreSQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else{ throw new ConfigException("not supported yet !" + hostName); } return dataSources; } private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) { String name = conf.getName(); //数据库类型,我们这里只讨论MySQL String dbType = conf.getDbType(); //连接数据库驱动,我们这里只讨论MyCat自己实现的native String dbDriver = conf.getDbDriver(); //针对所有写节点创建PhysicalDatasource PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false); Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts(); Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>( readHostsMap.size()); //对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表 for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) { PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true); readSourcesMap.put(entry.getKey(), readSources); } PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType()); pool.setSlaveIDs(conf.getSlaveIDs()); return pool; } private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) { Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes(); Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>( nodeConfs.size()); for (DataNodeConfig conf : nodeConfs.values()) { PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost()); if (pool == null) { throw new ConfigException("dataHost not exists " + conf.getDataHost()); } PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool); nodes.put(dataNode.getName(), dataNode); } return nodes; } }
MyCATApache/Mycat-Server
src/main/java/io/mycat/config/ConfigInitializer.java
3,290
//数据库类型,我们这里只讨论MySQL
line_comment
zh-cn
/* * Copyright (c) 2020, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software;Designed and Developed mainly by many Chinese * opensource volunteers. you can redistribute it and/or modify it under the * terms of the GNU General Public License version 2 only, as published by the * Free Software Foundation. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Any questions about this component can be directed to it's project Web address * https://code.google.com/p/opencloudb/. * */ package io.mycat.config; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; import io.mycat.config.loader.zkprocess.comm.ZkConfig; import io.mycat.config.loader.zkprocess.comm.ZkParamCfg; import org.apache.log4j.Logger; import io.mycat.backend.datasource.PhysicalDBNode; import io.mycat.backend.datasource.PhysicalDBPool; import io.mycat.backend.datasource.PhysicalDatasource; import io.mycat.backend.jdbc.JDBCDatasource; import io.mycat.backend.mysql.nio.MySQLDataSource; import io.mycat.backend.postgresql.PostgreSQLDataSource; import io.mycat.config.loader.ConfigLoader; import io.mycat.config.loader.SchemaLoader; import io.mycat.config.loader.xml.XMLConfigLoader; import io.mycat.config.loader.xml.XMLSchemaLoader; import io.mycat.config.model.DBHostConfig; import io.mycat.config.model.DataHostConfig; import io.mycat.config.model.DataNodeConfig; import io.mycat.config.model.FirewallConfig; import io.mycat.config.model.SchemaConfig; import io.mycat.config.model.SystemConfig; import io.mycat.config.model.UserConfig; import io.mycat.config.util.ConfigException; import io.mycat.route.sequence.handler.DistributedSequenceHandler; import io.mycat.route.sequence.handler.IncrSequenceMySQLHandler; import io.mycat.route.sequence.handler.IncrSequenceTimeHandler; import io.mycat.route.sequence.handler.IncrSequenceZKHandler; /** * @author mycat */ public class ConfigInitializer { private static final Logger LOGGER = Logger.getLogger( ConfigInitializer.class ); private volatile SystemConfig system; private volatile MycatCluster cluster; private volatile FirewallConfig firewall; private volatile Map<String, UserConfig> users; private volatile Map<String, SchemaConfig> schemas; private volatile Map<String, PhysicalDBNode> dataNodes; private volatile Map<String, PhysicalDBPool> dataHosts; public ConfigInitializer(boolean loadDataHost) { //读取rule.xml和schema.xml SchemaLoader schemaLoader = new XMLSchemaLoader(); //读取server.xml XMLConfigLoader configLoader = new XMLConfigLoader(schemaLoader); schemaLoader = null; //加载配置 this.system = configLoader.getSystemConfig(); this.users = configLoader.getUserConfigs(); this.schemas = configLoader.getSchemaConfigs(); //是否重新加载DataHost和对应的DataNode if (loadDataHost) { this.dataHosts = initDataHosts(configLoader); this.dataNodes = initDataNodes(configLoader); } //权限管理 this.firewall = configLoader.getFirewallConfig(); this.cluster = initCobarCluster(configLoader); //不同类型的全局序列处理器的配置加载 if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_MYSQLDB) { IncrSequenceMySQLHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_LOCAL_TIME) { IncrSequenceTimeHandler.getInstance().load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_DISTRIBUTED) { DistributedSequenceHandler.getInstance(system).load(); } if (system.getSequenceHandlerType() == SystemConfig.SEQUENCEHANDLER_ZK_GLOBAL_INCREMENT) { IncrSequenceZKHandler.getInstance().load(); } /** * 配置文件初始化, 自检 */ this.selfChecking0(); } private void selfChecking0() throws ConfigException { // 检查user与schema配置对应以及schema配置不为空 if (users == null || users.isEmpty()) { throw new ConfigException("SelfCheck### user all node is empty!"); } else { for (UserConfig uc : users.values()) { if (uc == null) { throw new ConfigException("SelfCheck### users node within the item is empty!"); } Set<String> authSchemas = uc.getSchemas(); if (authSchemas == null) { throw new ConfigException("SelfCheck### user " + uc.getName() + "refered schemas is empty!"); } for (String schema : authSchemas) { if ( !schemas.containsKey(schema) ) { String errMsg = "SelfCheck### schema " + schema + " refered by user " + uc.getName() + " is not exist!"; throw new ConfigException(errMsg); } } } } // schema 配置检测 for (SchemaConfig sc : schemas.values()) { if (null == sc) { throw new ConfigException("SelfCheck### schema all node is empty!"); } else { // check dataNode / dataHost 节点 if ( this.dataNodes != null && this.dataHosts != null ) { Set<String> dataNodeNames = sc.getAllDataNodes(); for(String dataNodeName: dataNodeNames) { PhysicalDBNode node = this.dataNodes.get(dataNodeName); if ( node == null ) { throw new ConfigException("SelfCheck### schema dbnode is empty!"); } } } } } } public void testConnection() { // 实际链路的连接测试 if ( this.dataNodes != null && this.dataHosts != null ) { Map<String, Boolean> map = new HashMap<String, Boolean>(); for(PhysicalDBNode dataNode: dataNodes.values() ) { String database = dataNode.getDatabase(); PhysicalDBPool pool = dataNode.getDbPool(); for (PhysicalDatasource ds : pool.getAllDataSources()) { String key = ds.getName() + "_" + database; if ( map.get( key ) == null ) { map.put( key, false ); boolean isConnected = false; try { isConnected = ds.testConnection( database ); map.put( key, isConnected ); } catch (IOException e) { LOGGER.warn("test conn error:", e); } } } } // boolean isConnectivity = true; for (Map.Entry<String, Boolean> entry : map.entrySet()) { String key = entry.getKey(); Boolean value = entry.getValue(); if ( !value && isConnectivity ) { LOGGER.warn("SelfCheck### test " + key + " database connection failed "); isConnectivity = false; } else { LOGGER.info("SelfCheck### test " + key + " database connection success "); } } if ( !isConnectivity ) { throw new ConfigException("SelfCheck### there are some datasource connection failed, pls check!"); } } } public SystemConfig getSystem() { return system; } public MycatCluster getCluster() { return cluster; } public FirewallConfig getFirewall() { return firewall; } public Map<String, UserConfig> getUsers() { return users; } public Map<String, SchemaConfig> getSchemas() { return schemas; } public Map<String, PhysicalDBNode> getDataNodes() { return dataNodes; } public Map<String, PhysicalDBPool> getDataHosts() { return this.dataHosts; } private MycatCluster initCobarCluster(ConfigLoader configLoader) { return new MycatCluster(configLoader.getClusterConfig()); } private Map<String, PhysicalDBPool> initDataHosts(ConfigLoader configLoader) { Map<String, DataHostConfig> nodeConfs = configLoader.getDataHosts(); boolean isBooster="booster".equalsIgnoreCase(ZkConfig.getInstance().getValue(ZkParamCfg.MYCAT_SERVER_TYPE) ) ; //根据DataHost建立PhysicalDBPool,其实就是实际数据库连接池,每个DataHost对应一个PhysicalDBPool Map<String, PhysicalDBPool> nodes = new HashMap<String, PhysicalDBPool>( nodeConfs.size()); for (DataHostConfig conf : nodeConfs.values()) { if(isBooster){ conf.setMinCon(2); } //建立PhysicalDBPool PhysicalDBPool pool = getPhysicalDBPool(conf, configLoader); nodes.put(pool.getHostName(), pool); } return nodes; } private PhysicalDatasource[] createDataSource(DataHostConfig conf, String hostName, String dbType, String dbDriver, DBHostConfig[] nodes, boolean isRead) { PhysicalDatasource[] dataSources = new PhysicalDatasource[nodes.length]; if (dbType.equals("mysql") && dbDriver.equals("native")) { for (int i = 0; i < nodes.length; i++) { //设置最大idle时间,默认为30分钟 nodes[i].setIdleTimeout(system.getIdleTimeout()); MySQLDataSource ds = new MySQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if (dbDriver.equals("jdbc")) { for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); JDBCDatasource ds = new JDBCDatasource(nodes[i], conf, isRead); dataSources[i] = ds; } } else if ("postgresql".equalsIgnoreCase(dbType) && dbDriver.equalsIgnoreCase("native")){ for (int i = 0; i < nodes.length; i++) { nodes[i].setIdleTimeout(system.getIdleTimeout()); PostgreSQLDataSource ds = new PostgreSQLDataSource(nodes[i], conf, isRead); dataSources[i] = ds; } } else{ throw new ConfigException("not supported yet !" + hostName); } return dataSources; } private PhysicalDBPool getPhysicalDBPool(DataHostConfig conf, ConfigLoader configLoader) { String name = conf.getName(); //数据 <SUF> String dbType = conf.getDbType(); //连接数据库驱动,我们这里只讨论MyCat自己实现的native String dbDriver = conf.getDbDriver(); //针对所有写节点创建PhysicalDatasource PhysicalDatasource[] writeSources = createDataSource(conf, name, dbType, dbDriver, conf.getWriteHosts(), false); Map<Integer, DBHostConfig[]> readHostsMap = conf.getReadHosts(); Map<Integer, PhysicalDatasource[]> readSourcesMap = new HashMap<Integer, PhysicalDatasource[]>( readHostsMap.size()); //对于每个读节点建立key为writeHost下标value为readHost的PhysicalDatasource[]的哈希表 for (Map.Entry<Integer, DBHostConfig[]> entry : readHostsMap.entrySet()) { PhysicalDatasource[] readSources = createDataSource(conf, name, dbType, dbDriver, entry.getValue(), true); readSourcesMap.put(entry.getKey(), readSources); } PhysicalDBPool pool = new PhysicalDBPool(conf.getName(), conf, writeSources, readSourcesMap, conf.getBalance(), conf.getWriteType()); pool.setSlaveIDs(conf.getSlaveIDs()); return pool; } private Map<String, PhysicalDBNode> initDataNodes(ConfigLoader configLoader) { Map<String, DataNodeConfig> nodeConfs = configLoader.getDataNodes(); Map<String, PhysicalDBNode> nodes = new HashMap<String, PhysicalDBNode>( nodeConfs.size()); for (DataNodeConfig conf : nodeConfs.values()) { PhysicalDBPool pool = this.dataHosts.get(conf.getDataHost()); if (pool == null) { throw new ConfigException("dataHost not exists " + conf.getDataHost()); } PhysicalDBNode dataNode = new PhysicalDBNode(conf.getName(), conf.getDatabase(), pool); nodes.put(dataNode.getName(), dataNode); } return nodes; } }
18_2
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 0:时间(弹幕出现时间)
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0: <SUF> // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_3
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕)
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1: <SUF> // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_4
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 2:字号
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2: <SUF> // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_5
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 3:颜色
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3: <SUF> // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_6
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 4:时间戳 ?
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4: <SUF> // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_11
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 出现时间
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现 <SUF> int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_12
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 弹幕类型
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕 <SUF> float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_13
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 字体大小
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体 <SUF> int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
18_21
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径数据 if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
bilibili/DanmakuFlameMaster
Sample/src/main/java/com/sample/BiliDanmukuParser.java
3,073
// 路径数据
line_comment
zh-cn
/* * Copyright (C) 2013 Chen Hui <calmer91@gmail.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.sample; import android.graphics.Color; import android.text.TextUtils; import org.json.JSONArray; import org.json.JSONException; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory; import java.io.IOException; import java.util.Locale; import master.flame.danmaku.danmaku.model.AlphaValue; import master.flame.danmaku.danmaku.model.BaseDanmaku; import master.flame.danmaku.danmaku.model.Duration; import master.flame.danmaku.danmaku.model.IDisplayer; import master.flame.danmaku.danmaku.model.SpecialDanmaku; import master.flame.danmaku.danmaku.model.android.Danmakus; import master.flame.danmaku.danmaku.parser.BaseDanmakuParser; import master.flame.danmaku.danmaku.model.android.DanmakuFactory; import master.flame.danmaku.danmaku.parser.android.AndroidFileSource; import master.flame.danmaku.danmaku.util.DanmakuUtils; import static master.flame.danmaku.danmaku.model.IDanmakus.ST_BY_TIME; public class BiliDanmukuParser extends BaseDanmakuParser { static { System.setProperty("org.xml.sax.driver", "org.xmlpull.v1.sax2.Driver"); } protected float mDispScaleX; protected float mDispScaleY; @Override public Danmakus parse() { if (mDataSource != null) { AndroidFileSource source = (AndroidFileSource) mDataSource; try { XMLReader xmlReader = XMLReaderFactory.createXMLReader(); XmlContentHandler contentHandler = new XmlContentHandler(); xmlReader.setContentHandler(contentHandler); xmlReader.parse(new InputSource(source.data())); return contentHandler.getResult(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return null; } public class XmlContentHandler extends DefaultHandler { private static final String TRUE_STRING = "true"; public Danmakus result; public BaseDanmaku item = null; public boolean completed = false; public int index = 0; public Danmakus getResult() { return result; } @Override public void startDocument() throws SAXException { result = new Danmakus(ST_BY_TIME, false, mContext.getBaseComparator()); } @Override public void endDocument() throws SAXException { completed = true; } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String tagName = localName.length() != 0 ? localName : qName; tagName = tagName.toLowerCase(Locale.getDefault()).trim(); if (tagName.equals("d")) { // <d p="23.826000213623,1,25,16777215,1422201084,0,057075e9,757076900">我从未见过如此厚颜无耻之猴</d> // 0:时间(弹幕出现时间) // 1:类型(1从右至左滚动弹幕|6从左至右滚动弹幕|5顶端固定弹幕|4底端固定弹幕|7高级弹幕|8脚本弹幕) // 2:字号 // 3:颜色 // 4:时间戳 ? // 5:弹幕池id // 6:用户hash // 7:弹幕id String pValue = attributes.getValue("p"); // parse p value to danmaku String[] values = pValue.split(","); if (values.length > 0) { long time = (long) (parseFloat(values[0]) * 1000); // 出现时间 int type = parseInteger(values[1]); // 弹幕类型 float textSize = parseFloat(values[2]); // 字体大小 int color = (int) ((0x00000000ff000000 | parseLong(values[3])) & 0x00000000ffffffff); // 颜色 // int poolType = parseInteger(values[5]); // 弹幕池类型(忽略 item = mContext.mDanmakuFactory.createDanmaku(type, mContext); if (item != null) { item.setTime(time); item.textSize = textSize * (mDispDensity - 0.6f); item.textColor = color; item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK; } } } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if (item != null && item.text != null) { if (item.duration != null) { String tagName = localName.length() != 0 ? localName : qName; if (tagName.equalsIgnoreCase("d")) { item.setTimer(mTimer); item.flags = mContext.mGlobalFlagValues; Object lock = result.obtainSynchronizer(); synchronized (lock) { result.addItem(item); } } } item = null; } } @Override public void characters(char[] ch, int start, int length) { if (item != null) { DanmakuUtils.fillText(item, decodeXmlString(new String(ch, start, length))); item.index = index++; // initial specail danmaku data String text = String.valueOf(item.text).trim(); if (item.getType() == BaseDanmaku.TYPE_SPECIAL && text.startsWith("[") && text.endsWith("]")) { //text = text.substring(1, text.length() - 1); String[] textArr = null;//text.split(",", -1); try { JSONArray jsonArray = new JSONArray(text); textArr = new String[jsonArray.length()]; for (int i = 0; i < textArr.length; i++) { textArr[i] = jsonArray.getString(i); } } catch (JSONException e) { e.printStackTrace(); } if (textArr == null || textArr.length < 5 || TextUtils.isEmpty(textArr[4])) { item = null; return; } DanmakuUtils.fillText(item, textArr[4]); float beginX = parseFloat(textArr[0]); float beginY = parseFloat(textArr[1]); float endX = beginX; float endY = beginY; String[] alphaArr = textArr[2].split("-"); int beginAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[0])); int endAlpha = beginAlpha; if (alphaArr.length > 1) { endAlpha = (int) (AlphaValue.MAX * parseFloat(alphaArr[1])); } long alphaDuraion = (long) (parseFloat(textArr[3]) * 1000); long translationDuration = alphaDuraion; long translationStartDelay = 0; float rotateY = 0, rotateZ = 0; if (textArr.length >= 7) { rotateZ = parseFloat(textArr[5]); rotateY = parseFloat(textArr[6]); } if (textArr.length >= 11) { endX = parseFloat(textArr[7]); endY = parseFloat(textArr[8]); if (!"".equals(textArr[9])) { translationDuration = parseInteger(textArr[9]); } if (!"".equals(textArr[10])) { translationStartDelay = (long) (parseFloat(textArr[10])); } } if (isPercentageNumber(textArr[0])) { beginX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (isPercentageNumber(textArr[1])) { beginY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } if (textArr.length >= 8 && isPercentageNumber(textArr[7])) { endX *= DanmakuFactory.BILI_PLAYER_WIDTH; } if (textArr.length >= 9 && isPercentageNumber(textArr[8])) { endY *= DanmakuFactory.BILI_PLAYER_HEIGHT; } item.duration = new Duration(alphaDuraion); item.rotationZ = rotateZ; item.rotationY = rotateY; mContext.mDanmakuFactory.fillTranslationData(item, beginX, beginY, endX, endY, translationDuration, translationStartDelay, mDispScaleX, mDispScaleY); mContext.mDanmakuFactory.fillAlphaData(item, beginAlpha, endAlpha, alphaDuraion); if (textArr.length >= 12) { // 是否有描边 if (!TextUtils.isEmpty(textArr[11]) && TRUE_STRING.equalsIgnoreCase(textArr[11])) { item.textShadowColor = Color.TRANSPARENT; } } if (textArr.length >= 13) { //TODO 字体 textArr[12] } if (textArr.length >= 14) { // Linear.easeIn or Quadratic.easeOut ((SpecialDanmaku) item).isQuadraticEaseOut = ("0".equals(textArr[13])); } if (textArr.length >= 15) { // 路径 <SUF> if (!"".equals(textArr[14])) { String motionPathString = textArr[14].substring(1); if (!TextUtils.isEmpty(motionPathString)) { String[] pointStrArray = motionPathString.split("L"); if (pointStrArray.length > 0) { float[][] points = new float[pointStrArray.length][2]; for (int i = 0; i < pointStrArray.length; i++) { String[] pointArray = pointStrArray[i].split(","); if (pointArray.length >= 2) { points[i][0] = parseFloat(pointArray[0]); points[i][1] = parseFloat(pointArray[1]); } } mContext.mDanmakuFactory.fillLinePathData(item, points, mDispScaleX, mDispScaleY); } } } } } } } private String decodeXmlString(String title) { if (title.contains("&amp;")) { title = title.replace("&amp;", "&"); } if (title.contains("&quot;")) { title = title.replace("&quot;", "\""); } if (title.contains("&gt;")) { title = title.replace("&gt;", ">"); } if (title.contains("&lt;")) { title = title.replace("&lt;", "<"); } return title; } } private boolean isPercentageNumber(String number) { //return number >= 0f && number <= 1f; return number != null && number.contains("."); } private float parseFloat(String floatStr) { try { return Float.parseFloat(floatStr); } catch (NumberFormatException e) { return 0.0f; } } private int parseInteger(String intStr) { try { return Integer.parseInt(intStr); } catch (NumberFormatException e) { return 0; } } private long parseLong(String longStr) { try { return Long.parseLong(longStr); } catch (NumberFormatException e) { return 0; } } @Override public BaseDanmakuParser setDisplayer(IDisplayer disp) { super.setDisplayer(disp); mDispScaleX = mDispWidth / DanmakuFactory.BILI_PLAYER_WIDTH; mDispScaleY = mDispHeight / DanmakuFactory.BILI_PLAYER_HEIGHT; return this; } }
19_1
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
macrozheng/mall
mall-mbg/src/main/java/com/macro/mall/Generator.java
305
//MBG 执行过程中的警告信息
line_comment
zh-cn
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MB <SUF> List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
19_2
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
macrozheng/mall
mall-mbg/src/main/java/com/macro/mall/Generator.java
305
//当生成的代码重复时,覆盖原代码
line_comment
zh-cn
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生 <SUF> boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
19_3
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
macrozheng/mall
mall-mbg/src/main/java/com/macro/mall/Generator.java
305
//读取我们的 MBG 配置文件
line_comment
zh-cn
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取 <SUF> InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
19_6
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出警告信息 for (String warning : warnings) { System.out.println(warning); } } }
macrozheng/mall
mall-mbg/src/main/java/com/macro/mall/Generator.java
305
//输出警告信息
line_comment
zh-cn
package com.macro.mall; import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * MBG代码生成工具 * Created by macro on 2018/4/26. */ public class Generator { public static void main(String[] args) throws Exception { //MBG 执行过程中的警告信息 List<String> warnings = new ArrayList<String>(); //当生成的代码重复时,覆盖原代码 boolean overwrite = true; //读取我们的 MBG 配置文件 InputStream is = Generator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); is.close(); DefaultShellCallback callback = new DefaultShellCallback(overwrite); //创建 MBG MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); //执行生成代码 myBatisGenerator.generate(null); //输出 <SUF> for (String warning : warnings) { System.out.println(warning); } } }
21_0
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截器. Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理 mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
ityouknow/spring-boot-examples
2.x/spring-boot-shiro/src/main/java/com/neo/config/ShiroConfig.java
1,050
//拦截器.
line_comment
zh-cn
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截 <SUF> Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理 mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
21_1
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截器. Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理 mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
ityouknow/spring-boot-examples
2.x/spring-boot-shiro/src/main/java/com/neo/config/ShiroConfig.java
1,050
// 配置不会被拦截的链接 顺序判断
line_comment
zh-cn
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截器. Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置 <SUF> filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理 mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
21_4
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截器. Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理 mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
ityouknow/spring-boot-examples
2.x/spring-boot-shiro/src/main/java/com/neo/config/ShiroConfig.java
1,050
//散列的次数,比如散列两次,相当于 md5(md5(""));
line_comment
zh-cn
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截器. Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列 <SUF> return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理 mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
21_6
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截器. Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据库异常处理 mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
ityouknow/spring-boot-examples
2.x/spring-boot-shiro/src/main/java/com/neo/config/ShiroConfig.java
1,050
//数据库异常处理
line_comment
zh-cn
package com.neo.config; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; @Configuration public class ShiroConfig { @Bean public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) { System.out.println("ShiroConfiguration.shirFilter()"); ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); //拦截器. Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>(); // 配置不会被拦截的链接 顺序判断 filterChainDefinitionMap.put("/static/**", "anon"); //配置退出 过滤器,其中的具体的退出代码Shiro已经替我们实现了 filterChainDefinitionMap.put("/logout", "logout"); //<!-- 过滤链定义,从上向下顺序执行,一般将/**放在最为下边 -->:这是一个坑呢,一不小心代码就不好使了; //<!-- authc:所有url都必须认证通过才可以访问; anon:所有url都都可以匿名访问--> filterChainDefinitionMap.put("/**", "authc"); // 如果不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 shiroFilterFactoryBean.setLoginUrl("/login"); // 登录成功后要跳转的链接 shiroFilterFactoryBean.setSuccessUrl("/index"); //未授权界面; shiroFilterFactoryBean.setUnauthorizedUrl("/403"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } /** * 凭证匹配器 * (由于我们的密码校验交给Shiro的SimpleAuthenticationInfo进行处理了 * ) * @return */ @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5");//散列算法:这里使用MD5算法; hashedCredentialsMatcher.setHashIterations(2);//散列的次数,比如散列两次,相当于 md5(md5("")); return hashedCredentialsMatcher; } @Bean public MyShiroRealm myShiroRealm(){ MyShiroRealm myShiroRealm = new MyShiroRealm(); myShiroRealm.setCredentialsMatcher(hashedCredentialsMatcher()); return myShiroRealm; } @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(myShiroRealm()); return securityManager; } /** * 开启shiro aop注解支持. * 使用代理方式;所以需要开启代码支持; * @param securityManager * @return */ @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager){ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); return authorizationAttributeSourceAdvisor; } @Bean(name="simpleMappingExceptionResolver") public SimpleMappingExceptionResolver createSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver r = new SimpleMappingExceptionResolver(); Properties mappings = new Properties(); mappings.setProperty("DatabaseException", "databaseError");//数据 <SUF> mappings.setProperty("UnauthorizedException","403"); r.setExceptionMappings(mappings); // None by default r.setDefaultErrorView("error"); // No default r.setExceptionAttribute("ex"); // Default is "exception" //r.setWarnLogCategory("example.MvcLogger"); // No default return r; } }
23_0
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twi <SUF>*/ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_2
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 开始时间截 (2015-01-01) */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时 <SUF>*/ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_3
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 机器id所占的位数 */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器i <SUF>*/ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_4
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 数据标识id所占的位数 */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标 <SUF>*/ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_5
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的 <SUF>*/ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_6
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 支持的最大数据标识id,结果是31 */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的 <SUF>*/ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_7
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 序列在id中占的位数 */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在 <SUF>*/ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_9
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 数据标识id向左移17位(12+5) */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标 <SUF>*/ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_10
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 时间截向左移22位(5+5+12) */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截 <SUF>*/ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_11
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序 <SUF>*/ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_13
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 数据中心ID(0~31) */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中 <SUF>*/ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_20
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
//如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常
line_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果 <SUF> if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_21
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
//如果是同一时间生成的,则进行毫秒内序列
line_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果 <SUF> if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_23
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
//阻塞到下一个毫秒,获得新的时间戳
line_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞 <SUF> timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_24
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
//时间戳改变,毫秒内序列重置
line_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间 <SUF> else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_26
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
//移位并通过或运算拼到一起组成64位的ID
line_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位 <SUF> return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_28
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以 <SUF>*/ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
23_31
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 */ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
shuzheng/zheng
zheng-common/src/main/java/com/zheng/common/util/key/SnowflakeIdWorker.java
1,854
/** * 测试 */
block_comment
zh-cn
package com.zheng.common.util.key; /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0000000000 0000000000 0 - 00000 - 00000 - 000000000000 <br> * 1位标识,由于long基本类型在Java中是带符号的,最高位是符号位,正数是0,负数是1,所以id一般是正数,最高位是0<br> * 41位时间截(毫秒级),注意,41位时间截不是存储当前时间的时间截,而是存储时间截的差值(当前时间截 - 开始时间截) * 得到的值),这里的的开始时间截,一般是我们的id生成器开始使用的时间,由我们程序来指定的(如下下面程序IdWorker类的startTime属性)。41位的时间截,可以使用69年,年T = (1L << 41) / (1000L * 60 * 60 * 24 * 365) = 69<br> * 10位的数据机器位,可以部署在1024个节点,包括5位datacenterId和5位workerId<br> * 12位序列,毫秒内的计数,12位的计数顺序号支持每个节点每毫秒(同一机器,同一时间截)产生4096个ID序号<br> * 加起来刚好64位,为一个Long型。<br> * SnowFlake的优点是,整体上按照时间自增排序,并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分),并且效率较高,经测试,SnowFlake每秒能够产生26万ID左右。 */ public class SnowflakeIdWorker { // ==============================Fields=========================================== /** * 开始时间截 (2015-01-01) */ private final long twepoch = 1420041600000L; /** * 机器id所占的位数 */ private final long workerIdBits = 5L; /** * 数据标识id所占的位数 */ private final long datacenterIdBits = 5L; /** * 支持的最大机器id,结果是31 (这个移位算法可以很快的计算出几位二进制数所能表示的最大十进制数) */ private final long maxWorkerId = -1L ^ (-1L << workerIdBits); /** * 支持的最大数据标识id,结果是31 */ private final long maxDatacenterId = -1L ^ (-1L << datacenterIdBits); /** * 序列在id中占的位数 */ private final long sequenceBits = 12L; /** * 机器ID向左移12位 */ private final long workerIdShift = sequenceBits; /** * 数据标识id向左移17位(12+5) */ private final long datacenterIdShift = sequenceBits + workerIdBits; /** * 时间截向左移22位(5+5+12) */ private final long timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits; /** * 生成序列的掩码,这里为4095 (0b111111111111=0xfff=4095) */ private final long sequenceMask = -1L ^ (-1L << sequenceBits); /** * 工作机器ID(0~31) */ private long workerId; /** * 数据中心ID(0~31) */ private long datacenterId; /** * 毫秒内序列(0~4095) */ private long sequence = 0L; /** * 上次生成ID的时间截 */ private long lastTimestamp = -1L; //==============================Constructors===================================== /** * 构造函数 * * @param workerId 工作ID (0~31) * @param datacenterId 数据中心ID (0~31) */ public SnowflakeIdWorker(long workerId, long datacenterId) { if (workerId > maxWorkerId || workerId < 0) { throw new IllegalArgumentException(String.format("worker Id can't be greater than %d or less than 0", maxWorkerId)); } if (datacenterId > maxDatacenterId || datacenterId < 0) { throw new IllegalArgumentException(String.format("datacenter Id can't be greater than %d or less than 0", maxDatacenterId)); } this.workerId = workerId; this.datacenterId = datacenterId; } // ==============================Methods========================================== /** * 获得下一个ID (该方法是线程安全的) * * @return SnowflakeId */ public synchronized long nextId() { long timestamp = timeGen(); //如果当前时间小于上一次ID生成的时间戳,说明系统时钟回退过这个时候应当抛出异常 if (timestamp < lastTimestamp) { throw new RuntimeException( String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } //如果是同一时间生成的,则进行毫秒内序列 if (lastTimestamp == timestamp) { sequence = (sequence + 1) & sequenceMask; //毫秒内序列溢出 if (sequence == 0) { //阻塞到下一个毫秒,获得新的时间戳 timestamp = tilNextMillis(lastTimestamp); } } //时间戳改变,毫秒内序列重置 else { sequence = 0L; } //上次生成ID的时间截 lastTimestamp = timestamp; //移位并通过或运算拼到一起组成64位的ID return ((timestamp - twepoch) << timestampLeftShift) // | (datacenterId << datacenterIdShift) // | (workerId << workerIdShift) // | sequence; } /** * 阻塞到下一个毫秒,直到获得新的时间戳 * * @param lastTimestamp 上次生成ID的时间截 * @return 当前时间戳 */ protected long tilNextMillis(long lastTimestamp) { long timestamp = timeGen(); while (timestamp <= lastTimestamp) { timestamp = timeGen(); } return timestamp; } /** * 返回以毫秒为单位的当前时间 * * @return 当前时间(毫秒) */ protected long timeGen() { //return System.currentTimeMillis(); return SystemClock.now(); } //==============================Test============================================= /** * 测试 <SUF>*/ public static void main(String[] args) { long start = System.currentTimeMillis(); SnowflakeIdWorker idWorker0 = new SnowflakeIdWorker(0, 0); for (int i = 0; i < 10000000; i++) { long id = idWorker0.nextId(); //System.out.println(id); } System.out.println("耗时:" + (System.currentTimeMillis() - start)); } }
24_1
package cn.hutool.core.io.resource; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.nio.charset.Charset; /** * 资源接口定义<br> * <p>资源是数据表示的统称,我们可以将任意的数据封装为一个资源,然后读取其内容。</p> * <p>资源可以是文件、URL、ClassPath中的文件亦或者jar(zip)包中的文件。</p> * <p> * 提供资源接口的意义在于,我们可以使用一个方法接收任意类型的数据,从而处理数据, * 无需专门针对File、InputStream等写多个重载方法,同时也为更好的扩展提供了可能。 * </p> * <p>使用非常简单,假设我们需要从classpath中读取一个xml,我们不用关心这个文件在目录中还是在jar中:</p> * <pre> * Resource resource = new ClassPathResource("test.xml"); * String xmlStr = resource.readUtf8Str(); * </pre> * <p>同样,我们可以自己实现Resource接口,按照业务需要从任意位置读取数据,比如从数据库中。</p> * * @author looly * @since 3.2.1 */ public interface Resource { /** * 获取资源名,例如文件资源的资源名为文件名 * * @return 资源名 * @since 4.0.13 */ String getName(); /** * 获得解析后的{@link URL},无对应URL的返回{@code null} * * @return 解析后的{@link URL} */ URL getUrl(); /** * 获得 {@link InputStream} * * @return {@link InputStream} */ InputStream getStream(); /** * 检查资源是否变更<br> * 一般用于文件类资源,检查文件是否被修改过。 * * @return 是否变更 * @since 5.7.21 */ default boolean isModified(){ return false; } /** * 将资源内容写出到流,不关闭输出流,但是关闭资源流 * * @param out 输出流 * @throws IORuntimeException IO异常 * @since 5.3.5 */ default void writeTo(OutputStream out) throws IORuntimeException { try (InputStream in = getStream()) { IoUtil.copy(in, out); } catch (IOException e) { throw new IORuntimeException(e); } } /** * 获得Reader * * @param charset 编码 * @return {@link BufferedReader} */ default BufferedReader getReader(Charset charset) { return IoUtil.getReader(getStream(), charset); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @param charset 编码 * @return 读取资源内容 * @throws IORuntimeException 包装{@link IOException} */ default String readStr(Charset charset) throws IORuntimeException { return IoUtil.read(getReader(charset)); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default String readUtf8Str() throws IORuntimeException { return readStr(CharsetUtil.CHARSET_UTF_8); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default byte[] readBytes() throws IORuntimeException { return IoUtil.readBytes(getStream()); } }
dromara/hutool
hutool-core/src/main/java/cn/hutool/core/io/resource/Resource.java
1,003
/** * 获取资源名,例如文件资源的资源名为文件名 * * @return 资源名 * @since 4.0.13 */
block_comment
zh-cn
package cn.hutool.core.io.resource; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.nio.charset.Charset; /** * 资源接口定义<br> * <p>资源是数据表示的统称,我们可以将任意的数据封装为一个资源,然后读取其内容。</p> * <p>资源可以是文件、URL、ClassPath中的文件亦或者jar(zip)包中的文件。</p> * <p> * 提供资源接口的意义在于,我们可以使用一个方法接收任意类型的数据,从而处理数据, * 无需专门针对File、InputStream等写多个重载方法,同时也为更好的扩展提供了可能。 * </p> * <p>使用非常简单,假设我们需要从classpath中读取一个xml,我们不用关心这个文件在目录中还是在jar中:</p> * <pre> * Resource resource = new ClassPathResource("test.xml"); * String xmlStr = resource.readUtf8Str(); * </pre> * <p>同样,我们可以自己实现Resource接口,按照业务需要从任意位置读取数据,比如从数据库中。</p> * * @author looly * @since 3.2.1 */ public interface Resource { /** * 获取资 <SUF>*/ String getName(); /** * 获得解析后的{@link URL},无对应URL的返回{@code null} * * @return 解析后的{@link URL} */ URL getUrl(); /** * 获得 {@link InputStream} * * @return {@link InputStream} */ InputStream getStream(); /** * 检查资源是否变更<br> * 一般用于文件类资源,检查文件是否被修改过。 * * @return 是否变更 * @since 5.7.21 */ default boolean isModified(){ return false; } /** * 将资源内容写出到流,不关闭输出流,但是关闭资源流 * * @param out 输出流 * @throws IORuntimeException IO异常 * @since 5.3.5 */ default void writeTo(OutputStream out) throws IORuntimeException { try (InputStream in = getStream()) { IoUtil.copy(in, out); } catch (IOException e) { throw new IORuntimeException(e); } } /** * 获得Reader * * @param charset 编码 * @return {@link BufferedReader} */ default BufferedReader getReader(Charset charset) { return IoUtil.getReader(getStream(), charset); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @param charset 编码 * @return 读取资源内容 * @throws IORuntimeException 包装{@link IOException} */ default String readStr(Charset charset) throws IORuntimeException { return IoUtil.read(getReader(charset)); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default String readUtf8Str() throws IORuntimeException { return readStr(CharsetUtil.CHARSET_UTF_8); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default byte[] readBytes() throws IORuntimeException { return IoUtil.readBytes(getStream()); } }
24_4
package cn.hutool.core.io.resource; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.nio.charset.Charset; /** * 资源接口定义<br> * <p>资源是数据表示的统称,我们可以将任意的数据封装为一个资源,然后读取其内容。</p> * <p>资源可以是文件、URL、ClassPath中的文件亦或者jar(zip)包中的文件。</p> * <p> * 提供资源接口的意义在于,我们可以使用一个方法接收任意类型的数据,从而处理数据, * 无需专门针对File、InputStream等写多个重载方法,同时也为更好的扩展提供了可能。 * </p> * <p>使用非常简单,假设我们需要从classpath中读取一个xml,我们不用关心这个文件在目录中还是在jar中:</p> * <pre> * Resource resource = new ClassPathResource("test.xml"); * String xmlStr = resource.readUtf8Str(); * </pre> * <p>同样,我们可以自己实现Resource接口,按照业务需要从任意位置读取数据,比如从数据库中。</p> * * @author looly * @since 3.2.1 */ public interface Resource { /** * 获取资源名,例如文件资源的资源名为文件名 * * @return 资源名 * @since 4.0.13 */ String getName(); /** * 获得解析后的{@link URL},无对应URL的返回{@code null} * * @return 解析后的{@link URL} */ URL getUrl(); /** * 获得 {@link InputStream} * * @return {@link InputStream} */ InputStream getStream(); /** * 检查资源是否变更<br> * 一般用于文件类资源,检查文件是否被修改过。 * * @return 是否变更 * @since 5.7.21 */ default boolean isModified(){ return false; } /** * 将资源内容写出到流,不关闭输出流,但是关闭资源流 * * @param out 输出流 * @throws IORuntimeException IO异常 * @since 5.3.5 */ default void writeTo(OutputStream out) throws IORuntimeException { try (InputStream in = getStream()) { IoUtil.copy(in, out); } catch (IOException e) { throw new IORuntimeException(e); } } /** * 获得Reader * * @param charset 编码 * @return {@link BufferedReader} */ default BufferedReader getReader(Charset charset) { return IoUtil.getReader(getStream(), charset); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @param charset 编码 * @return 读取资源内容 * @throws IORuntimeException 包装{@link IOException} */ default String readStr(Charset charset) throws IORuntimeException { return IoUtil.read(getReader(charset)); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default String readUtf8Str() throws IORuntimeException { return readStr(CharsetUtil.CHARSET_UTF_8); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default byte[] readBytes() throws IORuntimeException { return IoUtil.readBytes(getStream()); } }
dromara/hutool
hutool-core/src/main/java/cn/hutool/core/io/resource/Resource.java
1,003
/** * 检查资源是否变更<br> * 一般用于文件类资源,检查文件是否被修改过。 * * @return 是否变更 * @since 5.7.21 */
block_comment
zh-cn
package cn.hutool.core.io.resource; import cn.hutool.core.io.IORuntimeException; import cn.hutool.core.io.IoUtil; import cn.hutool.core.util.CharsetUtil; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.nio.charset.Charset; /** * 资源接口定义<br> * <p>资源是数据表示的统称,我们可以将任意的数据封装为一个资源,然后读取其内容。</p> * <p>资源可以是文件、URL、ClassPath中的文件亦或者jar(zip)包中的文件。</p> * <p> * 提供资源接口的意义在于,我们可以使用一个方法接收任意类型的数据,从而处理数据, * 无需专门针对File、InputStream等写多个重载方法,同时也为更好的扩展提供了可能。 * </p> * <p>使用非常简单,假设我们需要从classpath中读取一个xml,我们不用关心这个文件在目录中还是在jar中:</p> * <pre> * Resource resource = new ClassPathResource("test.xml"); * String xmlStr = resource.readUtf8Str(); * </pre> * <p>同样,我们可以自己实现Resource接口,按照业务需要从任意位置读取数据,比如从数据库中。</p> * * @author looly * @since 3.2.1 */ public interface Resource { /** * 获取资源名,例如文件资源的资源名为文件名 * * @return 资源名 * @since 4.0.13 */ String getName(); /** * 获得解析后的{@link URL},无对应URL的返回{@code null} * * @return 解析后的{@link URL} */ URL getUrl(); /** * 获得 {@link InputStream} * * @return {@link InputStream} */ InputStream getStream(); /** * 检查资 <SUF>*/ default boolean isModified(){ return false; } /** * 将资源内容写出到流,不关闭输出流,但是关闭资源流 * * @param out 输出流 * @throws IORuntimeException IO异常 * @since 5.3.5 */ default void writeTo(OutputStream out) throws IORuntimeException { try (InputStream in = getStream()) { IoUtil.copy(in, out); } catch (IOException e) { throw new IORuntimeException(e); } } /** * 获得Reader * * @param charset 编码 * @return {@link BufferedReader} */ default BufferedReader getReader(Charset charset) { return IoUtil.getReader(getStream(), charset); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @param charset 编码 * @return 读取资源内容 * @throws IORuntimeException 包装{@link IOException} */ default String readStr(Charset charset) throws IORuntimeException { return IoUtil.read(getReader(charset)); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default String readUtf8Str() throws IORuntimeException { return readStr(CharsetUtil.CHARSET_UTF_8); } /** * 读取资源内容,读取完毕后会关闭流<br> * 关闭流并不影响下一次读取 * * @return 读取资源内容 * @throws IORuntimeException 包装IOException */ default byte[] readBytes() throws IORuntimeException { return IoUtil.readBytes(getStream()); } }
25_0
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
dromara/hutool
hutool-db/src/main/java/cn/hutool/db/ds/GlobalDSFactory.java
659
/** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */
block_comment
zh-cn
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的 <SUF>*/ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
25_1
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
dromara/hutool
hutool-db/src/main/java/cn/hutool/db/ds/GlobalDSFactory.java
659
/* * 设置在JVM关闭时关闭所有数据库连接 */
block_comment
zh-cn
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在 <SUF>*/ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
25_2
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
dromara/hutool
hutool-db/src/main/java/cn/hutool/db/ds/GlobalDSFactory.java
659
// JVM关闭时关闭所有连接池
line_comment
zh-cn
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JV <SUF> Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
25_3
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
dromara/hutool
hutool-db/src/main/java/cn/hutool/db/ds/GlobalDSFactory.java
659
/** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */
block_comment
zh-cn
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默 <SUF>*/ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
25_4
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
dromara/hutool
hutool-db/src/main/java/cn/hutool/db/ds/GlobalDSFactory.java
659
/** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */
block_comment
zh-cn
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全 <SUF>*/ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
25_5
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
dromara/hutool
hutool-db/src/main/java/cn/hutool/db/ds/GlobalDSFactory.java
659
// 数据源工厂不变时返回原数据源工厂
line_comment
zh-cn
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据 <SUF> } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
25_6
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定义数据源工厂前关闭之前的数据源 factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
dromara/hutool
hutool-db/src/main/java/cn/hutool/db/ds/GlobalDSFactory.java
659
// 自定义数据源工厂前关闭之前的数据源
line_comment
zh-cn
package cn.hutool.db.ds; import cn.hutool.log.StaticLog; /** * 全局的数据源工厂<br> * 一般情况下,一个应用默认只使用一种数据库连接池,因此维护一个全局的数据源工厂类减少判断连接池类型造成的性能浪费 * * @author looly * @since 4.0.2 */ public class GlobalDSFactory { private static volatile DSFactory factory; private static final Object lock = new Object(); /* * 设置在JVM关闭时关闭所有数据库连接 */ static { // JVM关闭时关闭所有连接池 Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { if (null != factory) { factory.destroy(); StaticLog.debug("DataSource: [{}] destroyed.", factory.dataSourceName); factory = null; } } }); } /** * 获取默认的数据源工厂,读取默认数据库配置文件<br> * 此处使用懒加载模式,在第一次调用此方法时才创建默认数据源工厂<br> * 如果想自定义全局的数据源工厂,请在第一次调用此方法前调用{@link #set(DSFactory)} 方法自行定义 * * @return 当前使用的数据源工厂 */ public static DSFactory get() { if (null == factory) { synchronized (lock) { if (null == factory) { factory = DSFactory.create(null); } } } return factory; } /** * 设置全局的数据源工厂<br> * 在项目中存在多个连接池库的情况下,我们希望使用低优先级的库时使用此方法自定义之<br> * 重新定义全局的数据源工厂此方法可在以下两种情况下调用: * * <pre> * 1. 在get方法调用前调用此方法来自定义全局的数据源工厂 * 2. 替换已存在的全局数据源工厂,当已存在时会自动关闭 * </pre> * * @param customDSFactory 自定义数据源工厂 * @return 自定义的数据源工厂 */ public static DSFactory set(DSFactory customDSFactory) { synchronized (lock) { if (null != factory) { if (factory.equals(customDSFactory)) { return factory;// 数据源工厂不变时返回原数据源工厂 } // 自定 <SUF> factory.destroy(); } StaticLog.debug("Custom use [{}] DataSource.", customDSFactory.dataSourceName); factory = customDSFactory; } return factory; } }
26_1
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环境 */ public static final String DEFAULT_PROFILE = "default"; /** 条件 */ private String profile; /** 编码 */ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }
dromara/hutool
hutool-setting/src/main/java/cn/hutool/setting/profile/Profile.java
1,084
/** 默认环境 */
block_comment
zh-cn
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环 <SUF>*/ public static final String DEFAULT_PROFILE = "default"; /** 条件 */ private String profile; /** 编码 */ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }
26_2
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环境 */ public static final String DEFAULT_PROFILE = "default"; /** 条件 */ private String profile; /** 编码 */ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }
dromara/hutool
hutool-setting/src/main/java/cn/hutool/setting/profile/Profile.java
1,084
/** 条件 */
block_comment
zh-cn
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环境 */ public static final String DEFAULT_PROFILE = "default"; /** 条件 <SUF>*/ private String profile; /** 编码 */ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }
26_3
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环境 */ public static final String DEFAULT_PROFILE = "default"; /** 条件 */ private String profile; /** 编码 */ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }
dromara/hutool
hutool-setting/src/main/java/cn/hutool/setting/profile/Profile.java
1,084
/** 编码 */
block_comment
zh-cn
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环境 */ public static final String DEFAULT_PROFILE = "default"; /** 条件 */ private String profile; /** 编码 <SUF>*/ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }
26_7
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环境 */ public static final String DEFAULT_PROFILE = "default"; /** 条件 */ private String profile; /** 编码 */ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }
dromara/hutool
hutool-setting/src/main/java/cn/hutool/setting/profile/Profile.java
1,084
/** * 默认构造,环境使用默认的:default,编码UTF-8,不使用变量 */
block_comment
zh-cn
package cn.hutool.setting.profile; import cn.hutool.core.lang.Assert; import cn.hutool.core.map.SafeConcurrentHashMap; import cn.hutool.core.util.StrUtil; import cn.hutool.setting.Setting; import java.io.Serializable; import java.nio.charset.Charset; import java.util.Map; /** * Profile可以让我们定义一系列的配置信息,然后指定其激活条件。<br> * 此类中我们规范一套规则如下:<br> * 默认的,我们读取${classpath}/default下的配置文件(*.setting文件),当调用setProfile方法时,指定一个profile,即可读取其目录下的配置文件。<br> * 比如我们定义几个profile:test,develop,production,分别代表测试环境、开发环境和线上环境,我希望读取数据库配置文件db.setting,那么: * <ol> * <li>test =》 ${classpath}/test/db.setting</li> * <li>develop =》 ${classpath}/develop/db.setting</li> * <li>production =》 ${classpath}/production/db.setting</li> * </ol> * * @author Looly * */ public class Profile implements Serializable { private static final long serialVersionUID = -4189955219454008744L; /** 默认环境 */ public static final String DEFAULT_PROFILE = "default"; /** 条件 */ private String profile; /** 编码 */ private Charset charset; /** 是否使用变量 */ private boolean useVar; /** 配置文件缓存 */ private final Map<String, Setting> settingMap = new SafeConcurrentHashMap<>(); // -------------------------------------------------------------------------------- Constructor start /** * 默认构 <SUF>*/ public Profile() { this(DEFAULT_PROFILE); } /** * 构造,编码UTF-8,不使用变量 * * @param profile 环境 */ public Profile(String profile) { this(profile, Setting.DEFAULT_CHARSET, false); } /** * 构造 * * @param profile 环境 * @param charset 编码 * @param useVar 是否使用变量 */ public Profile(String profile, Charset charset, boolean useVar) { this.profile = profile; this.charset = charset; this.useVar = useVar; } // -------------------------------------------------------------------------------- Constructor end /** * 获取当前环境下的配置文件 * * @param name 文件名,如果没有扩展名,默认为.setting * @return 当前环境下配置文件 */ public Setting getSetting(String name) { String nameForProfile = fixNameForProfile(name); Setting setting = settingMap.get(nameForProfile); if (null == setting) { setting = new Setting(nameForProfile, this.charset, this.useVar); settingMap.put(nameForProfile, setting); } return setting; } /** * 设置环境 * * @param profile 环境 * @return 自身 */ public Profile setProfile(String profile) { this.profile = profile; return this; } /** * 设置编码 * * @param charset 编码 * @return 自身 */ public Profile setCharset(Charset charset) { this.charset = charset; return this; } /** * 设置是否使用变量 * * @param useVar 变量 * @return 自身 */ public Profile setUseVar(boolean useVar) { this.useVar = useVar; return this; } /** * 清空所有环境的配置文件 * * @return 自身 */ public Profile clear() { this.settingMap.clear(); return this; } // -------------------------------------------------------------------------------- Private method start /** * 修正文件名 * * @param name 文件名 * @return 修正后的文件名 */ private String fixNameForProfile(String name) { Assert.notBlank(name, "Setting name must be not blank !"); final String actralProfile = StrUtil.nullToEmpty(this.profile); if (false == name.contains(StrUtil.DOT)) { return StrUtil.format("{}/{}.setting", actralProfile, name); } return StrUtil.format("{}/{}", actralProfile, name); } // -------------------------------------------------------------------------------- Private method end }