From 43ef14abd851fedade4f4347351c5e16ef95794e Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Mon, 20 Mar 2023 20:08:50 +0100 Subject: [PATCH] finishing static functions for isBinary of isText Signed-off-by: Anthony Debucquoy --- .../school_project/Parsers/BinaryParser.java | 79 ++++++++++++------- .../Parsers/FileParserFactory.java | 3 +- .../school_project/Parsers/TxtParser.java | 6 +- 3 files changed, 55 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/school_project/Parsers/BinaryParser.java b/app/src/main/java/school_project/Parsers/BinaryParser.java index 9a1a92b..6cab224 100644 --- a/app/src/main/java/school_project/Parsers/BinaryParser.java +++ b/app/src/main/java/school_project/Parsers/BinaryParser.java @@ -5,6 +5,7 @@ import school_project.Piece; import school_project.Utils.Bitwise; import java.io.*; +import java.nio.file.Files; import java.util.Arrays; public class BinaryParser implements FileParser { @@ -13,8 +14,26 @@ public class BinaryParser implements FileParser { * Check if the file is a binary file suited for parsing * @return true if it is a binary File */ - static boolean isBinaryFile(File file){ - // TODO: 3/18/23 + static boolean isBinaryFile(File file) throws IOException { + // TODO: 3/20/23 Need more testing + if (Files.probeContentType(file.toPath()) != null) + return false; + + FileInputStream r_file = new FileInputStream(file); + byte[] content = r_file.readAllBytes(); + + boolean header_found = false; + for (int i = 0; i < content.length; i++) { + if (!header_found) { + if (content[i] == 'S' && content[i + 1] == 'M' && content[i + 2] == 'S') { + header_found = true; + } + } else { + if (content[i] == 'S' && content[i + 1] == 'M' && content[i + 2] == 'E') { + return true; + } + } + } return false; } @@ -33,6 +52,34 @@ public class BinaryParser implements FileParser { return ret; } + /** + * Extract Level data from file content + * @param fileStream file stream to read extract data from + * @return Level data as an array of byte + * @throws IOException Expected if we can't read the file + */ + private static byte[] ExtractLevelData(FileInputStream fileStream) throws IOException { + + byte[] bytes = fileStream.readAllBytes(); + + int start_position = 0, end_position = 0; + for (int i = 0; i < bytes.length; i++) { + if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 83){ // SMS + start_position = i+3; + break; + } + } + + for (int i = start_position; i < bytes.length; i++) { + if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 69){ // SME + end_position = i; + break; + } + } + + return Arrays.copyOfRange(bytes, start_position, end_position); + } + /** * Get Pieces out of the level data * @param levelData full data of the level without header and footer @@ -64,34 +111,6 @@ public class BinaryParser implements FileParser { return BuildMatrixFromBytes(map_width, map_height, map_data); } - /** - * Extract Level data from file content - * @param fileStream file stream to read extract data from - * @return Level data as an array of byte - * @throws IOException Expected if we can't read the file - */ - private static byte[] ExtractLevelData(FileInputStream fileStream) throws IOException { - - byte[] bytes = fileStream.readAllBytes(); - - int start_position = 0, end_position = 0; - for (int i = 0; i < bytes.length; i++) { - if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 83){ // SMS - start_position = i+3; - break; - } - } - - for (int i = start_position; i < bytes.length; i++) { - if(bytes[i] == 83 && bytes[i+1] == 77 && bytes[i+2] == 69){ // SME - end_position = i; - break; - } - } - - return Arrays.copyOfRange(bytes, start_position, end_position); - } - /** * Build a boolean Matrix From a byte array * Each Byte is composed of 8 bit, each bit is 1 or 0 diff --git a/app/src/main/java/school_project/Parsers/FileParserFactory.java b/app/src/main/java/school_project/Parsers/FileParserFactory.java index 752af83..149ea97 100644 --- a/app/src/main/java/school_project/Parsers/FileParserFactory.java +++ b/app/src/main/java/school_project/Parsers/FileParserFactory.java @@ -1,6 +1,7 @@ package school_project.Parsers; import java.io.File; +import java.io.IOException; import java.io.NotSerializableException; /** @@ -14,7 +15,7 @@ import java.io.NotSerializableException; * @author tonitch */ public class FileParserFactory { - public static FileParser createParser(File file) throws NotSerializableException{ + public static FileParser createParser(File file) throws IOException { if(BinaryParser.isBinaryFile(file)) return new BinaryParser(); else if (TxtParser.isTextFile(file)) diff --git a/app/src/main/java/school_project/Parsers/TxtParser.java b/app/src/main/java/school_project/Parsers/TxtParser.java index 485e081..65683bc 100644 --- a/app/src/main/java/school_project/Parsers/TxtParser.java +++ b/app/src/main/java/school_project/Parsers/TxtParser.java @@ -3,14 +3,16 @@ 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){ - return false; + static boolean isTextFile(File file) throws IOException { + return Files.probeContentType(file.toPath()).equals("text/plain"); // TODO: 3/18/23 }