Skip to content

Commit 556002e

Browse files
authored
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
1 parent 5bcbd9d commit 556002e

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.eofdetections;
2+
3+
import java.io.*;
4+
import java.nio.ByteBuffer;
5+
import java.nio.channels.FileChannel;
6+
import java.nio.charset.StandardCharsets;
7+
import java.util.*;
8+
9+
public class EOFDetection {
10+
11+
public String readWithFileInputStream(String pathFile) throws IOException {
12+
try (FileInputStream fis = new FileInputStream(pathFile);
13+
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
14+
int data;
15+
while ((data = fis.read()) != -1) {
16+
baos.write(data);
17+
}
18+
return baos.toString();
19+
}
20+
}
21+
22+
public String readWithBufferedReader(String pathFile) throws IOException {
23+
try (FileInputStream fis = new FileInputStream(pathFile);
24+
InputStreamReader isr = new InputStreamReader(fis);
25+
BufferedReader reader = new BufferedReader(isr)) {
26+
StringBuilder actualContent = new StringBuilder();
27+
String line;
28+
while ((line = reader.readLine()) != null) {
29+
actualContent.append(line);
30+
}
31+
return actualContent.toString();
32+
}
33+
}
34+
35+
public String readWithScanner(String pathFile) throws IOException {
36+
File file = new File(pathFile);
37+
Scanner scanner = new Scanner(file);
38+
StringBuilder actualContent = new StringBuilder();
39+
while (scanner.hasNext()) {
40+
String line = scanner.nextLine();
41+
actualContent.append(line);
42+
}
43+
scanner.close();
44+
return actualContent.toString();
45+
}
46+
47+
public String readFileWithFileChannelAndByteBuffer(String pathFile) throws IOException {
48+
try (FileInputStream fis = new FileInputStream(pathFile);
49+
FileChannel channel = fis.getChannel()) {
50+
51+
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
52+
while (channel.read(buffer) != -1) {
53+
buffer.flip();
54+
buffer.clear();
55+
}
56+
return StandardCharsets.UTF_8.decode(buffer).toString();
57+
}
58+
}
59+
}
60+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.baeldung.eofdetections;
2+
3+
import org.junit.jupiter.api.MethodOrderer;
4+
import org.junit.jupiter.api.Order;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.TestMethodOrder;
7+
8+
import java.io.File;
9+
import java.io.FileWriter;
10+
import java.io.IOException;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
import static org.junit.jupiter.api.Assertions.fail;
14+
15+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
16+
public class EOFDetectionUnitTest {
17+
18+
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";
19+
String pathToFile = "sample.txt"; // init sample file path
20+
EOFDetection eofDetection = new EOFDetection();
21+
@Test
22+
@Order(1)
23+
public void givenDummyText_whenReadWithFileInputStream_returnText() {
24+
try {
25+
String actualText = eofDetection.readWithFileInputStream(pathToFile);
26+
assertEquals(LOREM_IPSUM, actualText);
27+
} catch (IOException e) {
28+
fail(e.getMessage());
29+
}
30+
}
31+
32+
@Test
33+
@Order(2)
34+
public void givenDummyText_whenReadFileWithBufferedReader_returnText() {
35+
try {
36+
String actualText = eofDetection.readWithBufferedReader(pathToFile);
37+
assertEquals(LOREM_IPSUM, actualText);
38+
} catch (IOException e) {
39+
fail(e.getMessage());
40+
}
41+
}
42+
43+
@Test
44+
@Order(3)
45+
public void givenDummyText_whenReadFileWithScanner_returnText() {
46+
try {
47+
String actualText = eofDetection.readWithScanner(pathToFile);
48+
assertEquals(LOREM_IPSUM, actualText);
49+
} catch (IOException e) {
50+
fail(e.getMessage());
51+
}
52+
}
53+
54+
@Test
55+
@Order(4)
56+
public void givenDummyText_whenReadFileWithFileChannelAndByteBuffer_returnText() {
57+
try {
58+
String actualText = eofDetection.readFileWithFileChannelAndByteBuffer(pathToFile);
59+
assertEquals(LOREM_IPSUM, actualText);
60+
} catch (IOException e) {
61+
fail(e.getMessage());
62+
}
63+
}
64+
@Test
65+
@Order(0)
66+
public void prepareFileForTest() {
67+
File file = new File(pathToFile);
68+
69+
if (!file.exists()) {
70+
try {
71+
file.createNewFile();
72+
FileWriter writer = new FileWriter(file);
73+
writer.write(LOREM_IPSUM);
74+
writer.close();
75+
} catch (IOException e) {
76+
e.printStackTrace();
77+
}
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)