📄 tools.java
字号:
package com.rainbow.util.tools;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.sql.Types;
import java.util.Iterator;
import java.util.Map;
import org.apache.log4j.Logger;
/**
* @author liu_hongshan
*/
public abstract class Tools {
private static final Logger log = Logger.getLogger(Tools.class);
public static final int CMPP_MSG_FORMAT_ASCII = 0;
public static final int CMPP_MSG_FORMAT_SMSWRITE = 3;
public static final int CMPP_MSG_FORMAT_BINARY = 4;
public static final int CMPP_MSG_FORMAT_UCS2 = 8;
public static final int CMPP_MSG_FORMAT_GBK = 15;
public static final String UCS2_CODE_NAME = "UnicodeBigUnmarked";
public static final String GBK_CODE_NAME = "GBK";
/**
* 未拷贝,返回-1 <code>UNCOPY</code>
*/
public static final int UNCOPY = -1;
/**
* 从byte[]转换到String
*
* @param data
* @param format
* @return 发生异常时返回null
*/
public static String getStrinbByFomat(final byte[] data, final int format) {
return getStrinbByFomat(data, format, data.length);
}
public static String getStrinbByFomat(final byte[] data, final int format,
final int len) {
String stringData = null;
if (format == CMPP_MSG_FORMAT_UCS2) {
try {
stringData = new String(data, 0, len, UCS2_CODE_NAME);
} catch (UnsupportedEncodingException e) {
log.error("系统不支持UnicodeLittleUnmarked编码", e);
}
} else {
try {
stringData = new String(data, 0, len, GBK_CODE_NAME);
} catch (UnsupportedEncodingException e) {
log.error("系统不支持GBK编码", e);
}
}
return stringData;
}
/**
* 从String转换到byte[]
*
* @param data
* @param format
* @return 发生异常时返回null
*/
public static byte[] getBytesFromStringByFormat(final String data,
final int format) {
if (format == CMPP_MSG_FORMAT_UCS2) {
try {
return data.getBytes(UCS2_CODE_NAME);
} catch (UnsupportedEncodingException e) {
log.error("系统不支持UnicodeLittleUnmarked编码", e);
}
} else {
try {
return data.getBytes(GBK_CODE_NAME);
} catch (UnsupportedEncodingException e) {
log.error("系统不支持GBK编码", e);
}
}
return null;
}
/**
*
*/
public Tools() {
super();
}
public static int DeepDuplicate(String src, byte[] dest) {
return DeepDuplicate(src.getBytes(), dest);
}
public static int DeepDuplicateWithFormat(String src, byte[] dest,
int format) {
return DeepDuplicate(getBytesFromStringByFormat(src, format), dest);
}
public static int DeepDuplicate(byte[] src, byte[] dest) {
if (null == dest || null == src || 0 == dest.length || 0 == src.length) {
return UNCOPY;
}
int length = src.length > dest.length - 1 ? dest.length - 1
: src.length;
System.arraycopy(src, 0, dest, 0, length);
return length;
}
//判断字符串是否为手机号码
public static boolean isMobile(String mobile) {
if (null == mobile) {
return false;
}
//不是11位的,不是手机号码
if (mobile.length() != 11) {
return false;
}
//不以“13”开头的,不是手机号码
if (!mobile.startsWith("13")) {
return false;
}
byte[] data = mobile.getBytes();
//3到11位不是数字的,不是手机号码
for (int i = 2; i < 10; ++i) {
if ('0' > data[i] || '9' < data[i]) {
return false;
}
}
return true;
}
//判断字符串是否为移动手机号码
public static boolean isYDMobile(String mobile) {
if (!isMobile(mobile)) {
return false;
}
byte[] data = mobile.getBytes();
if (data[2] >= '5' && data[2] <= '9') {
return true;
} else {
return false;
}
}
// 判断字符串是否为联通手机号码
public static boolean isLTMobile(String mobile) {
if (!isMobile(mobile)) {
return false;
}
byte[] data = mobile.getBytes();
if (data[2] >= '0' && data[2] <= '4') {
return true;
} else {
return false;
}
}
/**
* @return
*/
public static String byteToStringWithTrim(byte[] bytes) {
return new String(bytes).trim();
}
public static void getSqlTypes() {
Class clazz = Types.class;
Field[] fields = clazz.getFields();
for (int i = 0; i < fields.length; i++) {
try {
System.out.println(fields[i].getName() + "="
+ fields[i].getInt(clazz));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
getSqlTypes();
}
public static void printMap(Map r) {
Iterator i = r.entrySet().iterator();
while (i.hasNext()) {
System.out.println((String) i.next().toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -