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
This commit is contained in:
Hangga Aji Sayekti 2023-09-19 19:53:41 +07:00 committed by GitHub
parent 5bcbd9dc00
commit 556002e8d1
2 changed files with 140 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}
}