📄 util.java
字号:
return true;
}
/**
* 根据某种编码方式得到字符串的字节数组形式
* @param s 字符串
* @param encoding 编码方式
* @return 特定编码方式的字节数组,如果encoding不支持,返回一个缺省编码的字节数组
*/
public static byte[] getBytes(String s, String encoding) {
try {
return s.getBytes(encoding);
} catch (UnsupportedEncodingException e) {
return s.getBytes();
}
}
/**
* 根据缺省编码得到字符串的字节数组形式
*
* @param s
* @return
*/
public static byte[] getBytes(String s) {
return getBytes(s, QQ.QQ_CHARSET_DEFAULT);
}
/**
* 对原始字符串进行编码转换,如果失败,返回原始的字符串
* @param s 原始字符串
* @param srcEncoding 源编码方式
* @param destEncoding 目标编码方式
* @return 转换编码后的字符串,失败返回原始字符串
*/
public static String getString(String s, String srcEncoding, String destEncoding) {
try {
return new String(s.getBytes(srcEncoding), destEncoding);
} catch (UnsupportedEncodingException e) {
return s;
}
}
/**
* 从buf的当前位置解析出一个字符串,直到碰到一个分隔符为止,或者到了buf的结尾
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于分隔符之后
* </p>
* <p>
* 返回的字符串将使用QQ缺省编码,一般来说就是GBK编码
* </p>
*
* @param buf
* ByteBuffer
* @param delimit
* 分隔符
* @return 字符串
*/
public static String getString(ByteBuffer buf, byte delimit) {
baos.reset();
while(buf.hasRemaining()) {
byte b = buf.get();
if(b == delimit)
return getString(baos.toByteArray());
else
baos.write(b);
}
return getString(baos.toByteArray());
}
/**
* 从buf的当前位置解析出一个字符串,直到碰到了buf的结尾
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于buf最后之后
* </p>
* <p>
* 返回的字符串将使用QQ缺省编码,一般来说就是GBK编码
* </p>
*
* @param buf
* ByteBuffer
* @return 字符串
*/
public static String getString(ByteBuffer buf) {
baos.reset();
while(buf.hasRemaining()) {
baos.write(buf.get());
}
return getString(baos.toByteArray());
}
/**
* 从buf的当前位置解析出一个字符串,直到碰到了buf的结尾或者读取了len个byte之后停止
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于len字节之后或者最后之后
* </p>
* <p>
* 返回的字符串将使用QQ缺省编码,一般来说就是GBK编码
* </p>
*
* @param buf
* ByteBuffer
* @return 字符串
*/
public static String getString(ByteBuffer buf, int len) {
baos.reset();
while(buf.hasRemaining() && len-- > 0) {
baos.write(buf.get());
}
return getString(baos.toByteArray());
}
/**
* 从buf的当前位置解析出一个字符串,直到碰到了delimit或者读取了maxLen个byte或者
* 碰到结尾之后停止
* <p>
* 此方法不负责调整buf位置,调用之前务必使buf当前位置处于字符串开头。在读取完成
* 后,buf当前位置将位于maxLen之后
* </p>
* <p>
* 返回的字符串将使用QQ缺省编码,一般来说就是GBK编码
* </p>
*
* @param buf
* ByteBuffer
* @param delimit
* delimit
* @param maxLen
* max len to read
* @return String
*/
public static String getString(ByteBuffer buf, byte delimit, int maxLen) {
baos.reset();
while(buf.hasRemaining() && maxLen-- > 0) {
byte b = buf.get();
if(b == delimit)
break;
else
baos.write(b);
}
while(buf.hasRemaining() && maxLen-- > 0)
buf.get();
return getString(baos.toByteArray());
}
/**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
public static String getString(byte[] b, String encoding) {
try {
return new String(b, encoding);
} catch (UnsupportedEncodingException e) {
return new String(b);
}
}
/**
* 根据缺省编码将字节数组转换成字符串
*
* @param b
* 字节数组
* @return
* 字符串
*/
public static String getString(byte[] b) {
return getString(b, QQ.QQ_CHARSET_DEFAULT);
}
/**
* 根据某种编码方式将字节数组转换成字符串
* @param b 字节数组
* @param offset 要转换的起始位置
* @param len 要转换的长度
* @param encoding 编码方式
* @return 如果encoding不支持,返回一个缺省编码的字符串
*/
public static String getString(byte[] b, int offset, int len, String encoding) {
try {
return new String(b, offset, len, encoding);
} catch (UnsupportedEncodingException e) {
return new String(b, offset, len);
}
}
/**
* 根据缺省编码方式将字节数组转换成字符串
* @param b 字节数组
* @param offset 要转换的起始位置
* @param len 要转换的长度
* @return
*/
public static String getString(byte[] b, int offset, int len) {
return getString(b, offset, len, QQ.QQ_CHARSET_DEFAULT);
}
/**
* 把字符串转换成int
* @param s 字符串
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static int getInt(String s, int faultValue) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException e) {
return faultValue;
}
}
/**
* 把字符串转换成long
* @param s 字符串
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static long getLong(String s, int radix, long faultValue) {
try {
return Long.parseLong(s, radix);
} catch (NumberFormatException e) {
return faultValue;
}
}
/**
* 把字符串转换成int
* @param s 字符串
* @param radix
* 基数
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static int getInt(String s, int radix, int faultValue) {
try {
return Integer.parseInt(s, radix);
} catch (NumberFormatException e) {
return faultValue;
}
}
/**
* 检查字符串是否是整数格式
*
* @param s
* 字符串
* @return
* true表示可以解析成整数
*/
public static boolean isInt(String s) {
try {
Integer.parseInt(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}
/**
* 把字符串转换成char类型的无符号数
* @param s 字符串
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static char getChar(String s, int faultValue) {
return (char)(getInt(s, faultValue) & 0xFFFF);
}
/**
* 把字符串转换成byte
* @param s 字符串
* @param faultValue 如果转换失败,返回这个值
* @return 如果转换失败,返回faultValue,成功返回转换后的值
*/
public static byte getByte(String s, int faultValue) {
return (byte)(getInt(s, faultValue) & 0xFF);
}
/**
* @param ip ip的字节数组形式
* @return 字符串形式的ip
*/
public static String getIpStringFromBytes(byte[] ip) {
sb.delete(0, sb.length());
sb.append(ip[0] & 0xFF);
sb.append('.');
sb.append(ip[1] & 0xFF);
sb.append('.');
sb.append(ip[2] & 0xFF);
sb.append('.');
sb.append(ip[3] & 0xFF);
return sb.toString();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -