From d75c03b786ca7a20e4872144ca3ca4731c5b5b48 Mon Sep 17 00:00:00 2001 From: Mysaa Date: Mon, 24 May 2021 14:19:05 +0200 Subject: [PATCH] =?UTF-8?q?Premier=20commit=20-=20Inclusion=20dans=20le=20?= =?UTF-8?q?syst=C3=A8me=20git?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 9 +++ src/BernardFilesTestMain.java | 39 +++++++++ src/com/bernard/file/BernardFile.java | 101 ++++++++++++++++++++++++ src/com/bernard/file/BernardParam.java | 29 +++++++ src/com/bernard/inutil/BinarYnutil.java | 13 +++ src/com/bernard/math/geom/Figure.java | 7 ++ src/com/bernard/math/geom/Point.java | 9 +++ 7 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 src/BernardFilesTestMain.java create mode 100644 src/com/bernard/file/BernardFile.java create mode 100644 src/com/bernard/file/BernardParam.java create mode 100644 src/com/bernard/inutil/BinarYnutil.java create mode 100644 src/com/bernard/math/geom/Figure.java create mode 100644 src/com/bernard/math/geom/Point.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..abe7a2e --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.project +.settings +.classpath +JDA*.jar +bin/ + +doc/ +doc.zip +doc.tar.gz diff --git a/src/BernardFilesTestMain.java b/src/BernardFilesTestMain.java new file mode 100644 index 0000000..58f93e3 --- /dev/null +++ b/src/BernardFilesTestMain.java @@ -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 params = new HashMap(); + 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é"); + } + +} diff --git a/src/com/bernard/file/BernardFile.java b/src/com/bernard/file/BernardFile.java new file mode 100644 index 0000000..d2e25e8 --- /dev/null +++ b/src/com/bernard/file/BernardFile.java @@ -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 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 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 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); + } + +} diff --git a/src/com/bernard/file/BernardParam.java b/src/com/bernard/file/BernardParam.java new file mode 100644 index 0000000..590ad28 --- /dev/null +++ b/src/com/bernard/file/BernardParam.java @@ -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; + } + +} diff --git a/src/com/bernard/inutil/BinarYnutil.java b/src/com/bernard/inutil/BinarYnutil.java new file mode 100644 index 0000000..d5f3ef5 --- /dev/null +++ b/src/com/bernard/inutil/BinarYnutil.java @@ -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]); + } + +} diff --git a/src/com/bernard/math/geom/Figure.java b/src/com/bernard/math/geom/Figure.java new file mode 100644 index 0000000..602654c --- /dev/null +++ b/src/com/bernard/math/geom/Figure.java @@ -0,0 +1,7 @@ +package com.bernard.math.geom; + +public interface Figure { + + public boolean appartient(Point p); + +} diff --git a/src/com/bernard/math/geom/Point.java b/src/com/bernard/math/geom/Point.java new file mode 100644 index 0000000..76f259c --- /dev/null +++ b/src/com/bernard/math/geom/Point.java @@ -0,0 +1,9 @@ +package com.bernard.math.geom; + +public class Point { + + double[] coords; + + + +}