Commit b5ed11ec authored by zengtianlai3's avatar zengtianlai3

协议的data和end字段单独解析出来

parent 789b2919
...@@ -3,7 +3,7 @@ package iot.sixiang.license.consts; ...@@ -3,7 +3,7 @@ package iot.sixiang.license.consts;
public class Consts { public class Consts {
public static final int CMD_LICENSE = 16;//授权消息的cmd,十进制 public static final int CMD_LICENSE = 1;//授权消息的cmd,十进制
public static final int EXECUTOR_THREAD_NUM = 30; public static final int EXECUTOR_THREAD_NUM = 30;
public static final Integer DEVICE_STATE_ONLINE = 1;// 设备在线 public static final Integer DEVICE_STATE_ONLINE = 1;// 设备在线
public static final Integer DEVICE_STATE_OFFLINE = 0;// 设备离线 public static final Integer DEVICE_STATE_OFFLINE = 0;// 设备离线
......
...@@ -3,6 +3,7 @@ package iot.sixiang.license.device; ...@@ -3,6 +3,7 @@ package iot.sixiang.license.device;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import iot.sixiang.license.util.Util;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.List; import java.util.List;
...@@ -13,10 +14,14 @@ import java.util.List; ...@@ -13,10 +14,14 @@ import java.util.List;
*/ */
@Slf4j @Slf4j
public class DeviceDecoder extends ByteToMessageDecoder { public class DeviceDecoder extends ByteToMessageDecoder {
public final int BASE_LENGTH = 2 + 2 + 1 + 1; public final int BASE_LENGTH = 2 + 2 + 1+1+1;
@Override @Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) { protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
byte[] packet=new byte[buffer.readableBytes()];
buffer.readBytes(packet);
buffer.resetReaderIndex();
Util.DEBUG_HEX("SERVER -> IN",packet,packet.length);
try { try {
if (buffer.readableBytes() < BASE_LENGTH) { if (buffer.readableBytes() < BASE_LENGTH) {
return; return;
...@@ -54,10 +59,10 @@ public class DeviceDecoder extends ByteToMessageDecoder { ...@@ -54,10 +59,10 @@ public class DeviceDecoder extends ByteToMessageDecoder {
log.debug("stx:" + stx + ",len:" + len + ",cmd:" + cmd + ",ack:" + ack);//stx:21930,len:52,cmd:-112,ack:0 log.debug("stx:" + stx + ",len:" + len + ",cmd:" + cmd + ",ack:" + ack);//stx:21930,len:52,cmd:-112,ack:0
int real_len = len + 1; int real_len = len ;
int cmd_ack_len = 2; int cmd_ack_len = 2;
if (buffer.readableBytes() < real_len - cmd_ack_len) { if (buffer.readableBytes() < real_len - cmd_ack_len+1) {
buffer.resetReaderIndex(); buffer.resetReaderIndex();
return; return;
} }
...@@ -67,7 +72,9 @@ public class DeviceDecoder extends ByteToMessageDecoder { ...@@ -67,7 +72,9 @@ public class DeviceDecoder extends ByteToMessageDecoder {
byte[] content = new byte[real_len - cmd_ack_len]; byte[] content = new byte[real_len - cmd_ack_len];
buffer.readBytes(content); buffer.readBytes(content);
DeviceProtocol protocol = new DeviceProtocol(stx, len, cmd, ack, content); byte end = buffer.readByte();
DeviceProtocol protocol = new DeviceProtocol(stx, len, cmd, ack, content,end);
out.add(protocol); out.add(protocol);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -27,6 +27,7 @@ public class DeviceEncoder extends MessageToByteEncoder<DeviceProtocol> { ...@@ -27,6 +27,7 @@ public class DeviceEncoder extends MessageToByteEncoder<DeviceProtocol> {
} else { } else {
out.writeBytes(msg.getContent()); out.writeBytes(msg.getContent());
} }
out.writeByte(msg.getEnd());
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
......
...@@ -13,14 +13,16 @@ public class DeviceProtocol { ...@@ -13,14 +13,16 @@ public class DeviceProtocol {
private byte ack; private byte ack;
private byte[] content;// 数据 private byte[] content;// 数据
private byte end;
public DeviceProtocol(short stx, int len, byte cmd, byte ack, byte[] content) { public DeviceProtocol(short stx, int len, byte cmd, byte ack, byte[] content,byte end) {
super(); super();
this.stx = stx; this.stx = stx;
this.len = len; this.len = len;
this.cmd = cmd; this.cmd = cmd;
this.ack = ack; this.ack = ack;
this.content = content; this.content = content;
this.end = end;
} }
@Override @Override
......
...@@ -10,6 +10,7 @@ import iot.sixiang.license.event.DeviceClientInactiveEvent; ...@@ -10,6 +10,7 @@ import iot.sixiang.license.event.DeviceClientInactiveEvent;
import iot.sixiang.license.event.EventPublisher; import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientRequestEvent; import iot.sixiang.license.event.ForwardClientRequestEvent;
import iot.sixiang.license.model.SessionContext; import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.util.HexUtil;
import iot.sixiang.license.util.SpringUtil; import iot.sixiang.license.util.SpringUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -51,6 +52,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -51,6 +52,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
byte cmd = protocol.getCmd(); byte cmd = protocol.getCmd();
int cmdInt = cmd & 0xFF; int cmdInt = cmd & 0xFF;
log.info("real cmd:"+cmdInt); log.info("real cmd:"+cmdInt);
log.info("收到的消息:"+HexUtil.bytes2hex(protocol.getContent()));
boolean license = false; boolean license = false;
// cmdInt = Consts.CMD_LICENSE; // cmdInt = Consts.CMD_LICENSE;
...@@ -90,7 +92,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -90,7 +92,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
// TODO 以下为模拟的测试代码 // TODO 以下为模拟的测试代码
String appId = "123456"; /*String appId = "123456";
String appKey = "123456"; String appKey = "123456";
String token = "123456"; String token = "123456";
...@@ -114,7 +116,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -114,7 +116,7 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
CreateForwarClientEvent event = new CreateForwarClientEvent(); CreateForwarClientEvent event = new CreateForwarClientEvent();
event.setAppId(appId); event.setAppId(appId);
eventPublisher.publishEvent(event); eventPublisher.publishEvent(event);
} }*/
} }
@Override @Override
...@@ -178,6 +180,12 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> { ...@@ -178,6 +180,12 @@ public class DeviceServerHandler extends SimpleChannelInboundHandler<Object> {
eventPublisher.publishEvent(event); eventPublisher.publishEvent(event);
} }
// byte ack = 0x0;
// byte ack2 = 0x01;
// protocol.setAck(ack);
// channel.writeAndFlush(protocol);
return license; return license;
} }
......
package iot.sixiang.license.event;
import iot.sixiang.license.device.DeviceProtocol;
import lombok.Data;
@Data
public class DeviceClientLicenseEvent extends BaseEvent {
private String appId;
private DeviceProtocol protocol;
}
package iot.sixiang.license.event;
import iot.sixiang.license.device.DeviceManager;
import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.model.SessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class DeviceClientLicenseEventHandler {
@Autowired
DeviceManager deviceManager;
@Autowired
EventPublisher eventPublisher;
public DeviceClientLicenseEventHandler() {
}
// @Async("asyncExecutor")
@EventListener
public void handlerEvent(DeviceClientLicenseEvent event) {
String appId = event.getAppId();
DeviceProtocol protocol = event.getProtocol();
SessionContext session = deviceManager.getSessionContextByAppId(appId);
if (session == null) {
log.debug("device client license undo ...");
return;
} else {
session.getClientChannel().writeAndFlush(protocol);
log.debug("device client license success ...");
}
}
}
...@@ -35,13 +35,17 @@ public class ForwardMessageResponseEventHandler { ...@@ -35,13 +35,17 @@ public class ForwardMessageResponseEventHandler {
SessionContext forwardSessionContext = forwardManager.getSessionByChannelId(channelId); SessionContext forwardSessionContext = forwardManager.getSessionByChannelId(channelId);
String appId = forwardSessionContext.getAppId(); String appId = forwardSessionContext.getAppId();
log.info("forward client response..." + appId+",forward session:"+forwardSessionContext);
SessionContext deviceSessionContext = deviceManager.getSessionContextByAppId(appId); SessionContext deviceSessionContext = deviceManager.getSessionContextByAppId(appId);
if(deviceSessionContext!=null){
SocketChannel deviceClientChannel = deviceSessionContext.getClientChannel(); SocketChannel deviceClientChannel = deviceSessionContext.getClientChannel();
log.info("forward client response..." + appId+",forward session:"+deviceSessionContext); log.info("forward client response..." + appId+",forward session:"+deviceSessionContext);
deviceClientChannel.writeAndFlush(protocol); deviceClientChannel.writeAndFlush(protocol);
}else{
log.info("forward client response undo ..." + appId);
}
} }
......
...@@ -11,6 +11,7 @@ import iot.sixiang.license.event.EventPublisher; ...@@ -11,6 +11,7 @@ import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientInactiveEvent; import iot.sixiang.license.event.ForwardClientInactiveEvent;
import iot.sixiang.license.event.ForwardMessageResponseEvent; import iot.sixiang.license.event.ForwardMessageResponseEvent;
import iot.sixiang.license.model.SessionContext; import iot.sixiang.license.model.SessionContext;
import iot.sixiang.license.util.HexUtil;
import iot.sixiang.license.util.SpringUtil; import iot.sixiang.license.util.SpringUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -45,7 +46,8 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> { ...@@ -45,7 +46,8 @@ public class ForwardClientHandler extends SimpleChannelInboundHandler<Object> {
int serverPort = socketAddr.getPort(); int serverPort = socketAddr.getPort();
log.debug("channelRead0..."); log.debug("channelRead0...");
DeviceProtocol protocol = (DeviceProtocol) msg; DeviceProtocol protocol = (DeviceProtocol) msg;
log.info("准备发回给设备:" + protocol.toString()); log.info("桥接服务器响应1"+protocol.toString());
log.info("桥接服务器响应2:"+ HexUtil.bytes2hex(protocol.getContent()));
String channelId = channel.id().asLongText(); String channelId = channel.id().asLongText();
......
...@@ -3,7 +3,9 @@ package iot.sixiang.license.forward; ...@@ -3,7 +3,9 @@ package iot.sixiang.license.forward;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoop; import io.netty.channel.EventLoop;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.event.DeviceClientBeForcedOfflineEvent; import iot.sixiang.license.event.DeviceClientBeForcedOfflineEvent;
import iot.sixiang.license.event.DeviceClientLicenseEvent;
import iot.sixiang.license.event.EventPublisher; import iot.sixiang.license.event.EventPublisher;
import iot.sixiang.license.event.ForwardClientConnectEvent; import iot.sixiang.license.event.ForwardClientConnectEvent;
import iot.sixiang.license.net.BaseConnectionListener; import iot.sixiang.license.net.BaseConnectionListener;
...@@ -54,6 +56,30 @@ public class ForwardConnectionListener extends BaseConnectionListener { ...@@ -54,6 +56,30 @@ public class ForwardConnectionListener extends BaseConnectionListener {
log.debug("Successful connection to the server"); log.debug("Successful connection to the server");
log.info("forward client connect success ..." + appId); log.info("forward client connect success ..." + appId);
// byte ack2 = 0x01;
short stx = 21930;
byte ack = 0x0;
int len = 3;
byte cmd = 0x1;
byte[] content = new byte[1];
content[0] = 0x7e;
byte end = 0x1;
// stx:21930,len:76,cmd:1,ack:0
DeviceProtocol protocol = new DeviceProtocol( stx, len, cmd, ack, content,end);
DeviceClientLicenseEvent deviceClientLicenseEvent = new DeviceClientLicenseEvent();
deviceClientLicenseEvent.setAppId(appId);
deviceClientLicenseEvent.setProtocol(protocol);
eventPublisher.publishEvent(deviceClientLicenseEvent);
} }
} }
......
...@@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf; ...@@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import iot.sixiang.license.device.DeviceProtocol; import iot.sixiang.license.device.DeviceProtocol;
import iot.sixiang.license.util.Util;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.List; import java.util.List;
...@@ -17,6 +18,11 @@ public class ForwardDecoder extends ByteToMessageDecoder { ...@@ -17,6 +18,11 @@ public class ForwardDecoder extends ByteToMessageDecoder {
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) { protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) {
// 可读长度必须大于等于基本长度 // 可读长度必须大于等于基本长度
try { try {
byte[] packet=new byte[buffer.readableBytes()];
buffer.readBytes(packet);
buffer.resetReaderIndex();
Util.DEBUG_HEX("SERVER -> IN",packet,packet.length);
if (buffer.readableBytes() < BASE_LENGTH) { if (buffer.readableBytes() < BASE_LENGTH) {
return; return;
} }
...@@ -33,10 +39,10 @@ public class ForwardDecoder extends ByteToMessageDecoder { ...@@ -33,10 +39,10 @@ public class ForwardDecoder extends ByteToMessageDecoder {
log.debug("stx:" + stx + ",len:" + len); log.debug("stx:" + stx + ",len:" + len);
int real_len = len+1;//注意,透传前已经去掉了END一个字符 int real_len = len;//注意,透传前已经去掉了END一个字符
int cmd_ack_len = 2; int cmd_ack_len = 2;
if (buffer.readableBytes() < real_len - cmd_ack_len) { if (buffer.readableBytes() < real_len - cmd_ack_len+1) {
buffer.resetReaderIndex(); buffer.resetReaderIndex();
return; return;
} }
...@@ -47,9 +53,11 @@ public class ForwardDecoder extends ByteToMessageDecoder { ...@@ -47,9 +53,11 @@ public class ForwardDecoder extends ByteToMessageDecoder {
byte[] content = new byte[real_len - cmd_ack_len]; byte[] content = new byte[real_len - cmd_ack_len];
buffer.readBytes(content); buffer.readBytes(content);
byte end = buffer.readByte();
log.debug("ForwardDecoder.msg.getLen()...." + real_len); log.debug("ForwardDecoder.msg.getLen()...." + real_len);
DeviceProtocol protocol = new DeviceProtocol(stx, real_len, cmd, ack, content); DeviceProtocol protocol = new DeviceProtocol(stx, real_len, cmd, ack, content,end);
out.add(protocol); out.add(protocol);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -29,6 +29,7 @@ public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> { ...@@ -29,6 +29,7 @@ public class ForwardEncoder extends MessageToByteEncoder<DeviceProtocol> {
} else { } else {
out.writeBytes(msg.getContent()); out.writeBytes(msg.getContent());
} }
out.writeByte(msg.getEnd());
} catch (Exception e) { } catch (Exception e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
......
package iot.sixiang.license.util;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
@Slf4j
public class Util {
public static boolean Debug=false;
public static void logInit(String path){
}
public static byte asciiToByte(byte ascii)
{
byte ret;
if(ascii>='A'){
ret = (byte)((ascii-'A')+0X0A);
ret&=0x0f;
return ret;
}else{
ret = (byte)(ascii-(byte)0x30);
ret = (byte)(ret&0x0f);
return ret;
}
}
public static byte byteToAscii(byte data){
byte ret=0x0f;
if(data>=0X0A){
ret=(byte)((data-0X0A)+'A');
}else{
ret=(byte)(data+'0');
}
return ret;
}
public static int AsciiToHexByte(byte[] dst,byte[] src){
if(src.length==0){
return 0;
}
if((src.length%2)!=0){
return 0;
}
for(int i=0;i<src.length;){
dst[i/2]=(byte)(asciiToByte(src[i])<<4);
dst[i/2]+=asciiToByte(src[i+1]);
i+=2;
}
return src.length/2;
}
public static int AsciiToHexByte(byte[] dst,int dstOffset,byte[] src,int srcOffset,int len){
if(len==0){
return 0;
}
if((len%2)!=0){
return 0;
}
for(int i=0;i<len;){
dst[(i/2)+dstOffset]=(byte)(asciiToByte(src[i+srcOffset])<<4);
dst[(i/2)+dstOffset]+=asciiToByte(src[i+1+srcOffset]);
i+=2;
}
return len/2;
}
public static int HexToAsciiByte(byte[] dst,byte[] src){
for(int i=0;i<src.length;i++){
dst[i*2]=byteToAscii((byte)((src[i]>>4)&0x0f));
dst[(i*2)+1]=byteToAscii((byte)(src[i]&0x0f));
}
return src.length*2;
}
public static int IndexOfByte(byte[] src,int offer,int len,byte data){
for(int i=offer;i<len;i++){
if(data==src[i]){
return i;
}
}
return len;
}
public static void DEBUG_HEX(String tag,byte[] data,int datalen){
String hexStr="";
int i=0;
for( i=0;i<datalen;i++){
if((i%16)==0){
log.info(hexStr);
hexStr="";
}
if(Integer.toHexString(data[i]&0xff).length()==1){
hexStr+="0";
}
hexStr+=Integer.toHexString(data[i]&0xff).toUpperCase()+" ";
if((i%16)==15){
for(int j=(i-15);j<=i;j++){
if(data[j]>0x20&&data[j]<=0x7e){
hexStr+=String.valueOf((char)(data[j]&0xff));
}else{
hexStr+='.';
}
}
}
}
if((i%16)!=0){
for(int j=0;j<16;j++){
if(j<(16-(i%16))){
hexStr+="-- ";
}
}
for(int j=(i-(i%16));j<i;j++){
if(data[j]>0x20&&data[j]<=0x7e){
hexStr+=String.valueOf((char)(data[j]&0xff));
}else{
hexStr+='.';
}
}
}
log.info(hexStr);
}
public static void INFO(String tag,String content){
if(!Debug){
return ;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("I-["+df.format(new Date())+"]["+tag+"]:"+content);
}
public static void DEBUG(String tag,String content){
if(!Debug){
return ;
}
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("D-["+df.format(new Date())+"]["+tag+"]:"+content);
}
public static void ERROR(String tag,Object content){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("E-["+df.format(new Date())+"]["+tag+"]:"+content);
if (content instanceof Exception) {
StackTraceElement stack[] = ((Exception)content).getStackTrace();
for (int i = 0; i < stack.length; i++)
{
StackTraceElement s = stack[i];
System.out.format("Class:%d\t%s\n", i, s.getClass());//������
System.out.format("ClassName:%d\t%s\n", i, s.getClassName());//����
System.out.format("MethodName:%d\t%s\n", i, s.getMethodName());//������
System.out.format("FileName:%d\t%s\n", i, s.getFileName());//�ļ���
System.out.format("LineNumber:%d\t%s\n", i, s.getLineNumber());//����
System.out.println("-------------------------------------------");//����
}
}
}
public static String HexToStr(byte[] data,int offset,int length,boolean space){
String hexStr="";
for(int i=offset;i<(length+offset);i++){
if(Integer.toHexString(data[i]&0xff).length()==1){
hexStr+="0";
}
hexStr+=Integer.toHexString(data[i]&0xff).toUpperCase();
if(space){
hexStr+=" ";
}
}
return hexStr;
}
public static List<String> getLocalIPList() {
List<String> ipList = new ArrayList<String>();
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
NetworkInterface networkInterface;
Enumeration<InetAddress> inetAddresses;
InetAddress inetAddress;
String ip;
while (networkInterfaces.hasMoreElements()) {
networkInterface = networkInterfaces.nextElement();
inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
inetAddress = inetAddresses.nextElement();
if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
ip = inetAddress.getHostAddress();
ipList.add(ip);
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipList;
}
public static int CRC16(byte[] p,int offer,int len)
{
//Ԥ�� 1 �� 16 λ�ļĴ���Ϊʮ������FFFF, �ƴ˼Ĵ���Ϊ CRC�Ĵ�����
int crc = 0xFFFF;
int i, j;
for (i = 0; i < len; i++) {
//�ѵ�һ�� 8 λ���������� �� 16 λ�� CRC�Ĵ����ĵ� 8 λ�����, �ѽ������ CRC�Ĵ���
crc = ((crc & 0xFF00) | (crc & 0x00FF) ^ (p[i+offer] & 0xFF));
for (j = 0; j < 8; j++) {
//�� CRC �Ĵ�������������һλ( ����λ)�� 0 ����λ, ��������ƺ���Ƴ�λ
if ((crc & 0x0001) > 0) {
//����Ƴ�λΪ 1, CRC�Ĵ��������ʽA001�������
crc = crc >> 1;
crc = crc ^ 0xA001;
} else
//����Ƴ�λΪ 0,�ٴ�����һλ
crc = crc >> 1;
}
}
return crc;
}
public static String UnicodeToUTF8(String content){
try {
return new String(content.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String splitLineString(String content,int lineLength) {
String tmp="";
int offset=0;
int length=content.length();
while(length>0) {
if(length<lineLength) {
tmp+=content.substring(offset)+"\n";
length=0;
}else {
tmp+=content.substring(offset,lineLength+offset)+"\n";
offset+=lineLength;
length-=lineLength;
}
}
return tmp;
}
public static byte[] getSHA256(byte[] content) {
MessageDigest messageDigest;
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(content);
return messageDigest.digest();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment