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
This commit is contained in:
Tomasz Lelek 2017-04-28 17:02:08 +02:00 committed by pedja4
parent e83002ce99
commit 01cad343f7
3 changed files with 69 additions and 0 deletions

View File

@ -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<String> 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());
}
}

View File

@ -0,0 +1 @@
This is a content of the file