import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.*;
/**
-
Created by adnin on 2019/3/29.
*/
public class ConfigByteDecoder extends ByteToMessageDecoder {
//todo fliter byte by specific title and config the filter
//decode byte
//Byte[]{y.x}
private final TreeMap<Byte, Byte[][]> coorniteMap = new TreeMap<>();//short by x
private final HashMap<Byte, TreeMap<Byte, LinkedList>> positionInitMap = new HashMap<>(20);
//HashMap<Byte, TreeMap<Byte x,Byte y >>
int Pointer;
private final int TiTleNUm;
private final int[] TiTleLength;
private final byte[][] titleGroup;
private final int[] readLength;private Byte getGroupElement(byte y, byte x) {
if (y <= titleGroup.length) {
if (x <= titleGroup[y].length) {return titleGroup[y][x]; } else { return null; } } else return null;
}
int titleNum;
HashMap<byte[], Integer> LengthMap = new HashMap<>();public ConfigByteDecoder(int[] readLength, byte[]… titleGroup) {
this.readLength = readLength;this.titleGroup = titleGroup; matchingWindow = new byte[titleGroup.length]; for (byte re : matchingWindow) { re = -1; }//initMatching TiTleNUm = titleGroup.length; TiTleLength = new int[TiTleNUm]; //get Title length of each title for (byte y = 0; y < titleGroup.length; y++) { byte[] title = titleGroup[y]; TiTleLength[y] = title.length; for (byte x = 0; x < title.length; x++) { byte element = title[x]; if (positionInitMap.containsKey(element)) { TreeMap<Byte, LinkedList<Byte>> tempmap = positionInitMap.get(element); if (tempmap.containsValue(x)) { tempmap.get(x).add(y); } else { LinkedList<Byte> yList = new LinkedList<Byte>(); yList.add(y); tempmap.put(x, yList);//(x.y) } } else { positionInitMap.put(element, new TreeMap<Byte, LinkedList<Byte>>()); LinkedList<Byte> yList = new LinkedList<Byte>(); yList.add(y); positionInitMap.get(element).put(x, yList); } } positionInitMap.forEach((aTitleByte, byteByteTreeMap) -> { ArrayList<Byte[]> tempCollection = new ArrayList<Byte[]>(); byteByteTreeMap.forEach((XByte, Ybytes) -> { Ybytes.forEach(Ybyte -> tempCollection.add(new Byte[]{Ybyte, XByte})); }); // byteByteTreeMap.forEach((xByte, yByte) -> tempCollection.add(new Byte[]{yByte, xByte})); Byte[][] allCoordinatorofAByte = (Byte[][]) tempCollection.toArray();//fixme 数组转型? coorniteMap.put(aTitleByte, allCoordinatorofAByte); }); //fixme 使用TreeMap可行性 }
}
LinkedList window = new LinkedList<>();
//important
final byte[] matchingWindow; //recoder evert Title matchingWindow Positon 0-N
//for (byte re:matchingWindow){re=-1;}//initMatching
//important
int waitingData = 0;//todo@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception {
LinkedList window = this.window;
byte alltitle[][] = titleGroup;
ByteBuf waitingPackge;while (in.isReadable()) { //!!!!!!!!!read one byte r1 = in.readByte();//read byte // coorniteMap.containsValue(r1); Byte[][] coordinate = coorniteMap.get(r1);//get all coordinate{y.x} sortby x//todo 性能瓶茎 System.out.println(coordinate); if (coordinate != null) { for (Byte[] YX : coordinate) { byte next = YX[1].byteValue(); if (next== ++matchingWindow[YX[0]] ) {
//
if (matchingWindow[YX[0]]++ == (TiTleLength[YX[0]] - 1)) {
int readNUM = readLength[YX[0]];
if (in.readableBytes() >= readNUM) {
ByteBuf packge = in.readBytes(readNUM);
waitingData = 0;
} else {
waitingData = readNUM - in.readableBytes();
}
//todo 匹配完成
//todo 读取数据包 等待字节注入
//todo 重置位置 window
for (byte re : matchingWindow) {
re = -1;
}
}
;
} else {
//todo 根据Title回滚 位置
byte[] TiTle = titleGroup[YX[0]];
byte mark = matchingWindow[YX[0]];
while (matchingWindow[YX[0]] >= 0) {
if (r1 == TiTle[matchingWindow[YX[0]]]) {
boolean reMached = false;
for (int i = 0; matchingWindow[YX[0]] >= i; i++) {
if (TiTle[mark - i] == TiTle[matchingWindow[YX[0]] - 1 - i]) {
reMached = true;
} else {
reMached = false;
break;
}
}
if (reMached) {
break;
}//fixme
} else {
matchingWindow[YX[0]]--;
}
}
}
window.add(r1);
}
}
//jion in or link window
//
// for (byte[] t : alltitle) {
// byte nextCompare;
// if (t[0] == r1) {
// nextCompare = t[1];
// }
// }
int titlePointer = 0;
int bytePointer = 0;
}
//ctx.executor().execute(() -> );
//in.readByte()==
}
}