查看“Java 自定义方法 - BASE”的源代码
←
Java 自定义方法 - BASE
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
Java 自定义函数 - BASE {| class="wikitable" |+ !No !Method !Explain !Example |- |1 |isnull |(null, "", [], {}) = true | |- |2 |log, logerror, logwarn |log | |- |3 |dt |date, time | |- |4 |hash, md5 |sha, sha-256, md5, etc hash | |- |5 |base64, ubase64 |BASE64 | |- |6 |des, udes |DES crypt | |- |7 |str2map |String to MAP | |- |8 |list2map |List to MAP | |- |9 |reverse |reverse string, abc -> cba | |- |10 |trim |remove substring, left & right | |- |11 |at |CAS, (1, -1, n) | |} <small><small> package com.udf; /* ------------------------------------------------------------------------------ Name : Udf.base.BASE Purpose : string crypt, hash, datetime, isnull, trim, etc... Author : Adam Revisions: Ver Date Author Description --------- ---------- --------------- ------------------------------------ 1.0 2024/02/20 Adam 1.1 2024/02/27 Adam merge STR 1.2 2024/02/29 Adam remove commons-codec, replace util 1.21 2024/03/20 Adam Add log, reverse2 1.22 2024/03/28 Adam Add isnull, log4j --> slf4j 1.23 2024/04/09 Adam Add at(Atomicinteger) format: object : property: VERSION, json, UDEFLOGOFF, LOOPMAX method : at, isnull, log, dt, hash, md5, base64, des, ltrim, rtrim, trim, reverse <nowiki><!--Json--></nowiki> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency> <nowiki><!--log4j--></nowiki> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <nowiki><!--slf4j--></nowiki> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>2.0.9</version> </dependency> ------------------------------------------------------------------------------ */ import com.google.gson.Gson; import org.apache.log4j.PropertyConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.Cipher; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import java.math.BigInteger; import java.security.Key; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class BASE { // Property public static final String VERSION = "v1.23"; public static boolean UDEFLOGOFF = false; public static String CHARSET = "UTF-8"; public static int LOOPMAX = 1024; public static Gson json = new Gson(); // Private private static final AtomicInteger _UDEFAT = new AtomicInteger(0); static { PropertyConfigurator.configure("config/log4j.properties"); } // Method public static int at() { return _UDEFAT.get(); } public static int at(int flag) { switch(flag) { case 1: return _UDEFAT.getAndIncrement(); case -1: return _UDEFAT.getAndDecrement(); default : _UDEFAT.set(flag); return flag; } } // Default behavior for unknown types (e.g., return "null") public static String type(Object obj1) { if (obj1 == null) return "null"; if (obj1 instanceof String) return "String"; else if (obj1 instanceof List) return "List"; else if (obj1 instanceof Map) return "Map"; else if (obj1 instanceof Properties) return "Properties"; else return "null"; } // Default behavior for unknown types (e.g., return default1[false]) public static boolean isnull(Object obj1) { return isnull(obj1, false); } public static boolean isnull(Object obj1, boolean default1) { if (obj1 == null) return true; switch (type(obj1)) { case "String": return ((String)obj1).isEmpty(); case "List": return ((List)obj1).isEmpty(); case "Map": return ((Map)obj1).isEmpty(); case "Properties": return ((Properties)obj1).isEmpty(); default: return default1; } } public static void log(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.info(""); else logger.info(log1.toString()); } public static void logwarn(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.warn(""); else logger.warn(log1.toString()); } public static void logerror(Object log1) { if (UDEFLOGOFF) return; Logger logger = LoggerFactory.getLogger(Thread.currentThread().getStackTrace()[2].getClassName()); if (log1==null) logger.error(""); else logger.error(log1.toString()); } // get sysdate, "-:" --> 2024-02-22 00:00:00.000 public static String dt(String p, int len) { SimpleDateFormat df = new SimpleDateFormat(); if (isnull(p)) df.applyPattern("yyyyMMddHHmmssSSS"); else df.applyPattern("yyyy"+p.substring(0,1)+"MM"+p.substring(0,1)+"dd HH"+p.substring(1,2)+"mm"+p.substring(1,2)+"ss"+"."+"SSS"); Date date1 = new Date(); p = df.format(date1); len = (len<1 || len>p.length()) ? p.length() : len; return p.substring(0, len); } public static String dt(String p) { return dt(p, 0); } public static String dt(int len) { return dt(null, len); } // get sysdate, 20240222000000 public static String dt() { return dt(14); } public static String hash(String s1, String type) { if (isnull(s1)) return ""; if (isnull(type)) type = "md5"; String hash1 = ""; try { MessageDigest md = MessageDigest.getInstance(type); md.update(s1.getBytes()); hash1 = new BigInteger(1, md.digest()).toString(16); } catch (NoSuchAlgorithmException e) { logerror(e); } return hash1; } public static String md5(String s1) { return hash(s1, "md5"); } public static String base64(String s1) { if (isnull(s1)) return ""; try { String b64s1 = Base64.getEncoder().encodeToString(s1.getBytes(CHARSET)); return b64s1; } catch (Exception e){ logerror(e); return null; } } public static String ubase64(String s1) { if (isnull(s1)) return ""; try { byte[] b1 = Base64.getDecoder().decode(s1); return new String(b1, CHARSET); } catch (Exception e){ logerror(e); return ""; } } public static String des(String s1, String k1) { if (isnull(s1) || isnull(k1)) { logerror("The parameter is NULL."); return ""; } while (k1.length() < 8){ logerror("The parameter is short."); k1 += "0"; } String DES_ECB = "DES/ECB/PKCS5Padding"; try { // get key DESKeySpec dk1 = new DESKeySpec(k1.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dk1); Cipher cipher = Cipher.getInstance(DES_ECB); cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom()); byte[] b1 = cipher.doFinal(s1.getBytes(CHARSET)); // JDK1.7 及以下可以使用 BASE64Encoder return Base64.getEncoder().encodeToString(b1); } catch (Exception e) { logerror(e); return ""; } } public static String udes(String s1, String k1) { if (isnull(s1) || isnull(k1)) { logerror("The parameter is NULL."); return ""; } while (k1.length() < 8) { logerror("The parameter is short."); k1 += "0"; } try { String DES_ECB = "DES/ECB/PKCS5Padding"; // get key DESKeySpec dk1 = new DESKeySpec(k1.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); Key secretKey = keyFactory.generateSecret(dk1); Cipher cipher = Cipher.getInstance(DES_ECB); cipher.init(Cipher.DECRYPT_MODE, secretKey, new SecureRandom()); return new String(cipher.doFinal(Base64.getDecoder().decode(s1)), CHARSET); } catch (Exception e) { logerror(e); return ""; } } public static String des(String s1) { return des(s1, "DeiPd8ltuAE3"); } public static String udes(String s1) { return udes(s1, "DeiPd8ltuAE3"); } // String(a=1 b=c...) --> map({a=1, b=c, ...}) public static HashMap<String, String> str2map (String[] s1) { HashMap<String, String> para = new HashMap<>(); String s11, key, val; String[] s2; for (int i=0; i<s1.length; i++) { try { s11 = s1[i]; s2 = s11.split("="); key = s2[0]; val = ltrim(s11, key+"="); if (null == key || "".equals(key)) { key = "" + i; } else if (key.equals(s11)) { val = key; key = "" + i; } para.put(key, val); } catch (Exception e) { para.put("" + i, s1[i].substring(1)); } } return para; } // list1, list2 --> map(list1, list2) // if list2 < list1, set to null. public static HashMap<String, String> list2map(ArrayList<String> ky1, ArrayList<String> val1) { HashMap<String, String> map1 = new HashMap<>(); for (int i=0; i<ky1.size(); i++) { map1.put(ky1.get(i), i>=val1.size()?null:val1.get(i)); } return map1; } // reverse a string, like: abc --> cba public static String reverse(String s1) { if (isnull(s1)) return ""; char[] cStr1 = s1.toCharArray(); int len1 = cStr1.length; for (int i = 0; i < len1 / 2; i++) { char c1 = cStr1[i]; cStr1[i] = cStr1[len1 - 1 - i]; cStr1[len1 - 1 - i] = c1; } //String rStr1 = new String(cStr1); //return rStr1; return String.valueOf(cStr1); } public static String reverse2(String s1) { if (isnull(s1)) return ""; StringBuffer sf1 = new StringBuffer(128); sf1.append(s1); return sf1.reverse().toString(); } // left remove all substring public static String ltrim(String src, String rep) { return ltrim(src, rep, LOOPMAX); } // left remove i substrings public static String ltrim(String src, String rep, int i) { if (i <= 0 || isnull(src) || isnull(rep)) return src; for (int j=0; j<i; j++) { if (src.indexOf(rep) == 0) { src = src.substring(rep.length()); } else { break; } } return src; } // right remove all substring public static String rtrim(String src, String rep) { return rtrim(src, rep, LOOPMAX); } // right remove i substrings public static String rtrim(String src, String rep, int i) { if (i <= 0 || isnull(src) || isnull(rep)) return src; String rsrc, rrep; rsrc = reverse(src); rrep = reverse(rep); return reverse(ltrim(rsrc, rrep, i)); } // left & right remove all substring public static String trim(String src, String rep) { return rtrim(ltrim(src, rep), rep); } // left & right remove i substring public static String trim(String src, String rep, int i) { return rtrim(ltrim(src, rep, i), rep, i); } }</small></small> [[分类:Develop]] [[分类:Java]]
返回
Java 自定义方法 - BASE
。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
目录
文章分类
侧边栏
帮助
工具
链入页面
相关更改
特殊页面
页面信息