updated post review comments

This commit is contained in:
Anurag Goyal 2019-04-07 16:48:07 +05:30
parent 8054742acb
commit fe53a23d41

View File

@ -1,6 +1,7 @@
package com.baeldung.filechannel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
@ -8,19 +9,24 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public class FileChannelUnitTest {
@Test
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
String expected_value = "Hello world";
String file = "src/test/resources/test_read.in";
RandomAccessFile reader = new RandomAccessFile(file, "r");
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
FileChannel channel = reader.getChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
int bufferSize = 1024;
if (bufferSize > channel.size()) {
@ -36,18 +42,18 @@ public class FileChannelUnitTest {
}
assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8));
out.close();
channel.close();
reader.close();
}
}
@Test
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
String expected_value = "Hello world";
String file = "src/test/resources/test_read.in";
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
FileInputStream fin = new FileInputStream(file);
FileChannel channel = fin.getChannel();
FileChannel channel = fin.getChannel();) {
int bufferSize = 1024;
if (bufferSize > channel.size()) {
@ -63,61 +69,82 @@ public class FileChannelUnitTest {
}
assertEquals(expected_value, new String(out.toByteArray(), StandardCharsets.UTF_8));
out.close();
channel.close();
fin.close();
}
}
@Test
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
String input = "Hello world";
String file = "src/test/resources/test_write_using_filechannel.txt";
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
String expected_value = "world";
String file = "src/test/resources/test_read.in";
RandomAccessFile writer = new RandomAccessFile(file, "rw");
FileChannel channel = writer.getChannel();
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
FileChannel channel = reader.getChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);
if(buff.hasRemaining()) {
byte[] data = new byte[buff.remaining()];
buff.get(data);
assertEquals(expected_value, new String(data, StandardCharsets.UTF_8));
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
while (buff.hasRemaining()) {
channel.write(buff);
}
channel.close();
writer.close();
// verify
RandomAccessFile reader = new RandomAccessFile(file, "r");
assertEquals(input, reader.readLine());
reader.close();
}
}
@Test
public void whenWriteWithFileChannelUsingFileOutputStream_thenCorrect() throws IOException {
String input = "Hello world";
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect()
throws IOException {
String expected = "Hello world";
String file = "src/test/resources/test_write_using_filechannel.txt";
FileOutputStream fout = new FileOutputStream(file);
FileChannel channel = fout.getChannel();
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
FileChannel channel = writer.getChannel();){
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
while (buff.hasRemaining()) {
if (buff.hasRemaining()) {
channel.write(buff);
}
channel.close();
fout.close();
// verify
RandomAccessFile reader = new RandomAccessFile(file, "r");
assertEquals(input, reader.readLine());
assertEquals(expected, reader.readLine());
reader.close();
}
}
@Test
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
String file = "src/test/resources/test_read.in";
try (RandomAccessFile reader = new RandomAccessFile(file, "rw");
FileChannel channel = reader.getChannel();
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE );){
assertNotNull(fileLock);
}catch(OverlappingFileLockException | IOException ex) {
}
}
@Test
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
long expected_value = 11;
String file = "src/test/resources/test_read.in";
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
RandomAccessFile reader = new RandomAccessFile(file, "r");
FileChannel channel = reader.getChannel();
FileChannel channel = reader.getChannel();) {
int bufferSize = 1024;
if (bufferSize > channel.size()) {
@ -126,34 +153,31 @@ public class FileChannelUnitTest {
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
while (channel.read(buff) > 0) {
if (buff.hasArray()) {
out.write(buff.array(), 0, buff.position());
buff.clear();
}
}
assertEquals(expected_value, channel.position());
channel.position(4);
assertEquals(4, channel.position());
out.close();
channel.close();
reader.close();
}
}
@Test
public void whenGetFileSize_thenCorrect() throws IOException {
long expectedSize = 11;
String file = "src/test/resources/test_read.in";
RandomAccessFile reader = new RandomAccessFile(file, "r");
FileChannel channel = reader.getChannel();
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
FileChannel channel = reader.getChannel();) {
long imageFileSize = channel.size();
assertEquals(expectedSize, imageFileSize);
}
channel.close();
reader.close();
}
@Test