Backwards compatibility

This commit is contained in:
Michael Mikovsky
2025-04-09 22:07:52 -06:00
parent 2210d8d654
commit ea41c5db24
7 changed files with 38 additions and 35 deletions
@@ -77,26 +77,36 @@ public class BuiltByteParser {
byte[] bytes;
ArrayList<parsedObject> objects = new ArrayList<>();
public BuiltByteParser(byte[] bytes){
this.bytes = bytes;
}
public ArrayList<parsedObject> parse() throws byteParsingExeption {
if(bytes.length < lengthHeaderBytes + 1){throw new byteParsingExeption("Invalid length");}
try{
return parse(lengthHeaderBytes); // Try decoding with new format
}catch (byteParsingExeption e){
objects.clear(); // Clear previous attempt at decoding
return parse(lengthHeaderBytes - 1); // Try parsing with old format
}
}
private ArrayList<parsedObject> parse(final int size) throws byteParsingExeption {
if(bytes.length < size + 1){throw new byteParsingExeption("Invalid length");}
int curIndex = 0;
while(true){
// Log.i("t", String.valueOf(curIndex));
final int length = FileEditor.fromBytes(FileEditor.getByteBlock(bytes, curIndex, curIndex+lengthHeaderBytes), lengthHeaderBytes);
final int type = bytes[curIndex+lengthHeaderBytes] & 0xFF;
final int length = FileEditor.fromBytes(FileEditor.getByteBlock(bytes, curIndex, curIndex+size), size);
final int type = bytes[curIndex+size] & 0xFF;
if(length == 0){
curIndex += lengthHeaderBytes;
curIndex += size;
continue;
}
final byte[] block;
try {
block = FileEditor.getByteBlock(bytes, curIndex + lengthHeaderBytes + 1, curIndex + length + lengthHeaderBytes + 1);
block = FileEditor.getByteBlock(bytes, curIndex + size + 1, curIndex + length + size + 1);
} catch(Exception e){
throw new byteParsingExeption("Array out of bounds");
}
@@ -129,7 +139,7 @@ public class BuiltByteParser {
intArrayObject ia = new intArrayObject();
ia.arr = intArr;
// System.out.println(Arrays.toString(intArr));
// System.out.println(Arrays.toString(intArr));lengthHeaderBytes
objects.add(ia);
break;
case 4:
@@ -159,7 +169,7 @@ public class BuiltByteParser {
break;
}
curIndex += length + lengthHeaderBytes + 1;
curIndex += length + size + 1;
if(curIndex == bytes.length){
break;
@@ -58,7 +58,7 @@ public class ByteBuilder {
public byte getType(){return int_id;}
public int length(){return precision;}
public byte[] build(){
return FileEditor.toBytes(num, precision);
return FileEditor.toBytes(num, precision, lengthHeaderBytes);
}
}
private int getLeastBytePrecision(int num){
@@ -162,7 +162,7 @@ public class ByteBuilder {
public byte getType(){return long_id;}
public int length(){return precision;}
public byte[] build(){
return FileEditor.toBytes(num, precision);
return FileEditor.toBytes(num, precision, lengthHeaderBytes);
}
}
private int getLeastBytePrecision(long num){
@@ -225,7 +225,7 @@ public class ByteBuilder {
for(byteType bt : bytesToBuild){
byte[] blockLength = FileEditor.toBytes(bt.length(), lengthHeaderBytes + 1);
byte[] blockLength = FileEditor.toBytes(bt.length(), lengthHeaderBytes + 1, lengthHeaderBytes);
for(int i = 0; i < lengthHeaderBytes; i++) {
bytes[bytesFilled] = blockLength[i];
@@ -51,14 +51,14 @@ public final class FileEditor {
}
public static char byteToChar(int num){
return new String(toBytes(num, 1), StandardCharsets.ISO_8859_1).charAt(0);
public static char byteToChar(int num, int headerSize){
return new String(toBytes(num, 1, headerSize), StandardCharsets.ISO_8859_1).charAt(0);
}
public static byte[] toBytes(int num, int byteCount){
if(num > (Math.pow(lengthHeaderBytes,byteCount*8)-1)){
public static byte[] toBytes(int num, int byteCount, int headerSize){
if(num > (Math.pow(headerSize,byteCount*8)-1)){
throw new BufferOverflowException();
}
byte[] bytes = new byte[byteCount];
@@ -68,8 +68,8 @@ public final class FileEditor {
return bytes;
}
public static byte[] toBytes(long num, int byteCount){
if(num > (Math.pow(lengthHeaderBytes,byteCount*8)-1)){
public static byte[] toBytes(long num, int byteCount, int headerSize){
if(num > (Math.pow(headerSize,byteCount*8)-1)){
throw new BufferOverflowException();
}
byte[] bytes = new byte[byteCount];
@@ -133,7 +133,7 @@ public final class FileEditor {
return outputStream.toByteArray();
}
public static byte[] blockCompress(byte[] inputData) {
public static byte[] blockCompress(byte[] inputData, int headerSize) {
List<byte[]> compiledData = new ArrayList<>();
for(int i = 0; i<Math.ceil((double) inputData.length / FileEditor.maxCompressedBlockSize); i++){
@@ -147,7 +147,7 @@ public final class FileEditor {
final byte[] compressedBlock = FileEditor.compress(dataBlock);
compiledData.add(FileEditor.toBytes(compressedBlock.length, 2));
compiledData.add(FileEditor.toBytes(compressedBlock.length, 2, headerSize));
compiledData.add(compressedBlock);
}
return combineByteArrays(compiledData);