diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/ISerialFileManager.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/ISerialFileManager.java new file mode 100644 index 00000000..66c65e08 --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/ISerialFileManager.java @@ -0,0 +1,12 @@ +package com.pqt.server.tools.io; + +import java.util.List; +import java.util.Set; + +//TODO écrire javadoc +public interface ISerialFileManager { + List loadListFromFile(); + Set loadSetFromFile(); + void saveListToFile(List list); + void saveSetToFile(Set set); +} diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/SimpleSerialFileManager.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/SimpleSerialFileManager.java new file mode 100644 index 00000000..619532c6 --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/SimpleSerialFileManager.java @@ -0,0 +1,108 @@ +package com.pqt.server.tools.io; + +import com.pqt.server.tools.FileUtil; + +import java.io.*; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; + +//TODO vérifier que le save écrase bien le contenu précédent du fichier +public class SimpleSerialFileManager implements ISerialFileManager { + + private Path filePath; + private Class clazz; + + SimpleSerialFileManager(String filePath, Class clazz){ + this(Paths.get(filePath), clazz); + } + + SimpleSerialFileManager(Path filePath, Class clazz){ + this.filePath = filePath; + this.clazz = clazz; + try{ + FileUtil.createFileIfNotExist(filePath); + }catch (IOException e){ + e.printStackTrace(); + } + } + + @Override + public List loadListFromFile() { + try{ + if(!FileUtil.createFileIfNotExist(filePath)){ + List loadedEntries = new ArrayList<>(); + fillCollection(loadedEntries); + return loadedEntries; + } + }catch(IOException | ClassNotFoundException e){ + e.printStackTrace(); + } + return null; + } + + @Override + public Set loadSetFromFile() { + try{ + if(!FileUtil.createFileIfNotExist(filePath)){ + Set loadedEntries = new HashSet<>(); + fillCollection(loadedEntries); + return loadedEntries; + } + }catch(IOException | ClassNotFoundException e){ + e.printStackTrace(); + } + return null; + } + + private void fillCollection(Collection collection) throws IOException, ClassNotFoundException { + if(collection==null) return; + try(FileInputStream fis = new FileInputStream(filePath.toString()); + ObjectInputStream ois = new ObjectInputStream(fis)){ + boolean end = false; + do{ + try{ + Object obj = ois.readObject(); + if(clazz.isInstance(obj)){ + T ae = clazz.cast(obj); + collection.add(ae); + } + }catch (EOFException e){ + end = true; + } + }while(!end); + } + } + + @Override + public void saveListToFile(List list) { + save(list); + } + + @Override + public void saveSetToFile(Set set) { + save(set); + } + + private void save(Collection collection){ + try{ + FileUtil.createFileIfNotExist(filePath); + }catch (IOException e){ + e.printStackTrace(); + return; + } + try(FileOutputStream fos = new FileOutputStream(filePath.toString()); + ObjectOutputStream oos = new ObjectOutputStream(fos)){ + + collection.forEach(p -> { + try { + oos.writeObject(p); + } catch (IOException e) { + e.printStackTrace(); + } + }); + }catch(IOException e){ + e.printStackTrace(); + } + } +} diff --git a/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/SimpleSerialFileManagerFactory.java b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/SimpleSerialFileManagerFactory.java new file mode 100644 index 00000000..2ebecb47 --- /dev/null +++ b/Workspace/server/src/main/WEB-INF/classes/com/pqt/server/tools/io/SimpleSerialFileManagerFactory.java @@ -0,0 +1,15 @@ +package com.pqt.server.tools.io; + +import java.nio.file.Path; + +public class SimpleSerialFileManagerFactory { + protected SimpleSerialFileManagerFactory(){} + + public static ISerialFileManager getFileManager(Class clazz, String filePath){ + return new SimpleSerialFileManager<>(filePath, clazz); + } + + public static ISerialFileManager getFileManager(Class clazz, Path filePath){ + return new SimpleSerialFileManager<>(filePath, clazz); + } +}