shit

tech2025-03-05  9

/**

author duke 2020/9 */

/**

the Basement to all type of ByteFormsimply explaining that maintain a intact and succession byte Array and Access by Item belong that byteFormItem hold a PositionAddress in Array to access bytes and each item fix a Length*/

public abstract class ByteForm {

abstract List<FormatItem> initList(); abstract FormatItem initItem(int length);

abstract FormatItem<VALUE> initItem(int length, VALUE value); abstract FormatItem<VALUE> initItem(byte data[]); abstract FormatItem<VALUE> initItem(byte data[], VALUE value); abstract ByteForm<VALUE> copy(String name); /**invoke when copy*/ abstract FormatItem<VALUE> imitation(FormatItem<VALUE> target); /***/ abstract byte[] source(int length_total); /**!!!!!!todo 注意 修改器的操作 一定情况下允许一个修改器可执行 //fixme 多个修改器引用 在多个线程调用会出现《完鸡巴蛋》级别的问题 请保持修改器执行函数对于list target的原子化 无论list 是否线程安全 // todo 或者合并未执行的修改器操作 所以对修改器的管理在子类实现 abstract Modifier getModifier(); private final List<FormatItem<VALUE>> unitsTable = initList(); private final String name;//name of that format private final byte[] Empt = new byte[0]; protected byte[] sourceData = Empt; protected int contentLength_total = 0;//total byte length /** * generate new bytes fix to the foramtable */ public byte[] getSupportArray() { return sourceData; } public int getContentLength_total() { return contentLength_total; } // public ByteForm(String formatName) { this.name = formatName; } public ByteForm(String formatName, Consumer<Modifier> modifier) { this.name = formatName; Modifier modifier1 = getModifier(); modifier.accept(modifier1); modifier1.effect(); //todo } public String getName() { return name; } protected final void recalculate(FormatItem<VALUE> index) { boolean checkPoint = false; int pos = 0; for (FormatItem<VALUE> item : unitsTable) { if (checkPoint) { item.setStart_position(pos); pos += item.getLength(); } else { if (item == index) { checkPoint = true; pos = index.position() + item.getLength(); //todo index refresh } } } } protected final void recalculate(int index) { List<FormatItem<VALUE>> list = index == 0 ? unitsTable : unitsTable.subList(index, unitsTable.size()); FormatItem<VALUE> startItem = list.get(0); int pos = startItem.position() + startItem.getLength(); for (FormatItem<VALUE> item : list) { item.setStart_position(pos); pos += item.getLength(); item.setSequence(index++); } } // public int indexOf(ByteFormatUnit<VALUE> o) { return unitsTable.indexOf(o); } public final ByteFormatUnit<VALUE> getItem(int index) { return unitsTable.get(index); } public final int size() { return unitsTable.size(); } public final void forEachItem(Consumer<ByteFormatUnit<VALUE>> consumer) { unitsTable.forEach(consumer); } public final Stream<? extends ByteFormatUnit<VALUE>> getStream() { return unitsTable.stream(); } public final List<? extends ByteFormatUnit<VALUE>> subList(int fromIndex, int toIndex) { List<FormatItem<VALUE>> formatItems = unitsTable.subList(fromIndex, toIndex); return formatItems; } public final Iterator<? extends ByteFormatUnit<VALUE>> itemIterator() { class safeWarp implements Iterator<ByteFormatUnit<VALUE>> { public safeWarp(Iterator<? extends ByteFormatUnit<VALUE>> iteratorRaw) { iterator = iteratorRaw; } final Iterator<? extends ByteFormatUnit<VALUE>> iterator; @Override public boolean hasNext() { return iterator.hasNext(); } @Override public ByteFormatUnit<VALUE> next() { return iterator.next(); } @Override public void remove() { } @Override public void forEachRemaining(Consumer action) { iterator.forEachRemaining(action); } } return new safeWarp(unitsTable.iterator()); } public final void forEach(Consumer<ByteFormatUnit<VALUE>> action) { unitsTable.forEach(action); } /** * record the position at sourceData(byte[]) and DataType * manage by FormatTable */ protected abstract class FormatItem<VALUE> implements ByteFormatUnit<VALUE> { String info = ""; int order = -1; int start_position = 0; final int byteLength; byte[] source = Empt; protected void release() { source = null; order = -1; start_position = 0; } protected FormatItem(byte... defaultData) { source = defaultData; this.byteLength = defaultData.length; } protected FormatItem(int byteLength) { this.byteLength = byteLength; } @Override public int getOrder() { return order; } @Override public int position() { return start_position; } @Override public int getLength() { return byteLength; } @Override public String getInfo() { return info; } @Override public String setInfo(String info) { String pre = this.info; this.info = info; return pre; } //------------------// protected final int checkOrder() { return ByteForm.this.unitsTable.indexOf(this); } protected final void setStart_position(int start_position) { this.start_position = start_position; } protected final byte[] getSource() { return source; } protected final boolean nullSource() { return source == Empt; } protected final FormatItem<VALUE> setSource(byte[] bytes) { if (bytes == null || bytes.length == 0) { source = Empt; } else { source = bytes; } return this; } protected final FormatItem<VALUE> setSequence(int order) { this.order = order; return this; } //-----------------// @Override public byte getByte(int index) { return getOne(index); } @Override public byte getByte(int index, byte substituted) { if (getSource().length < index) return substituted; return getOne(index); } @Override public void scene(int index, ByteReader consumer) { final int length = getSource().length; if (Math.abs(index) > length) return; if (index > 0) { for (int i = index; i < index + length; i++) { int i1 = i % length; consumer.accept(i1, getOne(i1)); } return; } if (index < 0) { for (int i = index; i >= index - length; i--) { int i1 = Math.abs(i % length); consumer.accept(i1, getOne(i1)); } return; } if (index == 0) { scene(consumer); return; } } @Override public void scene(int index, ByteVisitor consumer) { final int length = getSource().length; if (Math.abs(index) > length) return; if (index > 0) { for (int i = index; i < index + length; i++) { int i1 = i % length; if (!consumer.accept(i1, getOne(i1))) return; } return; } if (index < 0) { for (int i = index; i >= index - length; i--) { int i1 = Math.abs(i % length); if (!consumer.accept(i1, getOne(i1))) return; } return; } if (index == 0) { scene(consumer); return; } } @Override public void scene(ByteReader consumer) { for (int i = 0; i < getSource().length; i++) { consumer.accept(i, getOne(i)); } } @Override public void scene(ByteVisitor visitor) { for (int i = 0; i < getSource().length; i++) { if (!visitor.accept(i, getOne(i))) { return; } } } @Override public void setContent(int index, byte value) { setOne(index, value); } @Override public void setContents(int index, byte... value) { final int length = Math.min(getSource().length, value.length); if (Math.abs(index) > length) return; if (index > 0) { for (int i = index; i < index + length; i++) { int i1 = i % length; setOne(i1, value[i1]); } return; } if (index < 0) { for (int i = index; i >= index - length; i--) { int i1 = Math.abs(i % length); setOne(i1, value[i1]); } return; } if (index == 0) { for (int i = 0; i < length; i++) { setOne(i, value[i]); } return; } } @Override public void setContents(byte... value) { for (int i = 0; i < Math.min(getSource().length, value.length); i++) { setOne(i, value[i]); } } @Override public int setInContents(byte... value) { int len = Math.min(getSource().length, value.length); for (int i = 0; i < len; i++) { setOne(i, value[i]); } return len; } @Override public int setInContents(int index, byte... value) { final int length = Math.min(getSource().length, value.length); if (Math.abs(index) > length) return 0; if (index > 0) { for (int i = index; i < index + length; i++) { int i1 = i % length; setOne(i1, value[i1]); } return length; } if (index < 0) { for (int i = index; i >= index - length; i--) { int i1 = Math.abs(i % length); setOne(i1, value[i1]); } return length; } if (index == 0) { for (int i = 0; i < length; i++) { setOne(i, value[i]); } return length; } return 0; } private byte getOne(int index) { byte a = getSource()[start_position + index]; return a; } private void setOne(int index, byte data) { getSource()[start_position + index] = data; } @Override public final byte[] snapData() { byte[] bytes = new byte[byteLength]; for (int i = 0; i < bytes.length; i++) { bytes[i] = getOne(i); } return bytes; } } /** * when user invoke that Modifier * index in the corresponds List which to modify will compose a sequence of Operate Pos from 0-n * <p> * invoke effect at last to modifier the List */ public final class Modifier { boolean clearAll = false; /***/ TreeMap<Integer, OperateWill> operateQueue = new TreeMap(); private final class OperateWill { LinkedList<FormatItem<VALUE>> getAppends() { return appends; } public boolean preRemoved() { return preRemoved; } int removeExtend = 0;//remove amount of Item after that index boolean preRemoved = false;//remove or instead item at index //notice that if first is null mean instead with next if removed final LinkedList<FormatItem<VALUE>> appends = new LinkedList<>(); //notice increase calculate byte will add in include set in and appends int increase = 0; OperateWill setIn(FormatItem<VALUE> item) { if (preRemoved) { if (item != null) { FormatItem<VALUE> pre = appends.set(0, item); if (pre != null) increase -= pre.getLength(); item.getLength(); } } else { appends.addFirst(item); preRemoved = true; if (item != null) increase += item.getLength(); } return this; } OperateWill appendOne(FormatItem<VALUE> item) { if (item != null) { appends.addLast(item); increase += item.byteLength; } return this; } OperateWill appendRemove(int length) { removeExtend = length; return this; } //that will merge all remove operate( remove at local index achieve the length so choose longest ) // insert Operate will link to tail } private final List<FormatItem<VALUE>> getTarget() { return unitsTable; } private final boolean checkIndex(int index) { return index >= 0 && index < unitsTable.size(); } /***/ public Modifier clearAll() { clearAll = true; return this; } /***/ public Modifier remove(int... index) { for (int i : index) { if (checkIndex(i)) { reserveSet(i, null); } } return this; } /***/ public Modifier remove(int index) { if (checkIndex(index)) { reserveSet(index, null); } return this; } /***/ public Modifier removeFrom(int startIndex, int length) { if (checkIndex(startIndex)) { reserveDelete(startIndex, length); } return this; } /***/ public Modifier setIn(int index, int length) { if (checkIndex(index)) { reserveSet(index, initItem(length)); } return this; } /***/ public Modifier setIn(int index, byte... data) { if (checkIndex(index)) { reserveSet(index, initItem(data)); } return this; } /***/ public Modifier setIn(int index, VALUE value, byte... data) { if (checkIndex(index)) { reserveSet(index, initItem(data, value)); } return this; } /***/ public Modifier setIn(int index, int length, VALUE value) { if (checkIndex(index)) { reserveSet(index, initItem(length, value)); } return this; } /***/ public Modifier addTail(int length) { reserveInsert(getTarget().size(), length, initItem(length)); return this; } /***/ public Modifier addTail(int length, VALUE value) { reserveInsert(getTarget().size(), length, initItem(length, value)); return this; } /***/ public Modifier addTail(byte... data) { reserveInsert(getTarget().size(), data.length, initItem(data)); return this; } /***/ public Modifier addTail(int num, byte[] data) { reserveInserts(getTarget().size(), data, num); return this; } /***/ public Modifier addTail(byte[]... datas) { reserveInserts(getTarget().size(), datas); return this; } /***/ public Modifier insert(int index, int length) { if (checkIndex(index)) reserveInsert(index, length, initItem(length)); return this; } /***/ public Modifier insert(int index, byte... data) { if (checkIndex(index)) reserveInsert(index, data.length, initItem(data)); return this; } /***/ public Modifier insert(int index, VALUE value, byte... data) { if (checkIndex(index)) reserveInsert(index, data.length, initItem(data, value)); return this; } /***/ public Modifier insert(int index, int length, VALUE value) { if (checkIndex(index)) reserveInsert(index, length, initItem(length, value)); return this; } private void reserveInsert(int index, int length, FormatItem<VALUE> valueFormatItem) { OperateWill willOperate = operateQueue.get(index); if (willOperate == null) { operateQueue.put(index, new OperateWill().appendOne(valueFormatItem)); } else { willOperate.appendOne(valueFormatItem); } } private void reserveInserts(int index, byte[] data, int num) { OperateWill willOperate = operateQueue.get(index); if (willOperate == null) { operateQueue.put(index, willOperate = new OperateWill()); } for (int i = 0; i < num; i++) { willOperate.appendOne(initItem(data)); } } private void reserveInserts(int index, byte[][] datas) { OperateWill willOperate = operateQueue.get(index); if (willOperate == null) { operateQueue.put(index, willOperate = new OperateWill()); } for (int i = 0; i < datas.length; i++) { willOperate.appendOne(initItem(datas[i])); } } private void reserveSet(int index, FormatItem<VALUE> valueFormatItem) { OperateWill willOperate = operateQueue.get(index); if (willOperate == null) { operateQueue.put(index, new OperateWill().setIn(valueFormatItem)); } else { willOperate.setIn(valueFormatItem); } } private void reserveDelete(int index, int length) { OperateWill willOperate = operateQueue.get(index); if (willOperate == null) { operateQueue.put(index, new OperateWill().appendRemove(length)); } else { willOperate.appendRemove(length); } } /***/ public final ByteForm<VALUE> effect() { final List<FormatItem<VALUE>> target = getTarget(); int lengthGenerate = disposeQueue(); if (lengthGenerate < 0) return ByteForm.this;//that mean no change submit byte[] sourceNeo = new byte[lengthGenerate]; tidyList(target, sourceNeo); return ByteForm.this; } private void checkOperateQueue() { System.out.println(" print Operate queue"); operateQueue.forEach((integer, willOperate) -> { System.out.println(integer + "removed:" + willOperate.preRemoved + "increase:" + willOperate.increase + "" + "appends" + willOperate.appends.size() + ""); }); } /**iterate Operate queue perform remove and substitute * @return return the new source theory length * notice if length ==-1 mean doNothing to raw list * */ private int disposeQueue() { int total = ByteForm.this.contentLength_total; Iterator<Map.Entry<Integer, OperateWill>> operateIterator = operateQueue.entrySet().iterator(); / if (clearAll) { getTarget().clear(); total = 0; } else { if (!operateIterator.hasNext()) return -1; } if (getTarget().isEmpty()) // deal list empty //just insert { total = 0; List<FormatItem<VALUE>> target = getTarget(); if (clearAll) target.clear(); while (operateIterator.hasNext()) { Map.Entry<Integer, OperateWill> next = operateIterator.next(); OperateWill operate = next.getValue(); total += operate.increase; LinkedList<FormatItem<VALUE>> appends = operate.getAppends(); if (!appends.isEmpty()) { if (operate.preRemoved) { FormatItem<VALUE> substitute = appends.pollFirst(); if (substitute != null) { appends.addLast(substitute); } } target.addAll(appends); } } return total; } / else// deal list not empty { OperateWill operating = operateIterator.next().getValue(); total += operating.increase; int item_cursor = operateQueue.firstKey(); int operate_cursor = item_cursor; int recentInsertCursor = -1; List<FormatItem<VALUE>> itemList = getTarget(); ListIterator<FormatItem<VALUE>> targetIterator = itemList.listIterator(item_cursor); int removeLasting = 0; // //continue while (targetIterator.hasNext()) { FormatItem<VALUE> item = targetIterator.next();//!!!case of item_cursor++ boolean removeNeed = removeLasting > 0; if (operate_cursor == item_cursor)//detect operate execute operate { LinkedList<FormatItem<VALUE>> appends = operating.getAppends(); if (operating.preRemoved()) // { if (removeNeed) removeLasting--; total -= item.getLength(); FormatItem<VALUE> substitute = appends.pollFirst(); targetIterator.set(substitute); } else { if (removeNeed) { removeLasting--; targetIterator.set(null); } } / if (!appends.isEmpty()) { int backCount = 0; while ((item_cursor - backCount > recentInsertCursor) && targetIterator.hasPrevious() && !appends.isEmpty()) { if (targetIterator.previous() == null) { backCount++; } else { targetIterator.next(); break; } } for (int i = 0; i < backCount; i++) { targetIterator.set(appends.pollFirst()); targetIterator.next(); } if (!appends.isEmpty()) { recentInsertCursor = item_cursor; } else { operateIterator.remove(); } } else { operateIterator.remove(); } / removeLasting = Math.max(operating.removeExtend, removeLasting); if (operateIterator.hasNext()) { final Map.Entry<Integer, OperateWill> entryNext = operateIterator.next();//next operate OperateWill operateWill = entryNext.getValue(); operate_cursor = entryNext.getKey(); operating = operateWill; total += operateWill.increase; } else { break; } } else { if (removeNeed) { removeLasting--; targetIterator.set(null); total -= item.getLength(); } } item_cursor++;//!!! continue; } if (removeLasting > 0) { while (targetIterator.hasNext() && removeLasting > 0) { removeLasting--; FormatItem<VALUE> next = targetIterator.next(); total -= next.getLength(); targetIterator.set(null); } } if (operateIterator.hasNext()) { operateIterator.forEachRemaining(integerwillOperateEntry -> { contentLength_total += integerwillOperateEntry.getValue().increase; }); } insert All if (!operateQueue.isEmpty()) { Iterator<Map.Entry<Integer, OperateWill>> iterator = operateQueue.entrySet().iterator(); int addSwap = 0; while (iterator.hasNext()) { Map.Entry<Integer, OperateWill> next = iterator.next(); LinkedList<FormatItem<VALUE>> value = next.getValue().appends; itemList.addAll(next.getKey() + addSwap, value); addSwap += value.size(); } } operateQueue.clear(); /insert All return total; } } /** * 进行序号以及地址的整理//整理数组//移除null (根据移除数量) * */ private final void tidyList(List<FormatItem<VALUE>> target, byte[] sourceNeo) { contentLength_total = sourceNeo.length; System.err.println(contentLength_total); final int[] orderCount = {0}; final int[] posCount = {0}; target.removeIf(item -> { boolean isNull = item == null; if (!isNull) { int itemLength = item.getLength(); int pos = posCount[0]; try { if (!item.nullSource()) { for (int i = 0; i < itemLength; i++) { sourceNeo[pos + i] = item.getByte(i); System.out.println(item.getByte(i)); } } } catch (Exception e) { e.printStackTrace(); } item.setStart_position(pos); item.setSource(sourceNeo); item.setSequence(orderCount[0]); posCount[0] += itemLength; orderCount[0]++; } return isNull; }); sourceData = sourceNeo; } } public static interface ByteFormatUnit<VALUE> { String getInfo(); String setInfo(String s); int getOrder(); int position(); int getLength(); VALUE getValue(); VALUE setValue(VALUE value); byte[] snapData(); byte getByte(int index); byte getByte(int index, byte substituted); void scene(int index, ByteReader consumer); void scene(int index, ByteVisitor consumer); void scene(ByteReader consumer); void scene(ByteVisitor visitor); void setContent(int index, byte value); void setContents(int index, byte... value); void setContents(byte... value); int setInContents(byte... value); int setInContents(int index, byte... value); } @FunctionalInterface public static interface ByteVisitor { boolean accept(int index, byte data); } @FunctionalInterface public static interface ByteReader { void accept(int index, byte data); } } 在这里插入代码片

import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; import java.util.stream.Stream;

public class Tast {

public static void main(String[] args) { class ByteFormTast extends ByteForm<String> { public ByteFormTast(String formatName) { super(formatName); } public ByteFormTast(String formatName, Consumer<Modifier> modifier) { super(formatName, modifier); } @Override List<FormatItem<String>> initList() { return new ArrayList<>(128); } @Override FormatItem initItem(int length) { Item item = new Item(length); return item; } @Override FormatItem initItem(int length, String o) { Item item = new Item(length); item.setValue(o); return item ; } @Override FormatItem initItem(byte[] data) { Item item = new Item(data); return item; } @Override FormatItem initItem(byte[] data, String o) { Item item = new Item(data); item.setValue(o); return item; } @Override ByteForm copy(String name) { return null; } @Override FormatItem<String> imitation(FormatItem<String> target) { return null; } @Override byte[] source(int length_total) { return new byte[0]; } @Override Modifier getModifier() { return new Modifier(); } class Item extends ByteForm.FormatItem { public Item(byte... defaultData) { super(defaultData); } public Item(int byteLength) { super(byteLength); } @Override public Object getValue() { return null; } @Override public Object setValue(Object o) { return null; } } } ByteFormTast byteFormTast = new ByteFormTast("", modifier -> { for (int i = 0; i < 20; i++) { modifier.addTail(new byte[]{(byte) i}); } });//20+20 System.out.println(byteFormTast.contentLength_total); byteFormTast.forEach(formatUnit -> { System.out.println("n:" + formatUnit.getOrder() + " len:" + formatUnit.getLength() + " bytes:" + Arrays.toString(formatUnit.snapData())); }); System.out.println(Arrays.toString(byteFormTast.getSupportArray())); tastRemoveAndInsert( byteFormTast); System.out.println(byteFormTast.contentLength_total); System.out.println(Arrays.toString(byteFormTast.getSupportArray())); byteFormTast.forEach(formatUnit -> { System.out.println("n:" + formatUnit.getOrder() + " len:" + formatUnit.getLength() + " bytes:" + Arrays.toString(formatUnit.snapData())); }); System.out.println(byteFormTast.contentLength_total); byteFormTast.getStream(); } private static void tastInsert(ByteForm byteFormTast) { byteFormTast.getModifier() .addTail(10, new byte[]{0x12, 0x12}).addTail(new byte[]{66})//11+21 .insert(10, new byte[]{10, 6, 6})//1+3 .insert(10, new byte[]{101, 6, 6})//1+3 .insert(11, new byte[]{11, 6, 6})//1+3 .insert(6, new byte[]{6, 6, 6})//1+3 .insert(0, new byte[]{0, 1, 6, 6, 6})//1+5 .effect();//15+35 } private static void tastRemove(ByteForm byteFormTast) { byteFormTast.getModifier() .remove(1, 3, 5, 10, 11, 13) .removeFrom(6, 3) .effect(); } private static void tastRemoveAndInsert(ByteForm byteFormTast) { byteFormTast.getModifier() .removeFrom(11, 3) .removeFrom(13, 3) .insert(10, 3) .insert(10, 3) .effect(); } private static void tastSetIn(ByteForm byteFormTast) { byteFormTast.getModifier() .setIn(0, new byte[]{0, 0, 0}) .setIn(3, new byte[]{3, 3, 3}) .setIn(6, new byte[]{6, 6, 6, 6, 6, 6}) .setIn(11, new byte[]{11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11}) .effect(); }

}

最新回复(0)