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
@@ -6,6 +6,7 @@ import com.ridgebotics.ridgescout.utility.ByteBuilder;
import com.ridgebotics.ridgescout.utility.FileEditor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Objects;
public class ScoutingFile {
@@ -53,19 +54,11 @@ public class ScoutingFile {
ScoutingFile f = new ScoutingFile();
f.filename = (String) objects.get(0).get();
// ByteArrayOutputStream fileData = new ByteArrayOutputStream();
//
// for(int i = 1; i < objects.size(); i++){
// byte[] blockBytes = (byte[]) objects.get(i).get();
// fileData.write(blockBytes, (i-1)*65535, blockBytes.length);
// }
f.data = (byte[]) objects.get(1).get();
return f;
}catch (BuiltByteParser.byteParsingExeption e){
}catch (Exception e){
AlertManager.error(e);
return null;
}
@@ -27,7 +27,7 @@ public class frcMatch {
.addInt(redAlliance[1])
.addInt(redAlliance[2])
.build();
} catch (ByteBuilder.buildingException e) {
} catch (Exception e) {
AlertManager.error(e);
return new byte[1];
}
@@ -49,7 +49,7 @@ public class frcMatch {
return frc;
} catch (BuiltByteParser.byteParsingExeption e) {
} catch (Exception e) {
AlertManager.error(e);
return null;
}
@@ -101,7 +101,7 @@ public class BluetoothSenderFragment extends Fragment {
private void sendData() {
try {
byte[] compressed = FileEditor.blockCompress(data_to_send);
byte[] compressed = FileEditor.blockCompress(data_to_send, FileEditor.lengthHeaderBytes);
for(int i = 0; i < Math.ceil((double) compressed.length/1024); i++){
bluetoothSender.sendData(FileEditor.getByteBlock(compressed, i*1024, (i+1)*1024));
@@ -76,7 +76,7 @@ public class CodeGeneratorView extends Fragment {
qrIndexN = binding.qrIndexN;
qrIndexD = binding.qrIndexD;
String compressed = new String(FileEditor.blockCompress(data), StandardCharsets.ISO_8859_1);
String compressed = new String(FileEditor.blockCompress(data, FileEditor.lengthHeaderBytes), StandardCharsets.ISO_8859_1);
if(compressed.isEmpty()){
AlertManager.alert("Error!", "Empty data!");
@@ -190,10 +190,10 @@ public class CodeGeneratorView extends Fragment {
try {
// alert("test", ""+Math.ceil((double)data.length()/(double)qrSize));
qrBitmaps.add(generateQrCode(
FileEditor.byteToChar(FileEditor.internalDataVersion) +
String.valueOf(FileEditor.byteToChar(randID)) +
FileEditor.byteToChar(i) +
FileEditor.byteToChar(qrCount - 1) +
FileEditor.byteToChar(FileEditor.internalDataVersion, FileEditor.lengthHeaderBytes) +
String.valueOf(FileEditor.byteToChar(randID, FileEditor.lengthHeaderBytes)) +
FileEditor.byteToChar(i, FileEditor.lengthHeaderBytes) +
FileEditor.byteToChar(qrCount - 1, FileEditor.lengthHeaderBytes) +
data.substring(start, end)
));
// alert("title", ""+(qrCount-1));
@@ -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);