From 556002e8d18f0dd0c6d913909136c40b0db36b0c Mon Sep 17 00:00:00 2001 From: Hangga Aji Sayekti Date: Tue, 19 Sep 2023 19:53:41 +0700 Subject: [PATCH] EOF Detection | bazeniancode@gmail.com | Hangga Aji Sayekti (#14738) * [Add] add from my experiment repo github.com/hangga/eof-detection * [Update] reorder test * [Update] add close() * [Update] try with resource * [update] be careful with typo --- .../baeldung/eofdetections/EOFDetection.java | 60 ++++++++++++++ .../eofdetections/EOFDetectionUnitTest.java | 80 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/eofdetections/EOFDetection.java create mode 100644 core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/eofdetections/EOFDetectionUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/eofdetections/EOFDetection.java b/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/eofdetections/EOFDetection.java new file mode 100644 index 0000000000..c6302a3fc9 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/main/java/com/baeldung/eofdetections/EOFDetection.java @@ -0,0 +1,60 @@ +package com.baeldung.eofdetections; + +import java.io.*; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public class EOFDetection { + + public String readWithFileInputStream(String pathFile) throws IOException { + try (FileInputStream fis = new FileInputStream(pathFile); + ByteArrayOutputStream baos = new ByteArrayOutputStream()) { + int data; + while ((data = fis.read()) != -1) { + baos.write(data); + } + return baos.toString(); + } + } + + public String readWithBufferedReader(String pathFile) throws IOException { + try (FileInputStream fis = new FileInputStream(pathFile); + InputStreamReader isr = new InputStreamReader(fis); + BufferedReader reader = new BufferedReader(isr)) { + StringBuilder actualContent = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + actualContent.append(line); + } + return actualContent.toString(); + } + } + + public String readWithScanner(String pathFile) throws IOException { + File file = new File(pathFile); + Scanner scanner = new Scanner(file); + StringBuilder actualContent = new StringBuilder(); + while (scanner.hasNext()) { + String line = scanner.nextLine(); + actualContent.append(line); + } + scanner.close(); + return actualContent.toString(); + } + + public String readFileWithFileChannelAndByteBuffer(String pathFile) throws IOException { + try (FileInputStream fis = new FileInputStream(pathFile); + FileChannel channel = fis.getChannel()) { + + ByteBuffer buffer = ByteBuffer.allocate((int) channel.size()); + while (channel.read(buffer) != -1) { + buffer.flip(); + buffer.clear(); + } + return StandardCharsets.UTF_8.decode(buffer).toString(); + } + } +} + diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/eofdetections/EOFDetectionUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/eofdetections/EOFDetectionUnitTest.java new file mode 100644 index 0000000000..525b242bf9 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/eofdetections/EOFDetectionUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.eofdetections; + +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class EOFDetectionUnitTest { + + static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"; + String pathToFile = "sample.txt"; // init sample file path + EOFDetection eofDetection = new EOFDetection(); + @Test + @Order(1) + public void givenDummyText_whenReadWithFileInputStream_returnText() { + try { + String actualText = eofDetection.readWithFileInputStream(pathToFile); + assertEquals(LOREM_IPSUM, actualText); + } catch (IOException e) { + fail(e.getMessage()); + } + } + + @Test + @Order(2) + public void givenDummyText_whenReadFileWithBufferedReader_returnText() { + try { + String actualText = eofDetection.readWithBufferedReader(pathToFile); + assertEquals(LOREM_IPSUM, actualText); + } catch (IOException e) { + fail(e.getMessage()); + } + } + + @Test + @Order(3) + public void givenDummyText_whenReadFileWithScanner_returnText() { + try { + String actualText = eofDetection.readWithScanner(pathToFile); + assertEquals(LOREM_IPSUM, actualText); + } catch (IOException e) { + fail(e.getMessage()); + } + } + + @Test + @Order(4) + public void givenDummyText_whenReadFileWithFileChannelAndByteBuffer_returnText() { + try { + String actualText = eofDetection.readFileWithFileChannelAndByteBuffer(pathToFile); + assertEquals(LOREM_IPSUM, actualText); + } catch (IOException e) { + fail(e.getMessage()); + } + } + @Test + @Order(0) + public void prepareFileForTest() { + File file = new File(pathToFile); + + if (!file.exists()) { + try { + file.createNewFile(); + FileWriter writer = new FileWriter(file); + writer.write(LOREM_IPSUM); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } +}