finishing static functions for isBinary of isText

Signed-off-by: Anthony Debucquoy <debucquoy.anthony@gmail.com>
This commit is contained in:
Debucquoy Anthony 2023-03-20 20:08:50 +01:00
parent dfb297525b
commit 43ef14abd8
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
3 changed files with 55 additions and 33 deletions

View File

@ -5,6 +5,7 @@ import school_project.Piece;
import school_project.Utils.Bitwise; import school_project.Utils.Bitwise;
import java.io.*; import java.io.*;
import java.nio.file.Files;
import java.util.Arrays; import java.util.Arrays;
public class BinaryParser implements FileParser { 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 * 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){ static boolean isBinaryFile(File file) throws IOException {
// TODO: 3/18/23 // 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; return false;
} }
@ -33,6 +52,34 @@ public class BinaryParser implements FileParser {
return ret; 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 * Get Pieces out of the level data
* @param levelData full data of the level without header and footer * @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); 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 * Build a boolean Matrix From a byte array
* Each Byte is composed of 8 bit, each bit is 1 or 0 * Each Byte is composed of 8 bit, each bit is 1 or 0

View File

@ -1,6 +1,7 @@
package school_project.Parsers; package school_project.Parsers;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.NotSerializableException; import java.io.NotSerializableException;
/** /**
@ -14,7 +15,7 @@ import java.io.NotSerializableException;
* @author tonitch * @author tonitch
*/ */
public class FileParserFactory { public class FileParserFactory {
public static FileParser createParser(File file) throws NotSerializableException{ 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 (TxtParser.isTextFile(file))

View File

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