changes for review comments
This commit is contained in:
parent
fe53a23d41
commit
d9540eff75
|
@ -21,10 +21,8 @@ public class FileChannelUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
|
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
|
||||||
String expected_value = "Hello world";
|
|
||||||
String file = "src/test/resources/test_read.in";
|
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
||||||
|
|
||||||
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
|
|
||||||
FileChannel channel = reader.getChannel();
|
FileChannel channel = reader.getChannel();
|
||||||
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
|
ByteArrayOutputStream out = new ByteArrayOutputStream();) {
|
||||||
|
|
||||||
|
@ -34,130 +32,108 @@ public class FileChannelUnitTest {
|
||||||
}
|
}
|
||||||
ByteBuffer buff = ByteBuffer.allocate(bufferSize);
|
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
|
|
||||||
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
|
|
||||||
String expected_value = "Hello world";
|
|
||||||
String file = "src/test/resources/test_read.in";
|
|
||||||
|
|
||||||
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
||||||
FileInputStream fin = new FileInputStream(file);
|
|
||||||
FileChannel channel = fin.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, new String(out.toByteArray(), StandardCharsets.UTF_8));
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
|
|
||||||
String expected_value = "world";
|
|
||||||
String file = "src/test/resources/test_read.in";
|
|
||||||
|
|
||||||
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));
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@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";
|
|
||||||
|
|
||||||
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";
|
|
||||||
|
|
||||||
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) {
|
while (channel.read(buff) > 0) {
|
||||||
out.write(buff.array(), 0, buff.position());
|
out.write(buff.array(), 0, buff.position());
|
||||||
buff.clear();
|
buff.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(expected_value, channel.position());
|
assertEquals("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFile_whenReadWithFileChannelUsingFileInputStream_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
FileInputStream fin = new FileInputStream("src/test/resources/test_read.in");
|
||||||
|
FileChannel channel = fin.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("Hello world", new String(out.toByteArray(), StandardCharsets.UTF_8));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFile_whenReadAFileSectionIntoMemoryWithFileChannel_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "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("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));
|
||||||
|
|
||||||
|
if (buff.hasRemaining()) {
|
||||||
|
channel.write(buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify
|
||||||
|
RandomAccessFile reader = new RandomAccessFile(file, "r");
|
||||||
|
assertEquals("Hello world", reader.readLine());
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
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);) {
|
||||||
|
|
||||||
|
assertNotNull(fileLock);
|
||||||
|
|
||||||
|
} catch (OverlappingFileLockException | IOException ex) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFile_whenReadWithFileChannelGetPosition_thenCorrect() throws IOException {
|
||||||
|
|
||||||
|
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "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(11, channel.position());
|
||||||
|
|
||||||
channel.position(4);
|
channel.position(4);
|
||||||
assertEquals(4, channel.position());
|
assertEquals(4, channel.position());
|
||||||
|
@ -165,28 +141,23 @@ public class FileChannelUnitTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenGetFileSize_thenCorrect() throws IOException {
|
public void whenGetFileSize_thenCorrect() throws IOException {
|
||||||
long expectedSize = 11;
|
|
||||||
String file = "src/test/resources/test_read.in";
|
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
|
||||||
|
FileChannel channel = reader.getChannel();) {
|
||||||
try (RandomAccessFile reader = new RandomAccessFile(file, "r");
|
|
||||||
FileChannel channel = reader.getChannel();) {
|
|
||||||
|
|
||||||
long imageFileSize = channel.size();
|
long imageFileSize = channel.size();
|
||||||
assertEquals(expectedSize, imageFileSize);
|
assertEquals(11, imageFileSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenTruncateFile_thenCorrect() throws IOException {
|
public void whenTruncateFile_thenCorrect() throws IOException {
|
||||||
long expectedSize = 5;
|
|
||||||
String input = "this is a test input";
|
String input = "this is a test input";
|
||||||
String file = "src/test/resources/test_truncate.txt";
|
|
||||||
|
|
||||||
FileOutputStream fout = new FileOutputStream(file);
|
FileOutputStream fout = new FileOutputStream("src/test/resources/test_truncate.txt");
|
||||||
FileChannel channel = fout.getChannel();
|
FileChannel channel = fout.getChannel();
|
||||||
|
|
||||||
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
|
ByteBuffer buff = ByteBuffer.wrap(input.getBytes());
|
||||||
|
@ -194,7 +165,7 @@ public class FileChannelUnitTest {
|
||||||
buff.flip();
|
buff.flip();
|
||||||
|
|
||||||
channel = channel.truncate(5);
|
channel = channel.truncate(5);
|
||||||
assertEquals(expectedSize, channel.size());
|
assertEquals(5, channel.size());
|
||||||
|
|
||||||
fout.close();
|
fout.close();
|
||||||
channel.close();
|
channel.close();
|
||||||
|
|
Loading…
Reference in New Issue