begining save data methods
Some checks failed
continuous-integration/drone/pr Build is failing
continuous-integration/drone/push Build is failing

Signed-off-by: Anthony Debucquoy <debucquoy.anthony@gmail.com>
This commit is contained in:
Debucquoy Anthony 2023-03-20 23:18:59 +01:00
parent 43ef14abd8
commit 29c2fd7a0b
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
5 changed files with 58 additions and 30 deletions

View File

@ -10,14 +10,19 @@ import java.util.Arrays;
public class BinaryParser implements FileParser { public class BinaryParser implements FileParser {
@Override
public void saveLevelData(File file, Map levelData) throws IOException {
// TODO: 3/20/23
}
/** /**
* Check if the file is a binary file suited for parsing * check if the file is a binary file suited for parsing
* @return true if it is a binary File * @return true if it is a binary File
*/ */
static boolean isBinaryFile(File file) throws IOException { public static boolean isBinaryFile(File file) throws IOException {
// TODO: 3/20/23 Need more testing // TODO: 3/20/23 Need more testing
if (Files.probeContentType(file.toPath()) != null) if (Files.probeContentType(file.toPath()) != null)
return false; return false ;
FileInputStream r_file = new FileInputStream(file); FileInputStream r_file = new FileInputStream(file);
byte[] content = r_file.readAllBytes(); byte[] content = r_file.readAllBytes();

View File

@ -18,6 +18,13 @@ public interface FileParser {
*/ */
Map getLevel(File file) throws IOException; Map getLevel(File file) throws IOException;
/**
* Save Map to a file with all it's data
* @param file the file where to save
* @param levelData the map to save
*/
void saveLevelData(File file, Map levelData) throws IOException;
// TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file // TODO: 3/18/23 tonitch: Add getSavedData, for when piece could be added to map file
} }

View File

@ -18,8 +18,8 @@ public class FileParserFactory {
public static FileParser createParser(File file) throws IOException { public static FileParser createParser(File file) throws IOException {
if(BinaryParser.isBinaryFile(file)) if(BinaryParser.isBinaryFile(file))
return new BinaryParser(); return new BinaryParser();
else if (TxtParser.isTextFile(file)) else if (JsonParser.isJsonFile(file))
return new TxtParser(); return new JsonParser();
else else
throw new NotSerializableException("This file format is not supported"); throw new NotSerializableException("This file format is not supported");
} }

View File

@ -0,0 +1,40 @@
package school_project.Parsers;
import school_project.Map;
import java.io.*;
import java.nio.file.Files;
public class JsonParser implements FileParser{
@Override
public void saveLevelData(File file, Map levelData) throws IOException {
FileOutputStream fileStream = new FileOutputStream(file);
ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
objectStream.writeObject(levelData);
objectStream.close();
fileStream.close();
}
/**
* Check if the file is a text file
* @return true if it is a text File
*/
public static boolean isJsonFile(File file) throws IOException {
System.out.println(Files.probeContentType(file.toPath()));
return Files.probeContentType(file.toPath()).equals("application/json");
}
@Override
public Map getLevel(File file) throws IOException {
FileInputStream fileStream = new FileInputStream(file);
ObjectInputStream objectStream = new ObjectInputStream(fileStream);
try {
return (Map) objectStream.readObject();
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -1,24 +0,0 @@
package school_project.Parsers;
import school_project.Map;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
public class TxtParser implements FileParser{
/**
* Check if the file is a text file
* @return true if it is a text File
*/
static boolean isTextFile(File file) throws IOException {
return Files.probeContentType(file.toPath()).equals("text/plain");
// TODO: 3/18/23
}
@Override
public Map getLevel(File file) {
return null;
// TODO: 3/18/23
}
}