📄 buffmemoutil.java
字号:
package com.ekun.sms.common;
import java.io.*;
import java.util.regex.Pattern;
public class BuffMemoUtil
{
public static int ByteToInt(byte mybyte)
{
return mybyte;
}
public static byte IntToByte(int i)
{
return (byte) i;
}
public static byte[] IntToBytes(int i)
{
byte mybytes[] = new byte[2];
mybytes[1] = (byte) (0xff & i);
mybytes[0] = (byte) ( (0xff00 & i) >> 8);
return mybytes;
}
public static void IntToBytes(int i, byte[] mybytes)
{
mybytes[1] = (byte) (0xff & i);
mybytes[0] = (byte) ( (0xff00 & i) >> 8);
}
public static byte[] IntToBytes4(int i)
{
byte mybytes[] = new byte[4];
mybytes[3] = (byte) (0xff & i);
mybytes[2] = (byte) ( (0xff00 & i) >> 8);
mybytes[1] = (byte) ( (0xff0000 & i) >> 16);
mybytes[0] = (byte) ( (0xff000000 & i) >> 24);
return mybytes;
}
public static void IntToBytes4(int i, byte[] mybytes)
{
mybytes[3] = (byte) (0xff & i);
mybytes[2] = (byte) ( (0xff00 & i) >> 8);
mybytes[1] = (byte) ( (0xff0000 & i) >> 16);
mybytes[0] = (byte) (int) ( (0xff000000 & i) >> 24);
}
public static byte[] LongToBytes8(long i)
{
byte mybytes[] = new byte[8];
mybytes[7] = (byte) (0xff & i);
mybytes[6] = (byte) ( (0xff00 & i) >> 8);
mybytes[5] = (byte) ( (0xff0000 & i) >> 16);
mybytes[4] = (byte) ( (0xff000000 & i) >> 24);
int high = (int) (i >> 32);
mybytes[3] = (byte) (0xff & high);
mybytes[2] = (byte) ( (0xff00 & high) >> 8);
mybytes[1] = (byte) ( (0xff0000 & high) >> 16);
mybytes[0] = (byte) ( (0xff000000 & high) >> 24);
return mybytes;
}
public static long Bytes8ToLong(byte b[])
{
return (long) b[7] & (long) 255
| ( (long) b[6] & (long) 255) << 8
| ( (long) b[5] & (long) 255) << 16
| ( (long) b[4] & (long) 255) << 24
| ( (long) b[3] & (long) 255) << 32
| ( (long) b[2] & (long) 255) << 40
| ( (long) b[1] & (long) 255) << 48
| (long) b[0] << 56;
}
public static long Bytes4ToLong(byte[] abyte0)
{
return (255L & (long) abyte0[0]) << 24 | (255L & (long) abyte0[1]) << 16 |
(255L & (long) abyte0[2]) << 8 | 255L & (long) abyte0[3];
}
public static int Bytes4ToInt(byte[] mybytes)
{
int tmp = (0xff & mybytes[0]) << 24 | (0xff & mybytes[1]) << 16 |
(0xff & mybytes[2]) << 8 | 0xff & mybytes[3];
return tmp;
}
public static int Bytes2ToInt(byte[] abyte0)
{
return ( (0xff & abyte0[0]) << 8) + abyte0[1];
}
public static byte[] LongToBytes4(long l)
{
byte abyte0[] = new byte[4];
abyte0[3] = (byte) (int) (255L & l);
abyte0[2] = (byte) (int) ( (65280L & l) >> 8);
abyte0[1] = (byte) (int) ( (0xff0000L & l) >> 16);
abyte0[0] = (byte) (int) ( (0xffffffffff000000L & l) >> 24);
return abyte0;
}
public static void LongToBytes4(long l, byte[] abyte0)
{
abyte0[3] = (byte) (int) (255L & l);
abyte0[2] = (byte) (int) ( (65280L & l) >> 8);
abyte0[1] = (byte) (int) ( (0xff0000L & l) >> 16);
abyte0[0] = (byte) (int) ( (0xffffffffff000000L & l) >> 24);
}
public static void BytesCopy(byte[] source, byte[] dest, int sourcebegin,
int sourceend, int destbegin)
{
int j = 0;
int i = sourcebegin;
while (i <= sourceend)
{
dest[destbegin + j] = source[i];
j++;
i++;
}
}
public static void printBytes(byte bytes[], int len)
{
int i;
int j = 0;
for (i = 0; i < len; i++)
{
if (i % 4 == 0)
{
System.out.println('\n');
j++;
System.out.print("Line Number " + j + " :");
}
System.out.print(' ');
System.out.print(' ');
System.out.print('[');
System.out.print(Long.toString( (long) bytes[i] & 0xff, 16));
System.out.print('|');
System.out.print( (char) bytes[i]);
System.out.print(']');
}
System.out.print(
"\n-------------------------------------------------------------------");
}
public static void printBytes2(byte[] bytes, int len)
{
int i;
for (i = 0; i < len; i++)
{
System.out.print(Long.toString( (long) bytes[i] & 0xff, 16));
System.out.print(" ");
//System.out.print((char) bytes[i]);
}
System.out.print("\n");
}
/**
* 检测末尾是否半个汉字
* @param bytes
* @param start
* @param end
* @return 没有:true ,有:false
*/
public static boolean notHalfChar(byte[] bytes, int start, int end)
{
int len = bytes.length - 1;
//如果已经是整个字串末位,或end位置非汉字编码
if ( (len <= end) || ( (bytes[end] & 0x80) == 0))
{
return true;
}
for (; start <= end; )
{
if ( (bytes[start] & 0x80) != 0)
{
start += 2;
}
else
{
start++;
}
}
if ( (start - end) == 2)
{
return false;
}
return true;
}
/**
* 读取一定数量的数据!
* @param byteArray byte[]
* @param sin InputStream
* @return int,
* 1、成功读取
* 2、读到流尾不再有数据可读
* 3、超时。
* 以上三种情况返回实际读取的字节数;
* 若IO异常,则触发IOException异常;
*/
public static void nRead(byte[] byteArray, InputStream sin) throws IOException
{
int bytesRead = 0;
int reads = 0;
int nCount = byteArray.length;
//try
{
while (bytesRead < nCount)
{
reads = sin.read(byteArray, bytesRead, nCount - bytesRead);
if (reads == -1)
{
throw new IOException("socket read -1 byte!");
}
bytesRead += reads;
}
}
/*
catch (java.net.SocketTimeoutException ste)
{
return bytesRead;
}
*/
}
//中移手机号码匹配正则表达式,
private static Pattern patternMSISDNCMCC = Pattern.compile("^\\+?(86)?((13[456789])|(159))\\d{8}$");
//中国联通的手机号码正则表达式
private static Pattern patternMSISDNLT = Pattern.compile("^\\+?(86)?13[0123]\\d{8}$");
public static String addMobilePrefix86(String mobileNum)
{
if(mobileNum == null) return null;
String sTmp = mobileNum.trim();
//判断是否中移动或者中联通号码!若不是,直接返回!
if (!(patternMSISDNCMCC.matcher(sTmp).matches() || patternMSISDNLT.matcher(sTmp).matches()))
return sTmp;
if(sTmp.startsWith("+"))
sTmp = sTmp.substring(1);
if (!sTmp.startsWith("86"))
sTmp = "86" + sTmp;
return sTmp;
}
public static String removeMobilePrefix86(String mobileNum)
{
if (mobileNum == null)
return null;
else
{
String num = mobileNum.trim();
//判断是否中移动或者中联通号码!若不是,直接返回!
if (!(patternMSISDNCMCC.matcher(num).matches() || patternMSISDNLT.matcher(num).matches()))
return num;
if (num.startsWith("+"))
num = num.substring(1);
if (num.startsWith("86"))
num = num.substring(2);
return num;
}
}
/**
* 格式化字符串,左补0
* @param n int
* @param nZeroLen int
* @return String
*/
private static String leftAddZero(int n, int nZeroLen)
{
StringBuffer sb = new StringBuffer();
int nZeros = nZeroLen - String.valueOf(n).length();
for (int i = 0; i < nZeros; i++)
{
sb.append('0');
}
sb.append(n);
return sb.toString();
}
/**
* 获得CMPP协议中的Msg_Id
* @param usMsgId byte[]
* @return String
*/
public static String getMsgId(byte[] usMsgId)
{
long nMsgId = BuffMemoUtil.Bytes8ToLong(usMsgId);
//序列号:bit16~bit1,顺序增加,步长为1,循环使用。
int nSeq = (int) (nMsgId & ( (1 << 16) - 1));
//短信网关代码:bit38~bit17,把短信网关的代码转换为整数填写到该字段中。
int nIsmgCode = (int) ( (nMsgId >> 16) & ( (1 << 22) - 1));
/*
时间(格式为MMDDHHMMSS,即月日时分秒):bit64~bit39,其中
bit64~bit61:月份的二进制表示;
bit60~bit56:日的二进制表示;
bit55~bit51:小时的二进制表示;
bit50~bit45:分的二进制表示;
bit44~bit39:秒的二进制表示;
*/
byte ss = (byte) (int) ( (long) (nMsgId >> 39) & ( (long) (1 << 6) - 1));
byte mm = (byte) (int) ( (long) (nMsgId >> 44) & ( (long) (1 << 6) - 1));
byte HH = (byte) (int) ( (long) (nMsgId >> 50) & ( (long) (1 << 5) - 1));
byte dd = (byte) (int) ( (long) (nMsgId >> 55) & ( (long) (1 << 5) - 1));
byte MM = (byte) (int) ( (long) (nMsgId >> 60) & ( (long) (1 << 4) - 1));
StringBuffer sb = new StringBuffer();
sb.append(leftAddZero(MM, 2)).append(leftAddZero(dd, 2)).
append(leftAddZero(HH, 2)).append(leftAddZero(mm, 2)).
append(leftAddZero(ss, 2)).append('-').append(nIsmgCode)
.append('-').append(nSeq);
return sb.toString();
}
public static void main(String[] args)
{
System.out.println(addMobilePrefix86("13510328056"));
System.out.println(addMobilePrefix86("13011094369"));
System.out.println(addMobilePrefix86("8613510328056"));
System.out.println(removeMobilePrefix86("+8615911110070"));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -