In continuation of my previous question, I have tried a solution.
In short I am trying to inject a byte array as data in object and then also retrieve it back.
Main
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
byte[] bytes = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6};
child1 cc = new child1();
cc.encode(bytes); // this function loads byte to object.
System.out.println(cc.top.var1);
System.out.println(cc.innerBot.var3);
System.out.println(Arrays.toString(cc.decode())); // this function return byte data.
}
}
Child1 child1 class.
import java.math.BigInteger;
public class child1 extends parent {
InnerBot innerBot = new InnerBot();
child1(){
botMap.put("var3", new Row(3, 4, new GetSet() {
@Override
public void set(byte[] bytes) {
innerBot.var3 = new BigInteger(bytes).intValue();
}
@Override
public byte[] get() {
return new BigInteger(String.valueOf(innerBot.var3)).toByteArray();
}
}));
botMap.put("var4", new Row(4, 6, new GetSet() {
@Override
public void set(byte[] bytes) {
innerBot.var4 = new BigInteger(bytes).intValue();
}
@Override
public byte[] get() {
return new BigInteger(String.valueOf(innerBot.var4)).toByteArray();
}
}));
}
public class InnerBot extends Bot {
int var4 = 20;
}
}
parent parent class of child1
import java.math.BigInteger;
import java.util.Arrays;
import java.util.LinkedHashMap;
public class parent {
LinkedHashMap<String, Row> topMap;
LinkedHashMap<String, Row> botMap;
Top top = new Top();
parent() {
topMap = new LinkedHashMap<>();
botMap = new LinkedHashMap<>();
topMap.put("var1", new Row(0, 2, new GetSet() {
@Override
public void set(byte[] bytes) {
// convert byte to int
top.var1 = new BigInteger(bytes).intValue();
}
@Override
public byte[] get() {
return new BigInteger(String.valueOf(top.var1)).toByteArray();
}
}));
topMap.put("var2", new Row(2, 3, new GetSet() {
@Override
public void set(byte[] bytes) {
top.var2 = new BigInteger(bytes).intValue();
}
@Override
public byte[] get() {
return new BigInteger(String.valueOf(top.var2)).toByteArray();
}
}));
}
public void encode(byte[] bytes) {
for (String name : topMap.keySet()) {
Row row = topMap.get(name);
int index1 = row.index1;
int index2 = row.index2;
byte[] newArray = Arrays.copyOfRange(bytes, index1, index2);
row.getSet.set(newArray);
}
for (String name : botMap.keySet()) {
Row row = botMap.get(name);
int index1 = row.index1;
int index2 = row.index2;
byte[] newArray = Arrays.copyOfRange(bytes, index1, index2);
row.getSet.set(newArray);
}
}
public byte[] decode() {
byte[] result = {};
for (String name : topMap.keySet()) {
Row row = topMap.get(name);
byte[] newByte = row.getSet.get();
// concat arrays
int len = result.length;
result = Arrays.copyOf(result, len + newByte.length);
System.arraycopy(newByte, 0, result, len, newByte.length);
}
for (String name : botMap.keySet()) {
Row row = botMap.get(name);
byte[] newByte = row.getSet.get();
// concat arrays
int len = result.length;
result = Arrays.copyOf(result, len + newByte.length);
System.arraycopy(newByte, 0, result, len, newByte.length);
}
return result;
}
}
TOP class.
public class Top {
int var1 = 10;
int var2 = 20;
}
Bot class.
public class Bot {
int var3 = 10;
}
Row to store getset interface.
public class Row {
int index1;
int index2;
GetSet getSet;
public Row(int index1, int index2, GetSet getSet) {
this.index1 = index1;
this.index2 = index2;
this.getSet = getSet;
}
}
GetSet Interface
public interface GetSet {
public void set(byte[] bytes);
public byte[] get();
}
It works but want to know if there is better way to do it using generics or other core java design structures.
Aucun commentaire:
Enregistrer un commentaire