code
stringlengths 11
173k
| docstring
stringlengths 2
593k
| func_name
stringlengths 2
189
| language
stringclasses 1
value | repo
stringclasses 844
values | path
stringlengths 11
294
| url
stringlengths 60
339
| license
stringclasses 4
values |
---|---|---|---|---|---|---|---|
public static ParameterizedTypeName get(ParameterizedType type) {
return get(type, new LinkedHashMap<>());
} |
Returns a new {@link ParameterizedTypeName} instance for the specified {@code name} as nested
inside this class, with the specified {@code typeArguments}.
public ParameterizedTypeName nestedClass(String name, List<TypeName> typeArguments) {
checkNotNull(name, "name == null");
return new ParameterizedTypeName(this, rawType.nestedClass(name), typeArguments,
new ArrayList<>());
}
/** Returns a parameterized type, applying {@code typeArguments} to {@code rawType}.
public static ParameterizedTypeName get(ClassName rawType, TypeName... typeArguments) {
return new ParameterizedTypeName(null, rawType, Arrays.asList(typeArguments));
}
/** Returns a parameterized type, applying {@code typeArguments} to {@code rawType}.
public static ParameterizedTypeName get(Class<?> rawType, Type... typeArguments) {
return new ParameterizedTypeName(null, ClassName.get(rawType), list(typeArguments));
}
/** Returns a parameterized type equivalent to {@code type}. | ParameterizedTypeName::get | java | square/javapoet | src/main/java/com/squareup/javapoet/ParameterizedTypeName.java | https://github.com/square/javapoet/blob/master/src/main/java/com/squareup/javapoet/ParameterizedTypeName.java | Apache-2.0 |
static ParameterizedTypeName get(ParameterizedType type, Map<Type, TypeVariableName> map) {
ClassName rawType = ClassName.get((Class<?>) type.getRawType());
ParameterizedType ownerType = (type.getOwnerType() instanceof ParameterizedType)
&& !Modifier.isStatic(((Class<?>) type.getRawType()).getModifiers())
? (ParameterizedType) type.getOwnerType() : null;
List<TypeName> typeArguments = TypeName.list(type.getActualTypeArguments(), map);
return (ownerType != null)
? get(ownerType, map).nestedClass(rawType.simpleName(), typeArguments)
: new ParameterizedTypeName(null, rawType, typeArguments);
} |
Returns a new {@link ParameterizedTypeName} instance for the specified {@code name} as nested
inside this class, with the specified {@code typeArguments}.
public ParameterizedTypeName nestedClass(String name, List<TypeName> typeArguments) {
checkNotNull(name, "name == null");
return new ParameterizedTypeName(this, rawType.nestedClass(name), typeArguments,
new ArrayList<>());
}
/** Returns a parameterized type, applying {@code typeArguments} to {@code rawType}.
public static ParameterizedTypeName get(ClassName rawType, TypeName... typeArguments) {
return new ParameterizedTypeName(null, rawType, Arrays.asList(typeArguments));
}
/** Returns a parameterized type, applying {@code typeArguments} to {@code rawType}.
public static ParameterizedTypeName get(Class<?> rawType, Type... typeArguments) {
return new ParameterizedTypeName(null, ClassName.get(rawType), list(typeArguments));
}
/** Returns a parameterized type equivalent to {@code type}.
public static ParameterizedTypeName get(ParameterizedType type) {
return get(type, new LinkedHashMap<>());
}
/** Returns a parameterized type equivalent to {@code type}. | ParameterizedTypeName::get | java | square/javapoet | src/main/java/com/squareup/javapoet/ParameterizedTypeName.java | https://github.com/square/javapoet/blob/master/src/main/java/com/squareup/javapoet/ParameterizedTypeName.java | Apache-2.0 |
public Elements getElements() {
checkState(elements != null, "Not running within the rule");
return elements;
} |
Returns the {@link Elements} instance associated with the current execution of the rule.
@throws IllegalStateException if this method is invoked outside the execution of the rule.
| CompilationRule::getElements | java | square/javapoet | src/test/java/com/squareup/javapoet/TypesEclipseTest.java | https://github.com/square/javapoet/blob/master/src/test/java/com/squareup/javapoet/TypesEclipseTest.java | Apache-2.0 |
public Types getTypes() {
checkState(elements != null, "Not running within the rule");
return types;
} |
Returns the {@link Types} instance associated with the current execution of the rule.
@throws IllegalStateException if this method is invoked outside the execution of the rule.
| CompilationRule::getTypes | java | square/javapoet | src/test/java/com/squareup/javapoet/TypesEclipseTest.java | https://github.com/square/javapoet/blob/master/src/test/java/com/squareup/javapoet/TypesEclipseTest.java | Apache-2.0 |
public Integer getId() {
return id;
} |
@TableName migrations
@TableName(value = "migrations")
public class Migration implements Serializable {
/**
@TableId(type = IdType.AUTO)
private Integer id;
/**
private String migration;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
/** | Migration::getId | java | PlayEdu/PlayEdu | playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | Apache-2.0 |
public void setId(Integer id) {
this.id = id;
} |
@TableName migrations
@TableName(value = "migrations")
public class Migration implements Serializable {
/**
@TableId(type = IdType.AUTO)
private Integer id;
/**
private String migration;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
/**
public Integer getId() {
return id;
}
/** | Migration::setId | java | PlayEdu/PlayEdu | playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | Apache-2.0 |
public String getMigration() {
return migration;
} |
@TableName migrations
@TableName(value = "migrations")
public class Migration implements Serializable {
/**
@TableId(type = IdType.AUTO)
private Integer id;
/**
private String migration;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
/**
public Integer getId() {
return id;
}
/**
public void setId(Integer id) {
this.id = id;
}
/** | Migration::getMigration | java | PlayEdu/PlayEdu | playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | Apache-2.0 |
public void setMigration(String migration) {
this.migration = migration;
} |
@TableName migrations
@TableName(value = "migrations")
public class Migration implements Serializable {
/**
@TableId(type = IdType.AUTO)
private Integer id;
/**
private String migration;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
/**
public Integer getId() {
return id;
}
/**
public void setId(Integer id) {
this.id = id;
}
/**
public String getMigration() {
return migration;
}
/** | Migration::setMigration | java | PlayEdu/PlayEdu | playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-system/src/main/java/xyz/playedu/system/domain/Migration.java | Apache-2.0 |
private Log getAnnotationLog(JoinPoint joinPoint) throws Exception {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
if (method != null) {
return method.getAnnotation(Log.class);
}
return null;
} |
拦截异常操作
@param joinPoint 切点
@param e 异常
@AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e) {
handleLog(joinPoint, e, null);
}
protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) {
try {
// 获取注解信息
Log controllerLog = getAnnotationLog(joinPoint);
if (null == controllerLog) {
return;
}
AdminUser adminUser = adminUserService.findById(authService.userId());
if (null == adminUser) {
return;
}
// 日志
AdminLog adminLog = new AdminLog();
adminLog.setAdminId(adminUser.getId());
adminLog.setAdminName(adminUser.getName());
adminLog.setModule("BACKEND");
adminLog.setTitle(controllerLog.title());
adminLog.setOpt(controllerLog.businessType().ordinal());
// 设置方法名称
String className = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
adminLog.setMethod(className + "." + methodName + "()");
HttpServletRequest request = RequestUtil.handler();
if (null == request) {
return;
}
adminLog.setRequestMethod(request.getMethod());
adminLog.setUrl(request.getRequestURL().toString());
String params = "";
Map<String, String[]> parameterMap = request.getParameterMap();
if (StringUtil.isNotEmpty(parameterMap)) {
params = JSONUtil.toJsonStr(parameterMap);
} else {
Object[] args = joinPoint.getArgs();
if (StringUtil.isNotNull(args)) {
params = StringUtil.arrayToString(args);
}
}
if (StringUtil.isNotEmpty(params)) {
JSONObject paramObj = excludeProperties(params);
adminLog.setParam(JSONUtil.toJsonStr(paramObj));
}
if (null != jsonResult) {
jsonResult = excludeProperties(JSONUtil.toJsonStr(jsonResult));
adminLog.setResult(JSONUtil.toJsonStr(jsonResult));
}
adminLog.setIp(IpUtil.getIpAddress());
adminLog.setIpArea(IpUtil.getRealAddressByIP(IpUtil.getIpAddress()));
if (null != e) {
adminLog.setErrorMsg(e.getMessage());
}
adminLog.setCreatedAt(new Date());
// 保存数据库
adminLogService.save(adminLog);
} catch (Exception exp) {
// 记录本地异常日志
log.error("异常信息:" + exp.getMessage(), e);
}
}
/** 是否存在注解,如果存在就获取 | AdminLogAspect::getAnnotationLog | java | PlayEdu/PlayEdu | playedu-api/playedu-system/src/main/java/xyz/playedu/system/aspectj/AdminLogAspect.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-system/src/main/java/xyz/playedu/system/aspectj/AdminLogAspect.java | Apache-2.0 |
public static String desValue(
String origin, int prefixNoMaskLen, int suffixNoMaskLen, String maskStr) {
if (origin == null) {
return null;
}
StringBuilder sb = new StringBuilder();
for (int i = 0, n = origin.length(); i < n; i++) {
if (i < prefixNoMaskLen) {
sb.append(origin.charAt(i));
continue;
}
if (i > (n - suffixNoMaskLen - 1)) {
sb.append(origin.charAt(i));
continue;
}
sb.append(maskStr);
}
return sb.toString();
} |
对字符串进行脱敏操作
@param origin 原始字符串
@param prefixNoMaskLen 左侧需要保留几位明文字段
@param suffixNoMaskLen 右侧需要保留几位明文字段
@param maskStr 用于遮罩的字符串, 如'*'
@return 脱敏后结果
| PrivacyUtil::desValue | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/PrivacyUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/PrivacyUtil.java | Apache-2.0 |
public static <T> T nvl(T value, T defaultValue) {
return value != null ? value : defaultValue;
} |
获取参数不为空值
@param value defaultValue 要判断的value
@return value 返回值
| StringUtil::nvl | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isEmpty(Collection<?> coll) {
return isNull(coll) || coll.isEmpty();
} |
判断一个Collection是否为空,包含List, Set, Queue
@param coll 要判断的Collection
@return true=为空, false=非空
| StringUtil::isEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isNotEmpty(Collection<?> coll) {
return !isEmpty(coll);
} |
判断一个Collection是否非空,包含List, Set, Queue
@param coll 要判断的Collection
@return true=非空, false=空
| StringUtil::isNotEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isEmpty(Object[] objects) {
return isNull(objects) || (objects.length == 0);
} |
判断一个对象数组是否为空
@param objects 要判断的对象数组
@return true=为空, false=非空
| StringUtil::isEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isNotEmpty(Object[] objects) {
return !isEmpty(objects);
} |
判断一个对象数组是否非空
@param objects 要判断的对象数组
@return true=非空, false=空
| StringUtil::isNotEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isEmpty(Map<?, ?> map) {
return isNull(map) || map.isEmpty();
} |
判断一个Map是否为空
@param map 要判断的Map
@return true=为空, false=非空
| StringUtil::isEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isNotEmpty(Map<?, ?> map) {
return !isEmpty(map);
} |
判断一个Map是否为空
@param map 要判断的Map
@return true=非空, false=空
| StringUtil::isNotEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isEmpty(String str) {
return isNull(str) || NULL_STR.equals(str.trim());
} |
判断一个字符串是否为空串
@param str String
@return true=为空, false=非空
| StringUtil::isEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isNotEmpty(String str) {
return !isEmpty(str);
} |
判断一个字符串是否为非空串
@param str String
@return true=非空串, false=空串
| StringUtil::isNotEmpty | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isNull(Object object) {
return object == null;
} |
判断一个对象是否为空
@param object Object
@return true=为空, false=非空
| StringUtil::isNull | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isNotNull(Object object) {
return !isNull(object);
} |
判断一个对象是否非空
@param object Object
@return true=非空, false=空
| StringUtil::isNotNull | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isArray(Object object) {
return isNotNull(object) && object.getClass().isArray();
} |
判断一个对象是否是数组类型(Java基本型别的数组)
@param object 对象
@return true=是数组, false=不是数组
| StringUtil::isArray | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String trim(String str) {
return (str == null ? "" : str.trim());
} |
判断一个对象是否是数组类型(Java基本型别的数组)
@param object 对象
@return true=是数组, false=不是数组
public static boolean isArray(Object object) {
return isNotNull(object) && object.getClass().isArray();
}
/** 去空格 | StringUtil::trim | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String substring(final String str, int start) {
if (str == null) {
return NULL_STR;
}
if (start < 0) {
start = str.length() + start;
}
if (start < 0) {
start = 0;
}
if (start > str.length()) {
return NULL_STR;
}
return str.substring(start);
} |
截取字符串
@param str 字符串
@param start 开始
@return 结果
| StringUtil::substring | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String substring(final String str, int start, int end) {
if (str == null) {
return NULL_STR;
}
if (end < 0) {
end = str.length() + end;
}
if (start < 0) {
start = str.length() + start;
}
if (end > str.length()) {
end = str.length();
}
if (start > end) {
return NULL_STR;
}
if (start < 0) {
start = 0;
}
if (end < 0) {
end = 0;
}
return str.substring(start, end);
} |
截取字符串
@param str 字符串
@param start 开始
@param end 结束
@return 结果
| StringUtil::substring | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static Set<String> str2Set(String str, String sep) {
return new HashSet<String>(str2List(str, sep, true, false));
} |
字符串转set
@param str 字符串
@param sep 分隔符
@return set集合
| StringUtil::str2Set | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static List<String> str2List(String str, String sep, boolean filterBlank, boolean trim) {
List<String> list = new ArrayList<String>();
if (StringUtil.isEmpty(str)) {
return list;
}
// 过滤空白字符串
if (filterBlank && StringUtil.isBlank(str)) {
return list;
}
String[] split = str.split(sep);
for (String string : split) {
if (filterBlank && StringUtil.isBlank(string)) {
continue;
}
if (trim) {
string = string.trim();
}
list.add(string);
}
return list;
} |
字符串转list
@param str 字符串
@param sep 分隔符
@param filterBlank 过滤纯空白
@param trim 去掉首尾空白
@return list集合
| StringUtil::str2List | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean containsAnyIgnoreCase(
CharSequence cs, CharSequence... searchCharSequences) {
if (isEmpty(cs) || isEmpty(searchCharSequences)) {
return false;
}
for (CharSequence testStr : searchCharSequences) {
if (containsIgnoreCase(cs, testStr)) {
return true;
}
}
return false;
} |
查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写
@param cs 指定字符串
@param searchCharSequences 需要检查的字符串数组
@return 是否包含任意一个字符串
| StringUtil::containsAnyIgnoreCase | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String toUnderScoreCase(String str) {
if (str == null) {
return null;
}
StringBuilder sb = new StringBuilder();
// 前置字符是否大写
boolean preCharIsUpperCase = true;
// 当前字符是否大写
boolean cureCharIsUpperCase = true;
// 下一字符是否大写
boolean nextCharIsUpperCase = true;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (i > 0) {
preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
} else {
preCharIsUpperCase = false;
}
cureCharIsUpperCase = Character.isUpperCase(c);
if (i < (str.length() - 1)) {
nextCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
}
if (preCharIsUpperCase && cureCharIsUpperCase && !nextCharIsUpperCase) {
sb.append(SEPARATOR);
} else if ((i != 0 && !preCharIsUpperCase) && cureCharIsUpperCase) {
sb.append(SEPARATOR);
}
sb.append(Character.toLowerCase(c));
}
return sb.toString();
} |
查找指定字符串是否包含指定字符串列表中的任意一个字符串同时串忽略大小写
@param cs 指定字符串
@param searchCharSequences 需要检查的字符串数组
@return 是否包含任意一个字符串
public static boolean containsAnyIgnoreCase(
CharSequence cs, CharSequence... searchCharSequences) {
if (isEmpty(cs) || isEmpty(searchCharSequences)) {
return false;
}
for (CharSequence testStr : searchCharSequences) {
if (containsIgnoreCase(cs, testStr)) {
return true;
}
}
return false;
}
/** 驼峰转下划线命名 | StringUtil::toUnderScoreCase | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean inStringIgnoreCase(String str, String... strArr) {
if (str != null && strArr != null) {
for (String s : strArr) {
if (str.equalsIgnoreCase(trim(s))) {
return true;
}
}
}
return false;
} |
是否包含字符串
@param str 验证字符串
@param strArr 字符串组
@return 包含返回true
| StringUtil::inStringIgnoreCase | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String convertToCamelCase(String name) {
StringBuilder result = new StringBuilder();
// 快速检查
if (name == null || name.isEmpty()) {
// 没必要转换
return "";
} else if (!name.contains("_")) {
// 不含下划线,仅将首字母大写
return name.substring(0, 1).toUpperCase() + name.substring(1);
}
// 用下划线将原始字符串分割
String[] camels = name.split("_");
for (String camel : camels) {
// 跳过原始字符串中开头、结尾的下换线或双重下划线
if (camel.isEmpty()) {
continue;
}
// 首字母大写
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
return result.toString();
} |
将下划线大写方式命名的字符串转换为驼峰式。 如果转换前的下划线大写方式命名的字符串为空, 则返回空字符串。 例如:HELLO_WORLD->HelloWorld
@param name 转换前的下划线大写方式命名的字符串
@return 转换后的驼峰式命名的字符串
| StringUtil::convertToCamelCase | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == SEPARATOR) {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
} |
驼峰式命名法 例如:user_name->userName
@param s 字符串
@return 驼峰字符串
| StringUtil::toCamelCase | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean matches(String str, List<String> strArr) {
if (isEmpty(str) || isEmpty(strArr)) {
return false;
}
for (String pattern : strArr) {
if (isMatch(pattern, str)) {
return true;
}
}
return false;
} |
查找指定字符串是否匹配指定字符串列表中的任意一个字符串
@param str 指定字符串
@param strArr 需要检查的字符串数组
@return 是否匹配
| StringUtil::matches | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static boolean isMatch(String pattern, String url) {
AntPathMatcher matcher = new AntPathMatcher();
return matcher.match(pattern, url);
} |
判断url是否与规则配置: ? 表示单个字符; * 表示一层路径内的任意字符串,不可跨层级; ** 表示任意层路径;
@param pattern 匹配规则
@param url 需要匹配的url
@return boolean
| StringUtil::isMatch | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String padL(final Number num, final int size) {
return padL(num.toString(), size, '0');
} |
数字左边补齐0,使之达到指定长度。 注意,如果数字转换为字符串后,长度大于size,则只保留 最后size个字符。
@param num 数字对象
@param size 字符串指定长度
@return 返回数字的字符串格式,该字符串为指定长度。
| StringUtil::padL | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String padL(final String s, final int size, final char c) {
final StringBuilder sb = new StringBuilder(size);
if (s != null) {
final int len = s.length();
if (s.length() <= size) {
for (int i = size - len; i > 0; i--) {
sb.append(c);
}
sb.append(s);
} else {
return s.substring(len - size, len);
}
} else {
for (int i = size; i > 0; i--) {
sb.append(c);
}
}
return sb.toString();
} |
字符串左补齐 如果原始字符串s长度大于size,则只保留最后size个字符。
@param s 原始字符串
@param size 字符串指定长度
@param c 用于补齐的字符
@return 返回指定长度的字符串,由原字符串左补齐或截取得到。
| StringUtil::padL | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String format(String strPattern, Object... argArray) {
String EMPTY_JSON = "{}";
char C_BACKSLASH = '\\';
char C_DELIM_START = '{';
if (isEmpty(argArray) || isEmpty(strPattern)) {
return strPattern;
}
final int strPatternLength = strPattern.length();
StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
int handledPosition = 0;
int delimIndex;
for (int argIndex = 0; argIndex < argArray.length; argIndex++) {
delimIndex = strPattern.indexOf(EMPTY_JSON, handledPosition);
if (delimIndex == -1) {
if (handledPosition == 0) {
return strPattern;
} else {
sbuf.append(strPattern, handledPosition, strPatternLength);
return sbuf.toString();
}
} else {
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) {
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) {
sbuf.append(strPattern, handledPosition, delimIndex - 1);
sbuf.append(argArray[argIndex]);
handledPosition = delimIndex + 2;
} else {
// 占位符被转义
argIndex--;
sbuf.append(strPattern, handledPosition, delimIndex - 1);
sbuf.append(C_DELIM_START);
handledPosition = delimIndex + 1;
}
} else {
// 正常占位符
sbuf.append(strPattern, handledPosition, delimIndex);
sbuf.append(argArray[argIndex]);
handledPosition = delimIndex + 2;
}
}
}
sbuf.append(strPattern, handledPosition, strPattern.length());
return sbuf.toString();
} |
格式化文本, {} 表示占位符<br>
此方法只是简单将占位符 {} 按照顺序替换为参数<br>
如果想输出 {} 使用 \\转义 { 即可,如果想输出 {} 之前的 \ 使用双转义符 \\\\ 即可<br>
例:<br>
通常使用:format("this is {} for {}", "a", "b") -> this is a for b<br>
转义{}: format("this is \\{} for {}", "a", "b") -> this is \{} for a<br>
转义\: format("this is \\\\{} for {}", "a", "b") -> this is \a for b<br>
@param strPattern 文本模板,被替换的部分用 {} 表示
@param argArray 参数值
@return 格式化后的文本
| StringUtil::format | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/StringUtil.java | Apache-2.0 |
public static String getIpAddress() {
HttpServletRequest request = RequestUtil.handler();
if (request == null) {
return "unknown";
}
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Forwarded-For");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("X-Real-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
} |
获取客户端IP
@return IP地址 (113.67.10.194)
@author fzr
| IpUtil::getIpAddress | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
public static String getRealAddressByIP(String ip) {
String IP_URL = "https://whois.pconline.com.cn/ipJson.jsp";
String UNKNOWN = "未知";
if (IpUtil.internalIp(ip)) {
return "内网";
}
try {
String rspStr =
HttpUtil.get(
IP_URL,
new HashMap<>() {
{
put("ip", ip);
put("json", true);
}
});
if (StringUtil.isEmpty(rspStr)) {
log.error("获取地理位置异常1 {}", ip);
return UNKNOWN;
}
JSONObject json = JSONUtil.parseObj(rspStr);
return String.format("%s-%s", json.getStr("pro"), json.getStr("city"));
} catch (Exception e) {
log.error("获取地理位置异常2 {} msg {}", ip, e.getMessage());
}
return UNKNOWN;
} |
根据IP获取所在地址
@param ip Ip地址
@return String (广州省-广州市)
@author fzr
| IpUtil::getRealAddressByIP | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
public static boolean internalIp(String ip) {
byte[] address = textToNumericFormatV4(ip);
return internalIp(address) || "127.0.0.1".equals(ip);
} |
检查是否为内部IP地址
@param ip IP地址
@return 结果
| IpUtil::internalIp | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
private static boolean internalIp(byte[] address) {
if (address == null || address.length < 2) {
return true;
}
final byte b0 = address[0];
final byte b1 = address[1];
// 10.x.x.x/8
final byte SECTION_1 = 0x0A;
// 172.16.x.x/12
final byte SECTION_2 = (byte) 0xAC;
final byte SECTION_3 = (byte) 0x10;
final byte SECTION_4 = (byte) 0x1F;
// 192.168.x.x/16
final byte SECTION_5 = (byte) 0xC0;
final byte SECTION_6 = (byte) 0xA8;
switch (b0) {
case SECTION_1:
return true;
case SECTION_2:
if (b1 >= SECTION_3 && b1 <= SECTION_4) {
return true;
}
case SECTION_5:
if (b1 == SECTION_6) {
return true;
}
default:
return false;
}
} |
检查是否为内部IP地址
@param address byte地址
@return 结果
@author fzr
| IpUtil::internalIp | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
public static byte[] textToNumericFormatV4(String text) {
if (text.length() == 0) {
return null;
}
byte[] bytes = new byte[4];
String[] elements = text.split("\\.", -1);
try {
long l;
int i;
switch (elements.length) {
case 1:
l = Long.parseLong(elements[0]);
if ((l < 0L) || (l > 4294967295L)) {
return null;
}
bytes[0] = (byte) (int) (l >> 24 & 0xFF);
bytes[1] = (byte) (int) ((l & 0xFFFFFF) >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 2:
l = Integer.parseInt(elements[0]);
if ((l < 0L) || (l > 255L)) {
return null;
}
bytes[0] = (byte) (int) (l & 0xFF);
l = Integer.parseInt(elements[1]);
if ((l < 0L) || (l > 16777215L)) {
return null;
}
bytes[1] = (byte) (int) (l >> 16 & 0xFF);
bytes[2] = (byte) (int) ((l & 0xFFFF) >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 3:
for (i = 0; i < 2; ++i) {
l = Integer.parseInt(elements[i]);
if ((l < 0L) || (l > 255L)) {
return null;
}
bytes[i] = (byte) (int) (l & 0xFF);
}
l = Integer.parseInt(elements[2]);
if ((l < 0L) || (l > 65535L)) {
return null;
}
bytes[2] = (byte) (int) (l >> 8 & 0xFF);
bytes[3] = (byte) (int) (l & 0xFF);
break;
case 4:
for (i = 0; i < 4; ++i) {
l = Integer.parseInt(elements[i]);
if ((l < 0L) || (l > 255L)) {
return null;
}
bytes[i] = (byte) (int) (l & 0xFF);
}
break;
default:
return null;
}
} catch (NumberFormatException e) {
return null;
}
return bytes;
} |
将IPv4地址转换成字节
@param text IPv4地址
@return byte 字节
@author fzr
| IpUtil::textToNumericFormatV4 | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
public static String getHostIp() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException ignored) {
}
return "127.0.0.1";
} |
获取IP地址
@return 本地IP地址
@author fzr
| IpUtil::getHostIp | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
public static String getMultistageReverseProxyIp(String ip) {
if (ip != null && ip.indexOf(",") > 0) {
final String[] ips = ip.trim().split(",");
for (String subIp : ips) {
if (!isUnknown(subIp)) {
ip = subIp;
break;
}
}
}
return ip;
} |
从多级反向代理中获得第一个非unknown IP地址
@param ip 获得的IP地址
@return 第一个非unknown IP地址
@author fzr
| IpUtil::getMultistageReverseProxyIp | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
public static boolean isUnknown(String checkString) {
return StringUtil.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
} |
检测给定字符串是否为未知,多用于检测HTTP请求相关
@param checkString 被检测的字符串
@return 是否未知
@author fzr
| IpUtil::isUnknown | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/IpUtil.java | Apache-2.0 |
public static String uuid() {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
} |
制作UUID
@return String
@author fzr
| HelperUtil::uuid | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | Apache-2.0 |
public static String randomString(int length) {
Random random = new Random();
StringBuilder stringBuffer = new StringBuilder();
String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int strLength = str.length();
for (int i = 0; i < length; i++) {
int index = random.nextInt(strLength);
stringBuffer.append(str.charAt(index));
}
return stringBuffer.toString();
} |
返回随机字符串
@param length 要生成的长度
@return String
@author fzr
| HelperUtil::randomString | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | Apache-2.0 |
public static String randomInt(int length) {
Random random = new Random();
StringBuilder stringBuffer = new StringBuilder();
String str = "0123456789";
for (int i = 0; i < length; i++) {
int index = random.nextInt(10);
stringBuffer.append(str.charAt(index));
}
return stringBuffer.toString();
} |
返回随机数字字符串
@param length 要生成的长度
@return String
@author fzr
| HelperUtil::randomInt | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | Apache-2.0 |
public static String storageUnit(Long size) {
if (size == null) {
return "0B";
}
if (size < 1024) {
return size + "B";
} else {
size = size / 1024;
}
if (size < 1024) {
return size + "KB";
} else {
size = size / 1024;
}
if (size < 1024) {
size = size * 100;
return (size / 100) + "." + (size % 100) + "MB";
} else {
size = size * 100 / 1024;
return (size / 100) + "." + (size % 100) + "GB";
}
} |
转换存储单位: KB MB GB TB
@return String
@author fzr
| HelperUtil::storageUnit | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | Apache-2.0 |
public static void download(String urlString, String savePath, String filename)
throws IOException {
URL url = new URL(urlString);
URLConnection con = url.openConnection();
con.setConnectTimeout(20 * 1000);
File sf = new File(savePath);
if (!sf.exists()) {
if (sf.mkdirs()) {
throw new IOException("创建目录失败");
}
}
try (InputStream in = con.getInputStream();
OutputStream out = new FileOutputStream(sf.getPath() + "\\" + filename)) {
byte[] buff = new byte[1024];
int n;
while ((n = in.read(buff)) >= 0) {
out.write(buff, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
} |
下载文件
@param urlString (文件网址)
@param savePath (保存路径,如: /www/uploads)
@param filename (保存名称,如: aa.png)
@throws IOException 异常
@author fzr
| HelperUtil::download | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | Apache-2.0 |
public static Map<String, Object> mergeMapByObj(
Map<String, Object> map, Map<String, Object> map1) {
HashMap<String, Object> map2 = new HashMap<>();
map2.putAll(map);
map2.putAll(map1);
return map2;
} |
对象类型Map合并
@param map 对象
@return Object
@author fzr
| HelperUtil::mergeMapByObj | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | Apache-2.0 |
public static Map<String, String> mergeMapByStr(
Map<String, String> map, Map<String, String> map1) {
HashMap<String, String> map2 = new HashMap<>();
map2.putAll(map);
map2.putAll(map1);
return map2;
} |
字符串类型Map合并
@param map 对象
@return Object
@author fzr
| HelperUtil::mergeMapByStr | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/HelperUtil.java | Apache-2.0 |
public static RedisTemplate<String, Object> handler() {
return redisTemplate;
} |
对象句柄
@return RedisTemplate
@author fzr
| RedisUtil::handler | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void expire(String key, Long second) {
key = redisPrefix + key;
redisTemplate.expire(key, second, TimeUnit.SECONDS);
} |
指定缓存失效时间
@param key 键
@param second 时间(秒)
@author fzr
| RedisUtil::expire | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void pExpire(String key, Long millisecond) {
key = redisPrefix + key;
redisTemplate.expire(key, millisecond, TimeUnit.MILLISECONDS);
} |
指定缓存失效时间
@param key 键
@param millisecond 时间(毫秒)
@author fzr
| RedisUtil::pExpire | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void persist(String key) {
key = redisPrefix + key;
redisTemplate.persist(key);
} |
指定缓存永久有效
@param key 键
@author fzr
| RedisUtil::persist | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Long ttl(String key) {
key = redisPrefix + key;
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
} |
根据key获取过期时间
@param key 键不能为null
@return 返回0代表为永久有效(秒)
@author fzr
| RedisUtil::ttl | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Long pTtl(String key) {
key = redisPrefix + key;
return redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
} |
根据key获取过期时间
@param key 键不能为null
@return 返回0代表为永久有效(毫秒)
@author fzr
| RedisUtil::pTtl | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Boolean exists(String key) {
key = redisPrefix + key;
return redisTemplate.hasKey(key);
} |
判断key是否存在
@param key 键
@return true=存在,false=不存在
@author fzr
| RedisUtil::exists | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void rename(String oldKey, String newKey) {
oldKey = redisPrefix + oldKey;
newKey = redisPrefix + newKey;
redisTemplate.rename(oldKey, newKey);
} |
给key赋值一个新的key名
@param oldKey 旧的key
@param newKey 新的key
@author fzr
| RedisUtil::rename | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Boolean move(String key, int db) {
key = redisPrefix + key;
return redisTemplate.move(key, db);
} |
将当前数据库的key移动到给定的数据库db当中
@param key 键
@param db 库
@return Boolean
@author fzr
| RedisUtil::move | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
} |
获取匹配的key值
@param pattern 通配符(*, ?, [])
@return Set
@author fzr
@author fzr
| RedisUtil::keys | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static String randomKey() {
return redisTemplate.randomKey();
} |
随机返回一个key
@return String
@author fzr
@author fzr
| RedisUtil::randomKey | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Set<String> matchSet(String pattern) {
Set<String> keys = new LinkedHashSet<>();
RedisUtil.handler()
.execute(
(RedisConnection connection) -> {
try (Cursor<byte[]> cursor =
connection.scan(
ScanOptions.scanOptions()
.count(Long.MAX_VALUE)
.match(pattern)
.build())) {
cursor.forEachRemaining(
item -> {
keys.add(RedisSerializer.string().deserialize(item));
});
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
});
return keys;
} |
按匹配获取或有KEY
@param pattern 规则
@return Set<String>
@author fzr
| RedisUtil::matchSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Object get(String key) {
key = redisPrefix + key;
return redisTemplate.opsForValue().get(key);
} |
获取key的值
@param key 键
@return Object
@author fzr
| RedisUtil::get | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Object getSet(String key, Object newVal) {
key = redisPrefix + key;
return redisTemplate.opsForValue().getAndSet(key, newVal);
} |
获取旧值并设置新值
@param key 键
@param newVal 新值
@return Object
@author fzr
| RedisUtil::getSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void set(String key, Object value) {
key = redisPrefix + key;
redisTemplate.opsForValue().set(key, value);
} |
设置键值对
@param key 键
@param value 值
@author fzr
| RedisUtil::set | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void set(String key, Object value, long time) {
key = redisPrefix + key;
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
} |
设置键值对并设置时间
@param key 键
@param value 值
@param time time要大于0 如果time小于等于0 将设置无限期
@author fzr
| RedisUtil::set | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Long incr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递增因子必须大于0");
}
key = redisPrefix + key;
return redisTemplate.opsForValue().increment(key, delta);
} |
递增
@param key 键
@param delta 要增加几(大于0)
@return Long
@author fzr
| RedisUtil::incr | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Long decr(String key, long delta) {
if (delta < 0) {
throw new RuntimeException("递减因子必须大于0");
}
key = redisPrefix + key;
return redisTemplate.opsForValue().increment(key, -delta);
} |
递减
@param key 键
@param delta 要减少几(小于0)
@return Long
@author fzr
| RedisUtil::decr | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Object hGet(String key, String field) {
key = redisPrefix + key;
return redisTemplate.opsForHash().get(key, field);
} |
获取key中field域的值
@param key 键 不能为null
@param field 项 不能为null
@return 值
@author fzr
| RedisUtil::hGet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Boolean hExists(String key, Object field) {
key = redisPrefix + key;
return redisTemplate.opsForHash().hasKey(key, field);
} |
判断key中有没有field域名
@param key 键
@param field 字段
@return Boolean
@author fzr
| RedisUtil::hExists | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Map<Object, Object> hmGet(String key) {
key = redisPrefix + key;
return redisTemplate.opsForHash().entries(key);
} |
获取hashKey对应的所有键值
@param key 键
@return 对应的多个键值
@author fzr
| RedisUtil::hmGet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void hmSet(String key, Map<String, Object> map) {
key = redisPrefix + key;
redisTemplate.opsForHash().putAll(key, map);
} |
设置field1->N个域,对应的值是value1->N
@param key 键
@param map 对应多个键值
@author fzr
| RedisUtil::hmSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void hmSet(String key, Map<String, Object> map, long time) {
key = redisPrefix + key;
redisTemplate.opsForHash().putAll(key, map);
if (time > 0) {
expire(key, time);
}
} |
HashSet 并设置时间
@param key 键
@param map 对应多个键值
@param time 时间(秒)
@author fzr
| RedisUtil::hmSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void hSet(String key, String item, Object value) {
key = redisPrefix + key;
redisTemplate.opsForHash().put(key, item, value);
} |
向一张hash表中放入数据,如果不存在将创建
@param key 键
@param item 项
@param value 值
@author fzr
| RedisUtil::hSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static boolean hSet(String key, String item, Object value, long time) {
key = redisPrefix + key;
redisTemplate.opsForHash().put(key, item, value);
if (time > 0) {
expire(key, time);
}
return true;
} |
向一张hash表中放入数据,如果不存在将创建
@param key 键
@param item 项
@param value 值
@param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间
@return true 成功 false失败
@author fzr
| RedisUtil::hSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static void hDel(String key, Object... item) {
key = redisPrefix + key;
redisTemplate.opsForHash().delete(key, item);
} |
删除hash表中的值
@param key 键 不能为null
@param item 项 可以使多个 不能为null
@author fzr
| RedisUtil::hDel | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static boolean hHasKey(String key, String item) {
key = redisPrefix + key;
return redisTemplate.opsForHash().hasKey(key, item);
} |
判断hash表中是否有该项的值
@param key 键 不能为null
@param item 项 不能为null
@return true 存在 false不存在
@author fzr
| RedisUtil::hHasKey | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static double hIncr(String key, String item, long by) {
key = redisPrefix + key;
return redisTemplate.opsForHash().increment(key, item, by);
} |
hash递增 如果不存在,就会创建一个并把新增后的值返回
@param key 键
@param item 项
@param by 要增加几(大于0)
@return double
@author fzr
| RedisUtil::hIncr | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static double hDecr(String key, String item, long by) {
key = redisPrefix + key;
return redisTemplate.opsForHash().increment(key, item, -by);
} |
hash递减
@param key 键
@param item 项
@param by 要减少记(小于0)
@return double
@author fzr
| RedisUtil::hDecr | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Set<Object> sGet(String key) {
key = redisPrefix + key;
return redisTemplate.opsForSet().members(key);
} |
根据key获取Set中的所有值
@param key 键
@return Set
@author fzr
| RedisUtil::sGet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Boolean sHasKey(String key, Object value) {
key = redisPrefix + key;
return redisTemplate.opsForSet().isMember(key, value);
} |
根据value从一个set中查询,是否存在
@param key 键
@param value 值
@return true 存在 false不存在
@author fzr
| RedisUtil::sHasKey | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static Long sSet(String key, Object... values) {
key = redisPrefix + key;
return redisTemplate.opsForSet().add(key, values);
} |
将数据放入set缓存
@param key 键
@param values 值 可以是多个
@return 成功个数
@author fzr
| RedisUtil::sSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Long sSetAndTime(String key, long time, Object... values) {
key = redisPrefix + key;
return redisTemplate.opsForSet().add(key, values);
} |
将set数据放入缓存
@param key 键
@param time 时间(秒)
@param values 值 可以是多个
@return 成功个数
@author fzr
| RedisUtil::sSetAndTime | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Long sGetSetSize(String key) {
key = redisPrefix + key;
return redisTemplate.opsForSet().size(key);
} |
获取set缓存的长度
@param key 键
@return Long
@author fzr
| RedisUtil::sGetSetSize | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Long setRemove(String key, Object... values) {
key = redisPrefix + key;
return redisTemplate.opsForSet().remove(key, values);
} |
移除值为value的
@param key 键
@param values 值 可以是多个
@return 移除的个数
@author fzr
| RedisUtil::setRemove | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public List<Object> lGet(String key, long start, long end) {
key = redisPrefix + key;
return redisTemplate.opsForList().range(key, start, end);
} |
获取list缓存的内容
@param key 键
@param start 开始
@param end 结束 0 到 -1代表所有值
@return List
@author fzr
| RedisUtil::lGet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Long lGetListSize(String key) {
key = redisPrefix + key;
return redisTemplate.opsForList().size(key);
} |
获取list缓存的长度
@param key 键
@return Long
@author fzr
| RedisUtil::lGetListSize | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Object lGetIndex(String key, long index) {
key = redisPrefix + key;
return redisTemplate.opsForList().index(key, index);
} |
通过索引获取list中的值
@param key 键
@param index 索引 index>=0时,0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推
@return Object
@author fzr
| RedisUtil::lGetIndex | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public boolean lSet(String key, Object value) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPush(key, value);
return true;
} |
将list放入缓存
@param key 键
@param value 值
@return boolean
@author fzr
| RedisUtil::lSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public boolean lSet(String key, Object value, long second) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPush(key, value);
if (second > 0) expire(key, second);
return true;
} |
将list放入缓存
@param key 键
@param value 值
@param second 时间(秒)
@return boolean
@author fzr
| RedisUtil::lSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public boolean lSet(String key, List<Object> value) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPushAll(key, value);
return true;
} |
将list放入缓存
@param key 键
@param value 值
@return boolean
@author fzr
| RedisUtil::lSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public boolean lSet(String key, List<Object> value, Long time) {
key = redisPrefix + key;
redisTemplate.opsForList().rightPushAll(key, value);
if (time > 0) expire(key, time);
return true;
} |
将list放入缓存
@param key 键
@param value 值
@param time 时间(秒)
@return boolean
@author fzr
| RedisUtil::lSet | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public boolean lUpdateIndex(String key, Long index, Object value) {
key = redisPrefix + key;
redisTemplate.opsForList().set(key, index, value);
return true;
} |
根据索引修改list中的某条数据
@param key 键
@param index 索引
@param value 值
@return boolean
@author fzr
| RedisUtil::lUpdateIndex | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public Long lRemove(String key, Long count, Object value) {
key = redisPrefix + key;
return redisTemplate.opsForList().remove(key, count, value);
} |
移除N个值为value
@param key 键
@param count 移除多少个
@param value 值
@return 移除的个数
@author fzr
| RedisUtil::lRemove | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/RedisUtil.java | Apache-2.0 |
public static String encode(byte[] binaryData) {
if (binaryData == null) {
return null;
}
int lengthDataBits = binaryData.length * EIGHT_BIT;
if (lengthDataBits == 0) {
return "";
}
int fewerThan24bits = lengthDataBits % TWENTY_FOUR_BIT_GROUP;
int numberTriplets = lengthDataBits / TWENTY_FOUR_BIT_GROUP;
int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets;
char[] encodedData;
encodedData = new char[numberQuartet * 4];
byte k, l, b1, b2, b3;
int encodedIndex = 0;
int dataIndex = 0;
for (int i = 0; i < numberTriplets; i++) {
b1 = binaryData[dataIndex++];
b2 = binaryData[dataIndex++];
b3 = binaryData[dataIndex++];
k = (byte) (b1 & 0x03);
l = (byte) (b2 & 0x0f);
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc);
encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];
encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];
}
if (fewerThan24bits == EIGHT_BIT) {
b1 = binaryData[dataIndex];
k = (byte) (b1 & 0x03);
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
encodedData[encodedIndex++] = PAD;
encodedData[encodedIndex++] = PAD;
} else if (fewerThan24bits == SIXTEEN_BIT) {
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex + 1];
l = (byte) (b2 & 0x0f);
k = (byte) (b1 & 0x03);
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0);
byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0);
encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];
encodedData[encodedIndex++] = PAD;
}
return new String(encodedData);
} |
转码
@param binaryData 未转码数据
@return 转码后数据串
| Base64Util::encode | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | Apache-2.0 |
public static byte[] decode(String encoded) {
if (encoded == null) {
return null;
}
char[] base64Data = encoded.toCharArray();
int len = removeWhiteSpace(base64Data);
if (len % FOUR_BYTE != 0) {
return null;
}
int numberQuadruple = (len / FOUR_BYTE);
if (numberQuadruple == 0) {
return new byte[0];
}
byte[] decodedData;
byte b1, b2, b3, b4;
char d1, d2, d3, d4;
int i = 0;
int encodedIndex = 0;
int dataIndex = 0;
decodedData = new byte[(numberQuadruple) * 3];
for (; i < numberQuadruple - 1; i++) {
if (!isData((d1 = base64Data[dataIndex++]))
|| !isData((d2 = base64Data[dataIndex++]))
|| !isData((d3 = base64Data[dataIndex++]))
|| !isData((d4 = base64Data[dataIndex++]))) {
return null;
}
b1 = base64Alphabet[d1];
b2 = base64Alphabet[d2];
b3 = base64Alphabet[d3];
b4 = base64Alphabet[d4];
decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);
}
if (!isData((d1 = base64Data[dataIndex++])) || !isData((d2 = base64Data[dataIndex++]))) {
return null;
}
b1 = base64Alphabet[d1];
b2 = base64Alphabet[d2];
d3 = base64Data[dataIndex++];
d4 = base64Data[dataIndex++];
if (!isData((d3)) || !isData((d4))) {
if (isPad(d3) && isPad(d4)) {
if ((b2 & 0xf) != 0) {
return null;
}
byte[] tmp = new byte[i * 3 + 1];
System.arraycopy(decodedData, 0, tmp, 0, i * 3);
tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4);
return tmp;
} else if (!isPad(d3) && isPad(d4)) {
b3 = base64Alphabet[d3];
if ((b3 & 0x3) != 0) {
return null;
}
byte[] tmp = new byte[i * 3 + 2];
System.arraycopy(decodedData, 0, tmp, 0, i * 3);
tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
return tmp;
} else {
return null;
}
} else {
b3 = base64Alphabet[d3];
b4 = base64Alphabet[d4];
decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4);
decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf));
decodedData[encodedIndex++] = (byte) (b4 | b3 << 6);
}
return decodedData;
} |
解码
@param encoded 已转码数据
@return 解码后数据
| Base64Util::decode | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | Apache-2.0 |
private static boolean isWhiteSpace(char octEct) {
return (octEct == 0x20 || octEct == 0xd || octEct == 0xa || octEct == 0x9);
} |
是否空白
@param octEct 位
@return boolean
| Base64Util::isWhiteSpace | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | Apache-2.0 |
private static boolean isPad(char octEct) {
return (octEct == PAD);
} |
是否填充
@param octEct 位
@return boolean
| Base64Util::isPad | java | PlayEdu/PlayEdu | playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | https://github.com/PlayEdu/PlayEdu/blob/master/playedu-api/playedu-common/src/main/java/xyz/playedu/common/util/Base64Util.java | Apache-2.0 |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.