From 01cad343f7eac74686d5acd19037e7866e05469f Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 28 Apr 2017 17:02:08 +0200 Subject: [PATCH] Bael 851 mapped byte buffer (#1742) * BAEL-851 code for the MappedByteBuffer * BAEL-851 formatting * SetTest refactor (#1724) * Bael 850 (#1744) * BAEL-850 ConcurentskipLIst * BAEL-850 formatting * BAEL-850 Formatting * spring 5 work * SetTest refactor (#1724) * BAEL-850 use lambda * BAEL-850 no need to casting * Build optimization (#1746) * Build optimization * Build optimization * [BAEL-820] Difference between wait() and sleep() in Java (#1708) * injecting beans * XML-based configuration replaced with Java Config. * [BAEL-431] Exploring TestRestTemplate. * Revert of evaluation task "XML-based configuration replaced with Java Config." This reverts commit 66471cf0574c85f8ff514ec4caf5ba44ebba1a74. * Revert of evaluation task "injecting beans" This reverts commit d2ac20185e636245bc0ae0b4ccb952965de88e28. * [BAEL-431] fix to the tests in TestRestTemplateBasicLiveTest. * [BAEL-431] added more meaningful user and password for auth. * [BAEL-820] examples of wait() and sleep() methods. * [BAEL-820] wait() and sleep() examples. * remove utility classes (#1733) * remove utility classes * remove httpclient, extend http * move code to core-java --- .../MappedByteBufferTest.java | 68 +++++++++++++++++++ core-java/src/test/resources/fileToRead.txt | 1 + .../src/test/resources/fileToWriteTo.txt | 0 3 files changed, 69 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferTest.java create mode 100644 core-java/src/test/resources/fileToRead.txt create mode 100644 core-java/src/test/resources/fileToWriteTo.txt diff --git a/core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferTest.java b/core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferTest.java new file mode 100644 index 0000000000..4064d38267 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/mappedbytebuffer/MappedByteBufferTest.java @@ -0,0 +1,68 @@ +package com.baeldung.mappedbytebuffer; + +import org.junit.Test; + +import java.nio.CharBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.FileChannel; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.EnumSet; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class MappedByteBufferTest { + + + @Test + public void givenFileChannel_whenReadToTheMappedByteBuffer_thenShouldSuccess() throws Exception { + //given + CharBuffer charBuffer = null; + Path pathToRead = getFileURIFromResources("fileToRead.txt"); + + //when + try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToRead, EnumSet.of(StandardOpenOption.READ))) { + MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); + + if (mappedByteBuffer != null) { + charBuffer = Charset.forName("UTF-8").decode(mappedByteBuffer); + } + } + + //then + assertNotNull(charBuffer); + assertEquals(charBuffer.toString(), "This is a content of the file"); + } + + @Test + public void givenPath_whenWriteToItUsingMappedByteBuffer_thenShouldSuccessfullyWrite() throws Exception { + //given + CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file"); + Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt"); + + //when + try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite, + EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) { + MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, charBuffer.length()); + + if (mappedByteBuffer != null) { + mappedByteBuffer.put(Charset.forName("utf-8").encode(charBuffer)); + } + } + + //then + List fileContent = Files.readAllLines(pathToWrite); + assertEquals(fileContent.get(0), "This will be written to the file"); + + } + + public Path getFileURIFromResources(String fileName) throws Exception { + ClassLoader classLoader = getClass().getClassLoader(); + return Paths.get(classLoader.getResource(fileName).getPath()); + } +} diff --git a/core-java/src/test/resources/fileToRead.txt b/core-java/src/test/resources/fileToRead.txt new file mode 100644 index 0000000000..45d73fa10d --- /dev/null +++ b/core-java/src/test/resources/fileToRead.txt @@ -0,0 +1 @@ +This is a content of the file \ No newline at end of file diff --git a/core-java/src/test/resources/fileToWriteTo.txt b/core-java/src/test/resources/fileToWriteTo.txt new file mode 100644 index 0000000000..e69de29bb2