Skip to content

Commit

Permalink
ajout checkUtf8format
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelQuetin committed Nov 25, 2024
1 parent cd7975c commit 7b02fb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/fr/abes/kbart2kafka/service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import fr.abes.kbart2kafka.dto.LigneKbartDto;
import fr.abes.kbart2kafka.exception.IllegalDateException;
import fr.abes.kbart2kafka.exception.IllegalFileFormatException;
import fr.abes.kbart2kafka.utils.CheckFiles;
import fr.abes.kbart2kafka.utils.PUBLICATION_TYPE;
import fr.abes.kbart2kafka.utils.Utils;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -71,6 +72,7 @@ private void executeMultiThread(File fichier) throws IllegalFileFormatException
ThreadContext.put("package", fichier.getName() + ";" + cpt.get());
String[] tsvElementsOnOneLine = ligneKbart.split("\t");
try {
CheckFiles.isValidUtf8(ligneKbart);
kbartsToSend.add(mapper.writeValueAsString(constructDto(tsvElementsOnOneLine, cpt.get(), nbLignesFichier)));
} catch (IllegalDateException | IllegalFileFormatException | JsonProcessingException e) {
log.error("Erreur dans le fichier en entrée à la ligne " + cpt.get() + " : " + e.getMessage());
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/fr/abes/kbart2kafka/utils/CheckFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.StandardCharsets;

@Slf4j
public class CheckFiles {
Expand Down Expand Up @@ -94,4 +97,13 @@ public static void verifyFile(File file, String header) throws IllegalFileFormat
detectTabulations(file);
detectHeaderPresence(header, file, isBypassOptionPresent);
}

public static void isValidUtf8(String input) throws IllegalFileFormatException {
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
try {
decoder.decode(java.nio.ByteBuffer.wrap(input.getBytes(StandardCharsets.UTF_8)));
} catch (CharacterCodingException e) {
throw new IllegalFileFormatException("le fichier contient des caracters qui ne sont pas en UTF8");
}
}
}

0 comments on commit 7b02fb4

Please sign in to comment.