[BAEL-1951]create new module core-java-nio and adding java filechannel examples (#6899)

* create new module core-java-nio and adding filechannel

* updated readme with NIO article details

* fixed typo

* Remore unused dependencies from pom.xml

* fixed build issue

* moving into core-java-modules folder

* remove unused plugins from pom.xml

* remove junk file and update readme

* removing readme.md

* change in indentation. remove tabs to spaces
This commit is contained in:
anuraggoyal1 2019-05-22 01:54:38 +05:30 committed by ashleyfrieze
parent 4a46d484cf
commit 49304324ad
8 changed files with 205 additions and 1 deletions

View File

@ -0,0 +1,5 @@
0.*
# Files generated by integration tests
# *.txt
/temp

View File

@ -0,0 +1,16 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-nio</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-nio</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
</project>

View File

@ -0,0 +1,165 @@
package com.baeldung.filechannel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
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.charset.StandardCharsets;
import org.junit.Test;
public class FileChannelUnitTest {
@Test
public void givenFile_whenReadWithFileChannelUsingRandomAccessFile_thenCorrect() throws IOException {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel();
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
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();
}
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello world", fileContent);
}
}
@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();
}
String fileContent = new String(out.toByteArray(), StandardCharsets.UTF_8);
assertEquals("Hello world", fileContent);
}
}
@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));
channel.write(buff);
// now we verify whether the file was written correctly
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);
}
}
@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();
}
// the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, channel.position());
channel.position(4);
assertEquals(4, channel.position());
}
}
@Test
public void whenGetFileSize_thenCorrect() throws IOException {
try (RandomAccessFile reader = new RandomAccessFile("src/test/resources/test_read.in", "r");
FileChannel channel = reader.getChannel()) {
// the original file is 11 bytes long, so that's where the position pointer should be
assertEquals(11, channel.size());
}
}
@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();
}
}

View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -0,0 +1 @@
Hello world

View File

@ -392,6 +392,7 @@
<module>core-java-modules/core-java-concurrency-basic</module>
<module>core-java-modules/core-java-concurrency-collections</module>
<module>core-java-modules/core-java-io</module>
<module>core-java-modules/core-java-nio</module>
<module>core-java-modules/core-java-security</module>
<module>core-java-modules/core-java-lang-syntax</module>
<module>core-java-modules/core-java-lang</module>
@ -1074,6 +1075,7 @@
<module>core-java-modules/core-java-concurrency-basic</module>
<module>core-java-modules/core-java-concurrency-collections</module>
<module>core-java-modules/core-java-io</module>
<module>core-java-modules/core-java-nio</module>
<module>core-java-modules/core-java-security</module>
<module>core-java-modules/core-java-lang-syntax</module>
<module>core-java-modules/core-java-lang</module>
@ -1560,4 +1562,4 @@
<lombok.version>1.16.12</lombok.version>
<h2.version>1.4.197</h2.version>
</properties>
</project>
</project>