diff --git a/core-java-modules/core-java/pom.xml b/core-java-modules/core-java/pom.xml index bb19b525d0..786ee91192 100644 --- a/core-java-modules/core-java/pom.xml +++ b/core-java-modules/core-java/pom.xml @@ -59,6 +59,16 @@ spring-core ${spring.core.version} + + commons-io + commons-io + ${commons-io.version} + + + com.google.gdata + core + 1.47.1 + diff --git a/core-java-modules/core-java/src/test/java/com/baeldung/illegalcharacter/IllegalCharacterUnitTest.java b/core-java-modules/core-java/src/test/java/com/baeldung/illegalcharacter/IllegalCharacterUnitTest.java new file mode 100644 index 0000000000..4a08daa271 --- /dev/null +++ b/core-java-modules/core-java/src/test/java/com/baeldung/illegalcharacter/IllegalCharacterUnitTest.java @@ -0,0 +1,79 @@ +package com.baeldung.illegalcharacter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.util.Objects; + +import org.apache.commons.io.ByteOrderMark; +import org.apache.commons.io.input.BOMInputStream; +import org.junit.Test; + +import com.google.gdata.util.io.base.UnicodeReader; + +public class IllegalCharacterUnitTest { + + final String RESOURCE_FILE_NAME = "bom-file.txt"; + final InputStream ioStream = this.getClass() + .getClassLoader() + .getResourceAsStream(RESOURCE_FILE_NAME); + final String expected = "Hello world with BOM."; + + @Test + public void whenInputFileHasBOM_thenUseInputStream() throws IOException { + String line; + String actual = ""; + + try (BufferedReader br = new BufferedReader(new InputStreamReader(ioStream))) { + while ((line = br.readLine()) != null) { + actual += line; + } + } + + assertNotEquals(expected, actual); + } + + @Test + public void whenInputFileHasBOM_thenUseInputStreamWithReplace() throws IOException { + String line; + String actual = ""; + + try (BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(ioStream)))) { + while ((line = br.readLine()) != null) { + actual += line.replace("\uFEFF", ""); + } + } + + assertEquals(expected, actual); + } + + @Test + public void whenInputFileHasBOM_thenUseBOMInputStream() throws IOException { + String line; + String actual = ""; + + try (BufferedReader br = new BufferedReader(new InputStreamReader(new BOMInputStream(ioStream, false, ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE)))) { + while ((line = br.readLine()) != null) { + actual += line; + } + } + + assertEquals(expected, actual); + } + + @Test + public void whenInputFileHasBOM_thenUseGoogleGdata() throws IOException { + char[] actual = new char[21]; + + try (Reader r = new UnicodeReader(ioStream, null)) { + r.read(actual); + } + + assertEquals(expected, String.valueOf(actual)); + } +} diff --git a/core-java-modules/core-java/src/test/resources/bom-file.txt b/core-java-modules/core-java/src/test/resources/bom-file.txt new file mode 100644 index 0000000000..b2037fded6 --- /dev/null +++ b/core-java-modules/core-java/src/test/resources/bom-file.txt @@ -0,0 +1 @@ +Hello world with BOM. \ No newline at end of file