From d6742b7483d225ea847810b02e99a43219aa8447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Manelphe?= Date: Tue, 23 Jan 2024 11:18:01 +0100 Subject: [PATCH] fix: nb char extraction without reading full line --- .../operation/ChargementBrut.java | 38 ++++++++++++++----- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/arc-core/src/main/java/fr/insee/arc/core/service/p2chargement/operation/ChargementBrut.java b/arc-core/src/main/java/fr/insee/arc/core/service/p2chargement/operation/ChargementBrut.java index 64fea20b2..9e7500cf8 100644 --- a/arc-core/src/main/java/fr/insee/arc/core/service/p2chargement/operation/ChargementBrut.java +++ b/arc-core/src/main/java/fr/insee/arc/core/service/p2chargement/operation/ChargementBrut.java @@ -49,16 +49,7 @@ private String requeteFichierBrutalement(String idSource, BufferedReader br, int StringBuilder requete=new StringBuilder(); int idLigne = nbBoucle * LIMIT_CHARGEMENT_BRUTAL_NB_LIGNE; - String line; - try { - line = br.readLine(); - line = line.substring(0, Math.min(line.length(), LIMIT_CHARGEMENT_BRUTAL_NB_CHAR)); - } catch (IOException e) { - throw new ArcException(e, ArcExceptionMessage.FILE_READ_FAILED, idSource); - } - if (line == null) { - throw new ArcException(ArcExceptionMessage.FILE_IS_EMPTY, idSource); - } + String line = readLineBrutal(idSource, br); boolean start=true; while (line != null && idLigne < (nbBoucle + 1) * LIMIT_CHARGEMENT_BRUTAL_NB_LIGNE) { if (start) @@ -83,6 +74,33 @@ private String requeteFichierBrutalement(String idSource, BufferedReader br, int return requete.toString(); } + + /** + * Retourne la ligne lue dans la limite du nombre de caractères + * @param idSource du fichier chargé + * @param br reader ouvert sur le fichier + * @return + * @throws ArcException + */ + private String readLineBrutal(String idSource, BufferedReader br) throws ArcException { + char[] buffer = new char[LIMIT_CHARGEMENT_BRUTAL_NB_CHAR]; + int size = 0; + int offset = 0; + try { + while (size != -1 && offset < LIMIT_CHARGEMENT_BRUTAL_NB_CHAR) { + size = br.read(buffer, offset, LIMIT_CHARGEMENT_BRUTAL_NB_CHAR - offset); + offset += size; + } + } catch (IOException e) { + throw new ArcException(e, ArcExceptionMessage.FILE_READ_FAILED, idSource); + } + if (size == 0) { + throw new ArcException(ArcExceptionMessage.FILE_IS_EMPTY, idSource); + } + + String line = new String(buffer, 0, size); + return line; + } /** Calcule la norme. Retourne (par référence) la norme dans normeOk[0] et la validité dans validiteOk[0]. * @throws IOException