changes as per review comments
This commit is contained in:
parent
d9540eff75
commit
fece0dc4b8
|
@ -12,162 +12,154 @@ import java.nio.ByteBuffer;
|
||||||
import java.nio.MappedByteBuffer;
|
import java.nio.MappedByteBuffer;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.channels.FileLock;
|
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
|
@Test
|
||||||
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
|
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
|
||||||
|
|
||||||
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
||||||
FileChannel channel = reader.getChannel();
|
FileChannel channel = reader.getChannel();
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
|
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
|
||||||
|
|
||||||
int bufferSize = 1024;
|
int bufferSize = 1024;
|
||||||
if (bufferSize > channel.size()) {
|
if (bufferSize > channel.size()) {
|
||||||
bufferSize = (int) channel.size();
|
bufferSize = (int) channel.size();
|
||||||
}
|
}
|
||||||
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
|
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
|
||||||
|
|
||||||
while (channel.read(buff) > 0) {
|
while (channel.read(buff) > 0) {
|
||||||
out.write(buff.array(), 0, buff.position());
|
out.write(buff.array(), 0, buff.position());
|
||||||
buff.clear();
|
buff.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8));
|
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
assertEquals("Hello world", fileContent);
|
||||||
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
|
@Test
|
||||||
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
|
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
|
||||||
FileChannel channel = fin.getChannel();) {
|
|
||||||
|
|
||||||
int bufferSize = 1024;
|
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
if (bufferSize > channel.size()) {
|
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
|
||||||
bufferSize = (int) channel.size();
|
FileChannel channel = fin.getChannel();) {
|
||||||
}
|
|
||||||
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
|
|
||||||
|
|
||||||
while (channel.read(buff) > 0) {
|
int bufferSize = 1024;
|
||||||
out.write(buff.array(), 0, buff.position());
|
if (bufferSize > channel.size()) {
|
||||||
buff.clear();
|
bufferSize = (int) channel.size();
|
||||||
}
|
}
|
||||||
|
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
|
||||||
|
|
||||||
assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8));
|
while (channel.read(buff) > 0) {
|
||||||
|
out.write(buff.array(), 0, buff.position());
|
||||||
|
buff.clear();
|
||||||
|
}
|
||||||
|
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
|
||||||
|
|
||||||
}
|
assertEquals("Hello world", fileContent);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
|
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
|
||||||
|
|
||||||
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
||||||
FileChannel channel = reader.getChannel();
|
FileChannel channel = reader.getChannel();
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
|
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
|
||||||
|
|
||||||
MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);
|
MappedByteBuffer buff = channel.map(FileChannel.MapMode.READ_ONLY, 6, 5);
|
||||||
|
|
||||||
if (buff.hasRemaining()) {
|
if (buff.hasRemaining()) {
|
||||||
byte[] data = new byte[buff.remaining()];
|
byte[] data = new byte[buff.remaining()];
|
||||||
buff.get(data);
|
buff.get(data);
|
||||||
assertEquals("world", new String(data, StandardCharsets.UTF_8));
|
assertEquals("world", new String(data, StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
@Test
|
||||||
|
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
|
||||||
|
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));
|
||||||
|
|
||||||
}
|
channel.write(buff);
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
// now we verify whether the file was written correctly
|
||||||
public void whenWriteWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
|
RandomAccessFile reader = new RandomAccessFile(file, "r");
|
||||||
String file = "src/test/resources/test_write_using_filechannel.txt";
|
assertEquals("Hello world", reader.readLine());
|
||||||
try (RandomAccessFile writer = new RandomAccessFile(file, "rw");
|
reader.close();
|
||||||
FileChannel channel = writer.getChannel();) {
|
}
|
||||||
ByteBuffer buff = ByteBuffer.wrap("Hello world".getBytes(StandardCharsets.UTF_8));
|
}
|
||||||
|
|
||||||
if (buff.hasRemaining()) {
|
@Test
|
||||||
channel.write(buff);
|
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
|
||||||
}
|
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw");
|
||||||
|
FileChannel channel = reader.getChannel();
|
||||||
|
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {
|
||||||
|
|
||||||
// verify
|
assertNotNull(fileLock);
|
||||||
RandomAccessFile reader = new RandomAccessFile(file, "r");
|
}
|
||||||
assertEquals("Hello world", reader.readLine());
|
}
|
||||||
reader.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFile_whenWriteAFileUsingLockAFileSectionWithFileChannel_thenCorrect() throws IOException {
|
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
|
||||||
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "rw");
|
|
||||||
FileChannel channel = reader.getChannel();
|
|
||||||
FileLock fileLock = channel.tryLock(6, 5, Boolean.FALSE);) {
|
|
||||||
|
|
||||||
assertNotNull(fileLock);
|
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
||||||
|
FileChannel channel = reader.getChannel();) {
|
||||||
|
|
||||||
} catch (OverlappingFileLockException | IOException ex) {
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
// the original file is 11 bytes long, so that's where the position pointer should be
|
||||||
|
assertEquals(11, channel.position());
|
||||||
|
|
||||||
@Test
|
channel.position(4);
|
||||||
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
|
assertEquals(4, channel.position());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
|
@Test
|
||||||
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
public void whenGetFileSize_thenCorrect() throws IOException {
|
||||||
FileChannel channel = reader.getChannel();) {
|
|
||||||
|
|
||||||
int bufferSize = 1024;
|
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
||||||
if (bufferSize > channel.size()) {
|
FileChannel channel = reader.getChannel();) {
|
||||||
bufferSize = (int) channel.size();
|
|
||||||
}
|
|
||||||
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
|
|
||||||
|
|
||||||
while (channel.read(buff) > 0) {
|
// the original file is 11 bytes long, so that's where the position pointer should be
|
||||||
out.write(buff.array(), 0, buff.position());
|
assertEquals(11, channel.size());
|
||||||
buff.clear();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(11, channel.position());
|
@Test
|
||||||
|
public void whenTruncateFile_thenCorrect() throws IOException {
|
||||||
|
String input = "this is a test input";
|
||||||
|
|
||||||
channel.position(4);
|
FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
|
||||||
assertEquals(4, channel.position());
|
FileChannel channel = fout.getChannel();
|
||||||
|
|
||||||
}
|
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
|
||||||
}
|
channel.write(buff);
|
||||||
|
buff.flip();
|
||||||
|
|
||||||
@Test
|
channel = channel.truncate(5);
|
||||||
public void whenGetFileSize_thenCorrect() throws IOException {
|
assertEquals(5, channel.size());
|
||||||
|
|
||||||
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
fout.close();
|
||||||
FileChannel channel = reader.getChannel();) {
|
channel.close();
|
||||||
|
}
|
||||||
long imageFileSize = channel.size();
|
|
||||||
assertEquals(11, imageFileSize);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void whenTruncateFile_thenCorrect() throws IOException {
|
|
||||||
String input = "this is a test input";
|
|
||||||
|
|
||||||
FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
|
|
||||||
FileChannel channel = fout.getChannel();
|
|
||||||
|
|
||||||
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
|
|
||||||
channel.write(buff);
|
|
||||||
buff.flip();
|
|
||||||
|
|
||||||
channel = channel.truncate(5);
|
|
||||||
assertEquals(5, channel.size());
|
|
||||||
|
|
||||||
fout.close();
|
|
||||||
channel.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue