Work on improving ftp timestamp

This commit is contained in:
Astatin3
2024-10-07 20:16:42 -06:00
parent fb0718c4ec
commit fecf7e5d2b
4 changed files with 145 additions and 26 deletions
@@ -10,6 +10,7 @@ public class BuiltByteParser {
public static final Integer stringType = 2;
public static final Integer intArrayType = 3;
public static final Integer stringArrayType = 4;
public static final Integer longType = 5;
public class byteParsingExeption extends Exception {
public byteParsingExeption() {}
@@ -58,6 +59,11 @@ public class BuiltByteParser {
public Integer getType(){return stringArrayType;}
public Object get(){return arr;}
}
public class longObject extends parsedObject{
long num;
public Integer getType(){return longType;}
public Object get(){return num;}
}
public class rawObject extends parsedObject {
@@ -140,6 +146,11 @@ public class BuiltByteParser {
sa.arr = StringArr;
objects.add(sa);
break;
case 5:
longObject lo = new longObject();
lo.num = fileEditor.fromBytesLong(block, length);
objects.add(lo);
break;
default:
rawObject ro = new rawObject(type);
ro.bytes = block;
@@ -9,6 +9,7 @@ public class ByteBuilder {
public static final int string_id = 2;
public static final int int_arr_id = 3;
public static final int string_arr_id = 4;
public static final int long_id = 5;
ArrayList<byteType> bytesToBuild = new ArrayList<>();
@@ -153,6 +154,41 @@ public class ByteBuilder {
return this;
}
private class longType extends byteType {
public int precision;
public long num;
public byte getType(){return long_id;}
public int length(){return precision;}
public byte[] build(){
return fileEditor.toBytes(num, precision);
}
}
private int getLeastBytePrecision(long num){
if(num <= 1){return 1;}
return (int) Math.ceil(Math.log(Math.abs(num))/Math.log(8));
}
public ByteBuilder addLong(long num) throws buildingException {
// Get closest number of bytes
int precision = getLeastBytePrecision(num);
return addLong(num, precision);
}
public ByteBuilder addLong(long num, int precision) throws buildingException {
if(precision <= 0){throw new buildingException("Invalid precision: " + precision);}
if(precision > 65536){throw new buildingException("Precision too large (greter than 65536)");}
if(precision < getLeastBytePrecision(num)){throw new buildingException("Precision too small");}
if(num > Long.MAX_VALUE){throw new buildingException("Long overflow");}
if(num < Long.MIN_VALUE){throw new buildingException("Long overflow");}
longType longType = new longType();
longType.num = num;
longType.precision = precision;
bytesToBuild.add(longType);
return this;
}
private class rawType extends byteType {
public int type;
public byte[] bytes;
@@ -66,6 +66,17 @@ public final class fileEditor {
return bytes;
}
public static byte[] toBytes(long num, int byteCount){
if(num > (Math.pow(2,byteCount*8)-1)){
throw new BufferOverflowException();
}
byte[] bytes = new byte[byteCount];
for(int i=0;i<byteCount;i++){
bytes[i] = (byte)(num >> (i*8));
}
return bytes;
}
public static int fromBytes(byte[] bytes, int byteCount){
@@ -75,6 +86,13 @@ public final class fileEditor {
}
return returnInt;
}
public static long fromBytesLong(byte[] bytes, int byteCount){
long returnLong = 0;
for(int i=0;i<byteCount;i++){
returnLong |= (bytes[i] & 0xFFL) << (i*8);
}
return returnLong;
}