From 70f6c9fb028faba2c55ff2a870e7408c9a9aa063 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 18 Jul 2024 19:23:17 +0200 Subject: [PATCH] Refactored readFirstLine methods. --- .../devinsy/statoolinfos/util/FilesUtils.java | 107 ++++++++++++++++++ .../statoolinfos/util/LineIterator.java | 51 +++------ 2 files changed, 120 insertions(+), 38 deletions(-) diff --git a/src/fr/devinsy/statoolinfos/util/FilesUtils.java b/src/fr/devinsy/statoolinfos/util/FilesUtils.java index cac5fd7..a72eaf5 100644 --- a/src/fr/devinsy/statoolinfos/util/FilesUtils.java +++ b/src/fr/devinsy/statoolinfos/util/FilesUtils.java @@ -19,6 +19,7 @@ package fr.devinsy.statoolinfos.util; import java.io.File; +import java.io.IOException; import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; @@ -76,6 +77,112 @@ public class FilesUtils return result; } + /** + * Read first line. + * + * @param file + * the file + * @return the string + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static String readFirstLine(final File file) throws IOException + { + String result; + + result = readFirstLine(new Files(file)); + + // + return result; + } + + /** + * Read first line. + * + * @param files + * the file + * @return the string + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static String readFirstLine(final Files files) throws IOException + { + String result; + + FilesLineIterator iterator = null; + try + { + iterator = new FilesLineIterator(files); + if (iterator.hasNext()) + { + result = iterator.next(); + } + else + { + result = ""; + } + } + finally + { + if (iterator != null) + { + iterator.close(); + } + } + + // + return result; + } + + /** + * Read first line not blank. + * + * @param files + * the files + * @return the string + * @throws IOException + * Signals that an I/O exception has occurred. + */ + public static String readFirstLineNotBlank(final Files files) throws IOException + { + String result; + + FilesLineIterator iterator = null; + result = null; + try + { + iterator = new FilesLineIterator(files); + + boolean ended = false; + while (!ended) + { + if (iterator.hasNext()) + { + result = iterator.next(); + + if (!StringUtils.isBlank(result)) + { + ended = true; + } + } + else + { + result = null; + } + } + } + finally + { + if (iterator != null) + { + iterator.close(); + } + } + + // + return result; + } + /** * Search recursively. * diff --git a/src/fr/devinsy/statoolinfos/util/LineIterator.java b/src/fr/devinsy/statoolinfos/util/LineIterator.java index 7b5f7ae..3cc4496 100644 --- a/src/fr/devinsy/statoolinfos/util/LineIterator.java +++ b/src/fr/devinsy/statoolinfos/util/LineIterator.java @@ -37,6 +37,7 @@ public class LineIterator { private static Logger logger = LoggerFactory.getLogger(LineIterator.class); + private File file; private BufferedReader in; private String nextLine; private boolean ready; @@ -51,6 +52,8 @@ public class LineIterator */ public LineIterator(final File source) throws IOException { + this.file = source; + if (source.getName().endsWith(".gz")) { this.in = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(source)))); @@ -72,6 +75,16 @@ public class LineIterator IOUtils.closeQuietly(this.in); } + /** + * Gets the file. + * + * @return the file + */ + public File getFile() + { + return this.file; + } + /** * Checks for next. * @@ -130,42 +143,4 @@ public class LineIterator this.ready = true; } } - - /** - * Read first line. - * - * @param file - * the file - * @return the string - * @throws IOException - * Signals that an I/O exception has occurred. - */ - public static String readFirstLine(final File file) throws IOException - { - String result; - - LineIterator iterator = null; - try - { - iterator = new LineIterator(file); - if (iterator.hasNext()) - { - result = iterator.next(); - } - else - { - result = ""; - } - } - finally - { - if (iterator != null) - { - iterator.close(); - } - } - - // - return result; - } }