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