Premier commit - Inclusion dans le système git

This commit is contained in:
Mysaa 2021-05-24 14:19:05 +02:00
commit d75c03b786
7 changed files with 207 additions and 0 deletions

9
.gitignore vendored Normal file
View File

@ -0,0 +1,9 @@
.project
.settings
.classpath
JDA*.jar
bin/
doc/
doc.zip
doc.tar.gz

View File

@ -0,0 +1,39 @@
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Paths;
import java.util.HashMap;
import com.bernard.file.BernardFile;
import com.bernard.file.BernardParam;
public class BernardFilesTestMain {
public static void read(String[] args) {
try {
BernardFile file = new BernardFile(Paths.get("/home/mysaa/Documents/", "test.bernard"));
int data;
while((data = file.iStream().read()) != -1)
System.out.print((char)data);
file.iStream().close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Terminé");
}
public static void write(String[] args) {
try {
HashMap<BernardParam, byte[]> params = new HashMap<BernardParam, byte[]>();
params.put(BernardParam.ProgramID, new byte[] {0});
params.put(BernardParam.FileVersion,"beta-1".getBytes());
params.put(BernardParam.CreationTimestamp,ByteBuffer.allocate(Long.BYTES).putLong(System.currentTimeMillis()).array());
BernardFile file = new BernardFile(Paths.get("/home/mysaa/Documents/", "test.bernard"), params);
file.oStream().write("Le super contenu du fichier".getBytes());
file.oStream().close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Terminé");
}
}

View File

@ -0,0 +1,101 @@
package com.bernard.file;
import java.io.*;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class BernardFile{
public static final long bernardFileTempSignature = 0x4242424242424242L;
public static final byte headerEndByte = 0x42;
Path fichier;
Map<BernardParam, byte[]> params;//TODO change to Bernard's special byteArray (BinaryNumber)
int headerLength;
FileInputStream iStream;
FileOutputStream oStream;
public BernardFile(Path fichier) {
this.fichier = fichier;
params = new HashMap<>();
try {
iStream = new FileInputStream(fichier.toFile());
readHeaders(iStream);
} catch (FileNotFoundException e) {
throw new RuntimeException("Je ne peut pas charger le fichier néant ...",e);
} catch (IOException e) {
throw new RuntimeException("Je n'arrive pas a jouer avec le fichier ...",e);
}
}
public BernardFile(Path fichier,Map<BernardParam, byte[]> params) {
this.params = params;
try {
oStream = new FileOutputStream(fichier.toFile());
writeHeaders(oStream);
} catch (IOException e) {
throw new RuntimeException("Je n'arrive pas a jouer avec le fichier ...",e);
}
}
public void readHeaders(InputStream file) throws IOException {
DataInputStream din = new DataInputStream(file);
headerLength = 0;
if(!verifySignature(din))throw new IllegalArgumentException("Le fichier ne possède pas la signature des fichiers bernard !");
while(true) {
int ffCount = 0;
byte r;
while((r = din.readByte()) != 0xFF)
ffCount++;
Long key = 0xFFL*ffCount + r;
if(key == (headerEndByte & 0xFF))break;
headerLength += ffCount + 1;
int payloadLength = din.readInt();
byte[] payload = new byte[payloadLength];
din.read(payload);
headerLength += payloadLength;
params.put(BernardParam.getFromKey(key), payload);
}
}
public void writeHeaders(OutputStream file) throws IOException {
DataOutputStream dos = new DataOutputStream(file);
headerLength = 0;
writeSignature(dos);
for(Entry<BernardParam,byte[]> param : params.entrySet()) {
Long key = param.getKey().getKey();
while((key-=0xFF) >= 0xFF) {
dos.writeByte(0xFF);
headerLength++;
}
dos.writeByte(key.byteValue());
headerLength ++;
byte[] payload = param.getValue();
dos.write(payload);
headerLength += payload.length;
}
dos.writeByte(BernardFile.headerEndByte);
}
public FileInputStream iStream() {
return iStream;
}
public void readHeaders(File file) throws IOException {
readHeaders(new FileInputStream(file));
}
public final boolean verifySignature(DataInputStream signature) throws IOException {
headerLength += 8;
return signature.readLong() == BernardFile.bernardFileTempSignature;
}
public final void writeSignature(DataOutputStream signature) throws IOException {
headerLength += 8;
signature.writeLong(BernardFile.bernardFileTempSignature);
}
}

View File

@ -0,0 +1,29 @@
package com.bernard.file;
public enum BernardParam {
FileVersion(0x0A);
Long key;
private BernardParam(Long key) {
this.key = key;
}
private BernardParam(Integer key) {
this.key = key.longValue();
}
public Long getKey() {
return key;
}
public static BernardParam getFromKey(Long key) {
for (BernardParam param : values()) {
if(param.key.equals(key))return param;
}
return null;
}
}

View File

@ -0,0 +1,13 @@
package com.bernard.inutil;
public class BinarYnutil {
public static int concat(byte b1,byte b2,byte b3,byte b4) {
return (int)b1 << 0x30 + (int)b2 << 0x20 + (int)b3 << 0x10 + (int)b4;
}
public static int concatInt(byte... data) {
assert data.length ==4;
return concat(data[0],data[1],data[2],data[3]);
}
}

View File

@ -0,0 +1,7 @@
package com.bernard.math.geom;
public interface Figure {
public boolean appartient(Point p);
}

View File

@ -0,0 +1,9 @@
package com.bernard.math.geom;
public class Point {
double[] coords;
}