Skip to content

Commit 51be799

Browse files
committed
[BAEL-1951] Guide to FileChannel
1 parent d05ce3a commit 51be799

3 files changed

Lines changed: 183 additions & 0 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
package com.baeldung.filechannel;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.io.RandomAccessFile;
10+
import java.nio.ByteBuffer;
11+
import java.nio.channels.FileChannel;
12+
13+
import org.junit.Test;
14+
15+
public class FileChannelUnitTest {
16+
@Test
17+
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
18+
String expected_value = "Hello world";
19+
String file = "src/test/resources/test_read.in";
20+
RandomAccessFile reader = new RandomAccessFile(file, "r");
21+
FileChannel channel = reader.getChannel();
22+
ByteArrayOutputStream out = new ByteArrayOutputStream();
23+
24+
int bufferSize = 1024;
25+
if (bufferSize > channel.size()) {
26+
bufferSize = (int) channel.size();
27+
}
28+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
29+
int noOfBytesRead = channel.read(buff);
30+
31+
while (noOfBytesRead != -1) {
32+
if (buff.hasArray()) {
33+
out.write(buff.array(), 0, buff.position());
34+
buff.clear();
35+
noOfBytesRead = channel.read(buff);
36+
}
37+
}
38+
39+
assertEquals(expected_value, out.toString());
40+
out.close();
41+
channel.close();
42+
reader.close();
43+
}
44+
45+
@Test
46+
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
47+
String expected_value = "Hello world";
48+
String file = "src/test/resources/test_read.in";
49+
ByteArrayOutputStream out = new ByteArrayOutputStream();
50+
FileInputStream fin = new FileInputStream(file);
51+
FileChannel channel = fin.getChannel();
52+
53+
int bufferSize = 1024;
54+
if (bufferSize > channel.size()) {
55+
bufferSize = (int) channel.size();
56+
}
57+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
58+
int noOfBytesRead = channel.read(buff);
59+
60+
while (noOfBytesRead != -1) {
61+
if (buff.hasArray()) {
62+
out.write(buff.array(), 0, buff.position());
63+
buff.clear();
64+
noOfBytesRead = channel.read(buff);
65+
}
66+
}
67+
68+
assertEquals(expected_value, out.toString());
69+
channel.close();
70+
fin.close();
71+
}
72+
73+
@Test
74+
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
75+
String input = "Hello world";
76+
String file = "src/test/resources/test_write_using_filechannel.txt";
77+
78+
RandomAccessFile writer = new RandomAccessFile(file, "rw");
79+
FileChannel channel = writer.getChannel();
80+
81+
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
82+
while (buff.hasRemaining()) {
83+
channel.write(buff);
84+
}
85+
channel.close();
86+
writer.close();
87+
88+
// verify
89+
RandomAccessFile reader = new RandomAccessFile(file, "r");
90+
assertEquals(input, reader.readLine());
91+
reader.close();
92+
}
93+
94+
@Test
95+
public void whenWriteWithFileChannelUsingFileOutputStream_thenCorrect() throws IOException {
96+
String input = "Hello world";
97+
String file = "src/test/resources/test_write_using_filechannel.txt";
98+
99+
FileOutputStream fout = new FileOutputStream(file);
100+
FileChannel channel = fout.getChannel();
101+
102+
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
103+
while (buff.hasRemaining()) {
104+
channel.write(buff);
105+
}
106+
107+
channel.close();
108+
fout.close();
109+
110+
// verify
111+
RandomAccessFile reader = new RandomAccessFile(file, "r");
112+
assertEquals(input, reader.readLine());
113+
reader.close();
114+
}
115+
116+
@Test
117+
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
118+
long expected_value = 11;
119+
String file = "src/test/resources/test_read.in";
120+
ByteArrayOutputStream out = new ByteArrayOutputStream();
121+
RandomAccessFile reader = new RandomAccessFile(file, "r");
122+
FileChannel channel = reader.getChannel();
123+
124+
int bufferSize = 1024;
125+
if (bufferSize > channel.size()) {
126+
bufferSize = (int) channel.size();
127+
}
128+
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
129+
int noOfBytesRead = channel.read(buff);
130+
131+
while (noOfBytesRead != -1) {
132+
if (buff.hasArray()) {
133+
out.write(buff.array(), 0, buff.position());
134+
buff.clear();
135+
noOfBytesRead = channel.read(buff);
136+
}
137+
}
138+
139+
assertEquals(expected_value, channel.position());
140+
141+
channel.position(4);
142+
assertEquals(4, channel.position());
143+
144+
channel.close();
145+
reader.close();
146+
}
147+
148+
@Test
149+
public void whenGetFileSize_thenCorrect() throws IOException {
150+
long expectedSize = 11;
151+
String file = "src/test/resources/test_read.in";
152+
RandomAccessFile reader = new RandomAccessFile(file, "r");
153+
FileChannel channel = reader.getChannel();
154+
155+
long imageFileSize = channel.size();
156+
assertEquals(expectedSize, imageFileSize);
157+
158+
channel.close();
159+
reader.close();
160+
}
161+
162+
@Test
163+
public void whenTruncateFile_thenCorrect() throws IOException {
164+
long expectedSize = 5;
165+
String input = "this is a test input";
166+
String file = "src/test/resources/test_truncate.txt";
167+
168+
FileOutputStream fout = new FileOutputStream(file);
169+
FileChannel channel = fout.getChannel();
170+
171+
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
172+
channel.write(buff);
173+
buff.flip();
174+
175+
channel = channel.truncate(5);
176+
assertEquals(expectedSize, channel.size());
177+
178+
fout.close();
179+
channel.close();
180+
}
181+
}
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

0 commit comments

Comments
 (0)