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