From 24468842b090dad3dcc475a9004191304c3929ef Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 11:40:00 +0100 Subject: [PATCH 01/10] Fixed UpperCaseFileWriter, Test Passed --- .../impl/filters/UpperCaseFilterWriter.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java index 0f41a5d..5d9bc68 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java @@ -6,7 +6,7 @@ /** * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public class UpperCaseFilterWriter extends FilterWriter { @@ -16,17 +16,25 @@ public UpperCaseFilterWriter(Writer wrappedWriter) { @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + super.write(str.toUpperCase(), off, len); } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + for (int i = off; i < len + off; i++) { + cbuf[i] = Character.toUpperCase(cbuf[i]); + } + + super.write(cbuf, off, len); } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + super.write(Character.toUpperCase(c)); + } -} +} \ No newline at end of file From 869eed49dc8d66cf74aa3d611d5fd72250a5802a Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 12:48:34 +0100 Subject: [PATCH 02/10] Fixed FileNumberingFilterWriter --- .../filters/FileNumberingFilterWriter.java | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java index 976c946..ca00891 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java @@ -18,6 +18,9 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); + private boolean isFirstChar = true; + private int lineCounter = 0; + private char lastChar = '\0'; // Used to work on mac and windows public FileNumberingFilterWriter(Writer out) { super(out); @@ -25,17 +28,45 @@ public FileNumberingFilterWriter(Writer out) { @Override public void write(String str, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); +// throw new UnsupportedOperationException("The student has not implemented this method yet."); + + for (int i = off; i < off + len; i++) { + write(str.charAt(i)); + } } @Override public void write(char[] cbuf, int off, int len) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); +// throw new UnsupportedOperationException("The student has not implemented this method yet."); + + for (int i = off; i < off + len; i++) { + write(cbuf[i]); + } } @Override public void write(int c) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + if (isFirstChar) { + + out.write(Integer.toString(++lineCounter) + '\t'); + isFirstChar = false; + } + + // Windows has \r\n, Linux \n, Mac \r + // Function adds newline only with \n. Make sure to add a new line if \r appears without the \n. + if (lastChar == '\r' && c != '\n') { + + out.write(Integer.toString(++lineCounter) + '\t'); + } + + out.write((char)c); + + if (c == '\n') { + + out.write(Integer.toString(++lineCounter) + '\t'); + } + + lastChar = (char) c; + } } From a7706a9797118635b8fdec9d743ec27d7ce2ecbc Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 12:49:12 +0100 Subject: [PATCH 03/10] Changed UpperCase out write to match other filter --- .../res/labio/impl/filters/UpperCaseFilterWriter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java index 5d9bc68..bf9a442 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java @@ -17,7 +17,7 @@ public UpperCaseFilterWriter(Writer wrappedWriter) { @Override public void write(String str, int off, int len) throws IOException { - super.write(str.toUpperCase(), off, len); + out.write(str.toUpperCase(), off, len); } @Override @@ -27,13 +27,13 @@ public void write(char[] cbuf, int off, int len) throws IOException { cbuf[i] = Character.toUpperCase(cbuf[i]); } - super.write(cbuf, off, len); + out.write(cbuf, off, len); } @Override public void write(int c) throws IOException { - super.write(Character.toUpperCase(c)); + out.write(Character.toUpperCase(c)); } From de43974f4b4020c8774432e88bc1cca3fef37057 Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 13:23:37 +0100 Subject: [PATCH 04/10] Minor changes - Alignement and comments on filters --- .../res/labio/impl/filters/FileNumberingFilterWriter.java | 6 +++++- .../res/labio/impl/filters/UpperCaseFilterWriter.java | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java index ca00891..fa84fa3 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java @@ -13,7 +13,7 @@ * * Hello\n\World -> 1\Hello\n2\tWorld * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public class FileNumberingFilterWriter extends FilterWriter { @@ -47,6 +47,7 @@ public void write(char[] cbuf, int off, int len) throws IOException { @Override public void write(int c) throws IOException { + // Adds tab and numbers if it's the first char if (isFirstChar) { out.write(Integer.toString(++lineCounter) + '\t'); @@ -60,13 +61,16 @@ public void write(int c) throws IOException { out.write(Integer.toString(++lineCounter) + '\t'); } + // Writes current char out.write((char)c); + // Creates new line if \n found if (c == '\n') { out.write(Integer.toString(++lineCounter) + '\t'); } + // Used to make it work on windows, mac and linux lastChar = (char) c; } } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java index bf9a442..306c315 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/UpperCaseFilterWriter.java @@ -33,7 +33,7 @@ public void write(char[] cbuf, int off, int len) throws IOException { @Override public void write(int c) throws IOException { - out.write(Character.toUpperCase(c)); + out.write(Character.toUpperCase(c)); } From 47fdbe97a8b6ded6bb0167d4bd4682e7ad275913 Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 13:24:25 +0100 Subject: [PATCH 05/10] DFS File explorer passing all tests --- .../labio/impl/explorers/DFSFileExplorer.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java index 83f8e61..9509b00 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java @@ -3,6 +3,7 @@ import ch.heigvd.res.labio.interfaces.IFileExplorer; import ch.heigvd.res.labio.interfaces.IFileVisitor; import java.io.File; +import java.util.Arrays; /** * This implementation of the IFileExplorer interface performs a depth-first @@ -10,13 +11,36 @@ * node (file and directory). When the explorer reaches a directory, it visits all * files in the directory and then moves into the subdirectories. * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public class DFSFileExplorer implements IFileExplorer { @Override - public void explore(File rootDirectory, IFileVisitor vistor) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + public void explore(File rootDirectory, IFileVisitor visitor) { + + visitor.visit(rootDirectory); + + // No need to do anything more if it's not a directory + if (!rootDirectory.isDirectory()) { + + return; + } + + File[] files = rootDirectory.listFiles(); + // No need to do anything more if directory is empty + if (files != null) { + + // Test sometimes fails without sorting + Arrays.sort(files); + + for (File f : files) { + + if (f.isDirectory()) { + + explore(f, visitor); + } + } + } + } } From 9eca47093e5a08777f911bb8f45926738ce9742c Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 14:06:35 +0100 Subject: [PATCH 06/10] Fixed Utils, passed all tests --- .../java/ch/heigvd/res/labio/impl/Utils.java | 59 ++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java index c8a3a5a..beb9745 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java @@ -4,7 +4,7 @@ /** * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public class Utils { @@ -20,7 +20,62 @@ public class Utils { * contain any line separator, then the first element is an empty string. */ public static String[] getNextLine(String lines) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + String[] result = new String[2]; + + // Get position of first separator occurence in lines. Returns -1 if not found + + int posLinuxSeparator = lines.indexOf("\n"); + int posMacSeparator = lines.indexOf("\r"); + int posWindowsSeparator = lines.indexOf("\r\n"); + + // On windows + if (posWindowsSeparator != -1) { + + result[0] = lines.substring(0, posWindowsSeparator + 2); // +2 because delimiter length is 2 + + if (posWindowsSeparator + 3 <= lines.length()) { + result[1] = lines.substring(posWindowsSeparator + 2); // Takes the rest of the string + } + else { + result[1] = ""; + } + } + + // On Linux + else if (posLinuxSeparator != -1) { + + result[0] = lines.substring(0, posLinuxSeparator + 1); + + if (posLinuxSeparator + 2 <= lines.length()) { + result[1] = lines.substring(posLinuxSeparator + 1); + } + else { + result[1] = ""; + } + } + + // On macOS + else if (posMacSeparator != -1) { + + result[0] = lines.substring(0, posMacSeparator + 1); + + if (posMacSeparator + 2 <= lines.length()) { + result[1] = lines.substring(posMacSeparator + 1); + } + else { + result[1] = ""; + } + } + + // Nothing found returns empty string + else { + + result[0] = ""; + result[1] = lines; // Unchanged string is the remaining part + } + + return result; } } From d72ed6be65f23bd0f93dc12b2e6c1b4eb0a236ad Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 14:30:53 +0100 Subject: [PATCH 07/10] Fixed NoOperationFileTransformer, Passed all tests --- .../res/labio/impl/transformers/FileTransformer.java | 10 +++++++++- .../labio/impl/transformers/NoOpFileTransformer.java | 5 ++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java index 18e3f14..a055749 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/FileTransformer.java @@ -23,7 +23,7 @@ * The subclasses have to implement the decorateWithFilters method, which instantiates * a list of filters and decorates the output writer with them. * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public abstract class FileTransformer implements IFileVisitor { @@ -58,6 +58,14 @@ public void visit(File file) { * writer has been decorated by the concrete subclass!). You need to write a loop to read the * characters and write them to the writer. */ + + int value = -1; // If read fails, returns -1 + + // Writes values after each correct read + while (reader.ready() && (value = reader.read()) != -1) { + + writer.write(value); + } reader.close(); writer.flush(); diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java index 5971a30..211954b 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/NoOpFileTransformer.java @@ -7,20 +7,19 @@ * this class is passed to a file system explorer, it will simply duplicate * the content of the input file into the output file. * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public class NoOpFileTransformer extends FileTransformer { @Override public Writer decorateWithFilters(Writer writer) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); /* * The NoOpFileTransformer does not apply any transformation of the character stream * (no uppercase, no line number, etc.). So, we don't need to decorate the writer connected to * the output file at all. Just uncomment the following line and get rid of the UnsupportedOperationException and * you will be all set. */ - //return writer; + return writer; } } From 3be29fc7f67d75c79384ca98e0161c6d6a87a6c9 Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 14:33:59 +0100 Subject: [PATCH 08/10] Fixed Complete Transformer, Passed all tests --- .../impl/transformers/CompleteFileTransformer.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java index 4beca48..12830ce 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/transformers/CompleteFileTransformer.java @@ -1,5 +1,8 @@ package ch.heigvd.res.labio.impl.transformers; +import ch.heigvd.res.labio.impl.filters.FileNumberingFilterWriter; +import ch.heigvd.res.labio.impl.filters.UpperCaseFilterWriter; + import java.io.Writer; /** @@ -9,22 +12,20 @@ * generate an output file with 1) uppercase letters and 2) line numbers at the * beginning of each line. * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public class CompleteFileTransformer extends FileTransformer { @Override public Writer decorateWithFilters(Writer writer) { - if (true) { - throw new UnsupportedOperationException("The student has not implemented this method yet."); - } + /* * If you uncomment the following line (and get rid of th 3 previous lines...), you will restore the decoration * of the writer (connected to the file. You can see that you first decorate the writer with an UpperCaseFilterWriter, which you then * decorate with a FileNumberingFilterWriter. The resulting writer is used by the abstract class to write the characters read from the * input files. So, the input is first prefixed with line numbers, then transformed to uppercase, then sent to the output file.f */ - //writer = new FileNumberingFilterWriter(new UpperCaseFilterWriter(writer)); + writer = new FileNumberingFilterWriter(new UpperCaseFilterWriter(writer)); return writer; } From a1447296cfe43e035e8b987ca9b36ced864ed072 Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 16:12:19 +0100 Subject: [PATCH 09/10] Fixed Application, All tests passed --- .../ch/heigvd/res/labio/impl/Application.java | 49 ++++++++++++++++--- .../labio/impl/explorers/DFSFileExplorer.java | 5 +- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java index ad87a7d..c450f06 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Application.java @@ -7,17 +7,16 @@ import ch.heigvd.res.labio.interfaces.IFileVisitor; import ch.heigvd.res.labio.quotes.QuoteClient; import ch.heigvd.res.labio.quotes.Quote; -import java.io.File; -import java.io.IOException; -import java.io.StringWriter; -import java.io.Writer; + +import java.io.*; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.FileUtils; /** * - * @author Olivier Liechti + * @author Olivier Liechti - Modified by Nicolas Müller on 07.03.2020 */ public class Application implements IApplication { @@ -80,9 +79,12 @@ public static void main(String[] args) { @Override public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { + clearOutputDirectory(); QuoteClient client = new QuoteClient(); + for (int i = 0; i < numberOfQuotes; i++) { + Quote quote = client.fetchQuote(); /* There is a missing piece here! * As you can see, this method handles the first part of the lab. It uses the web service @@ -90,7 +92,11 @@ public void fetchAndStoreQuotes(int numberOfQuotes) throws IOException { * one method provided by this class, which is responsible for storing the content of the * quote in a text file (and for generating the directories based on the tags). */ + + storeQuote(quote, "quote-" + (i + 1) + ".utf8"); + LOG.info("Received a new joke with " + quote.getTags().size() + " tags."); + for (String tag : quote.getTags()) { LOG.info("> " + tag); } @@ -123,7 +129,31 @@ void clearOutputDirectory() throws IOException { * @throws IOException */ void storeQuote(Quote quote, String filename) throws IOException { - throw new UnsupportedOperationException("The student has not implemented this method yet."); + + StringBuilder path = new StringBuilder(WORKSPACE_DIRECTORY + '/'); + + List tags = quote.getTags(); + + // Generating path + for (String tag : tags) { + + path.append(tag); + path.append('/'); + } + + File newQuote = new File(path.toString() + filename); + + // Creating folders on path to quote + newQuote.getParentFile().mkdirs(); + + + FileWriter fileWriter; + + // Throws IOEXception if somthing goes wrong + + fileWriter = new FileWriter(newQuote); + fileWriter.write(quote.getQuote()); + fileWriter.close(); } /** @@ -140,6 +170,13 @@ public void visit(File file) { * of the the IFileVisitor interface inline. You just have to add the body of the visit method, which should * be pretty easy (we want to write the filename, including the path, to the writer passed in argument). */ + try { + + writer.write(file.getPath() + '\n'); // '\n' to match expected test results for Linux + } + catch (IOException e) { + e.printStackTrace(); + } } }); } diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java index 9509b00..81d8f38 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/explorers/DFSFileExplorer.java @@ -34,12 +34,15 @@ public void explore(File rootDirectory, IFileVisitor visitor) { // Test sometimes fails without sorting Arrays.sort(files); + // Explore more if it's a directory or visit it if it's a file for (File f : files) { if (f.isDirectory()) { - explore(f, visitor); } + else { + visitor.visit(f); + } } } } From 355e79017a994918ff055baafe350b01f5f5d9fe Mon Sep 17 00:00:00 2001 From: Nic0Mueller Date: Sat, 7 Mar 2020 16:55:28 +0100 Subject: [PATCH 10/10] Cleaned FileNumberingFilter and Utils --- .../java/ch/heigvd/res/labio/impl/Utils.java | 48 ++++--------------- .../filters/FileNumberingFilterWriter.java | 6 +-- 2 files changed, 13 insertions(+), 41 deletions(-) diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java index beb9745..895cf2d 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/Utils.java @@ -29,53 +29,25 @@ public static String[] getNextLine(String lines) { int posMacSeparator = lines.indexOf("\r"); int posWindowsSeparator = lines.indexOf("\r\n"); - // On windows - if (posWindowsSeparator != -1) { - - result[0] = lines.substring(0, posWindowsSeparator + 2); // +2 because delimiter length is 2 - - if (posWindowsSeparator + 3 <= lines.length()) { - result[1] = lines.substring(posWindowsSeparator + 2); // Takes the rest of the string - } - else { - result[1] = ""; - } - } - - // On Linux - else if (posLinuxSeparator != -1) { + int splitPos = -1; - result[0] = lines.substring(0, posLinuxSeparator + 1); + if (posWindowsSeparator != -1) { - if (posLinuxSeparator + 2 <= lines.length()) { - result[1] = lines.substring(posLinuxSeparator + 1); - } - else { - result[1] = ""; - } + splitPos = posWindowsSeparator + 2; } - - // On macOS else if (posMacSeparator != -1) { - result[0] = lines.substring(0, posMacSeparator + 1); - - if (posMacSeparator + 2 <= lines.length()) { - result[1] = lines.substring(posMacSeparator + 1); - } - else { - result[1] = ""; - } + splitPos = posMacSeparator + 1; } + else if (posLinuxSeparator != -1) { - // Nothing found returns empty string - else { - - result[0] = ""; - result[1] = lines; // Unchanged string is the remaining part + splitPos = posLinuxSeparator + 1; } + result[0] = splitPos != -1 ? lines.substring(0, splitPos) : ""; + result[1] = splitPos != -1 ? lines.substring(splitPos) : lines; + return result; } -} +} \ No newline at end of file diff --git a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java index fa84fa3..567232c 100644 --- a/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java +++ b/LabJavaIO/src/main/java/ch/heigvd/res/labio/impl/filters/FileNumberingFilterWriter.java @@ -1,5 +1,7 @@ package ch.heigvd.res.labio.impl.filters; +import ch.heigvd.res.labio.impl.Utils; + import java.io.FilterWriter; import java.io.IOException; import java.io.Writer; @@ -20,7 +22,7 @@ public class FileNumberingFilterWriter extends FilterWriter { private static final Logger LOG = Logger.getLogger(FileNumberingFilterWriter.class.getName()); private boolean isFirstChar = true; private int lineCounter = 0; - private char lastChar = '\0'; // Used to work on mac and windows + private char lastChar = '\0'; public FileNumberingFilterWriter(Writer out) { super(out); @@ -28,7 +30,6 @@ public FileNumberingFilterWriter(Writer out) { @Override public void write(String str, int off, int len) throws IOException { -// throw new UnsupportedOperationException("The student has not implemented this method yet."); for (int i = off; i < off + len; i++) { write(str.charAt(i)); @@ -37,7 +38,6 @@ public void write(String str, int off, int len) throws IOException { @Override public void write(char[] cbuf, int off, int len) throws IOException { -// throw new UnsupportedOperationException("The student has not implemented this method yet."); for (int i = off; i < off + len; i++) { write(cbuf[i]);