Skip to content

Commit 4930432

Browse files
anuraggoyal1ashleyfrieze
authored andcommitted
[BAEL-1951]create new module core-java-nio and adding java filechannel examples (eugenp#6899)
* create new module core-java-nio and adding filechannel * updated readme with NIO article details * fixed typo * Remore unused dependencies from pom.xml * fixed build issue * moving into core-java-modules folder * remove unused plugins from pom.xml * remove junk file and update readme * removing readme.md * change in indentation. remove tabs to spaces
1 parent 4a46d48 commit 4930432

8 files changed

Lines changed: 205 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0.*
2+
3+
# Files generated by integration tests
4+
# *.txt
5+
/temp
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>core-java-nio</artifactId>
6+
<version>0.1.0-SNAPSHOT</version>
7+
<name>core-java-nio</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-java</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<relativePath>../../parent-java</relativePath>
15+
</parent>
16+
</project>
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package com.baeldung.filechannel;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
6+
import java.io.ByteArrayOutputStream;
7+
import java.io.FileInputStream;
8+
import java.io.FileOutputStream;
9+
import java.io.IOException;
10+
import java.io.RandomAccessFile;
11+
import java.nio.ByteBuffer;
12+
import java.nio.MappedByteBuffer;
13+
import java.nio.channels.FileChannel;
14+
import java.nio.channels.FileLock;
15+
import java.nio.charset.StandardCharsets;
16+
17+
import org.junit.Test;
18+
19+
public class FileChannelUnitTest {
20+
21+
@Test
22+
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
23+
24+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
25+
FileChannel channel = reader.getChannel();
26+
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
27+
28+
int bufferSize = 1024;
29+
if (bufferSize > channel.size()) {
30+
bufferSize = (int) channel.size();
31+
}
32+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
33+
34+
while (channel.read(buff) > 0) {
35+
out.write(buff.array(), 0, buff.position());
36+
buff.clear();
37+
}
38+
39+
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
40+
41+
assertEquals("Hello world", fileContent);
42+
}
43+
}
44+
45+
@Test
46+
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
47+
48+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
49+
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
50+
FileChannel channel = fin.getChannel()) {
51+
52+
int bufferSize = 1024;
53+
if (bufferSize > channel.size()) {
54+
bufferSize = (int) channel.size();
55+
}
56+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
57+
58+
while (channel.read(buff) > 0) {
59+
out.write(buff.array(), 0, buff.position());
60+
buff.clear();
61+
}
62+
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
63+
64+
assertEquals("Hello world", fileContent);
65+
}
66+
}
67+
68+
@Test
69+
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
70+
71+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
72+
FileChannel channel = reader.getChannel();
73+
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
74+
75+
MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);
76+
77+
if (buff.hasRemaining()) {
78+
byte[] data = new byte[buff.remaining()];
79+
buff.get(data);
80+
assertEquals("world", new String(data, StandardCharsets.UTF_8));
81+
}
82+
}
83+
}
84+
85+
@Test
86+
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
87+
String file = "src/test/resources/test_write_using_filechannel.txt";
88+
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
89+
FileChannel channel = writer.getChannel()) {
90+
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
91+
92+
channel.write(buff);
93+
94+
// now we verify whether the file was written correctly
95+
RandomAccessFile reader = new RandomAccessFile(file, "r");
96+
assertEquals("Hello world", reader.readLine());
97+
reader.close();
98+
}
99+
}
100+
101+
@Test
102+
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
103+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw");
104+
FileChannel channel = reader.getChannel();
105+
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {
106+
107+
assertNotNull(fileLock);
108+
}
109+
}
110+
111+
@Test
112+
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
113+
114+
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
115+
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
116+
FileChannel channel = reader.getChannel()) {
117+
118+
int bufferSize = 1024;
119+
if (bufferSize > channel.size()) {
120+
bufferSize = (int) channel.size();
121+
}
122+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
123+
124+
while (channel.read(buff) > 0) {
125+
out.write(buff.array(), 0, buff.position());
126+
buff.clear();
127+
}
128+
129+
// the original file is 11 bytes long, so that's where the position pointer should be
130+
assertEquals(11, channel.position());
131+
132+
channel.position(4);
133+
assertEquals(4, channel.position());
134+
}
135+
}
136+
137+
@Test
138+
public void whenGetFileSize_thenCorrect() throws IOException {
139+
140+
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
141+
FileChannel channel = reader.getChannel()) {
142+
143+
// the original file is 11 bytes long, so that's where the position pointer should be
144+
assertEquals(11, channel.size());
145+
}
146+
}
147+
148+
@Test
149+
public void whenTruncateFile_thenCorrect() throws IOException {
150+
String input = "this is a test input";
151+
152+
FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
153+
FileChannel channel = fout.getChannel();
154+
155+
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
156+
channel.write(buff);
157+
buff.flip();
158+
159+
channel = channel.truncate(5);
160+
assertEquals(5, channel.size());
161+
162+
fout.close();
163+
channel.close();
164+
}
165+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.class
2+
3+
#folders#
4+
/target
5+
/neoDb*
6+
/data
7+
/src/main/webapp/WEB-INF/classes
8+
*/META-INF/*
9+
10+
# Packaged files #
11+
*.jar
12+
*.war
13+
*.ear
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@
392392
<module>core-java-modules/core-java-concurrency-basic</module>
393393
<module>core-java-modules/core-java-concurrency-collections</module>
394394
<module>core-java-modules/core-java-io</module>
395+
<module>core-java-modules/core-java-nio</module>
395396
<module>core-java-modules/core-java-security</module>
396397
<module>core-java-modules/core-java-lang-syntax</module>
397398
<module>core-java-modules/core-java-lang</module>
@@ -1074,6 +1075,7 @@
10741075
<module>core-java-modules/core-java-concurrency-basic</module>
10751076
<module>core-java-modules/core-java-concurrency-collections</module>
10761077
<module>core-java-modules/core-java-io</module>
1078+
<module>core-java-modules/core-java-nio</module>
10771079
<module>core-java-modules/core-java-security</module>
10781080
<module>core-java-modules/core-java-lang-syntax</module>
10791081
<module>core-java-modules/core-java-lang</module>
@@ -1560,4 +1562,4 @@
15601562
<lombok.version>1.16.12</lombok.version>
15611563
<h2.version>1.4.197</h2.version>
15621564
</properties>
1563-
</project>
1565+
</project>

0 commit comments

Comments
 (0)