Skip to content

Commit 01cad34

Browse files
tomekl007pedja4
authored andcommitted
Bael 851 mapped byte buffer (eugenp#1742)
* BAEL-851 code for the MappedByteBuffer * BAEL-851 formatting * SetTest refactor (eugenp#1724) * Bael 850 (eugenp#1744) * BAEL-850 ConcurentskipLIst * BAEL-850 formatting * BAEL-850 Formatting * spring 5 work * SetTest refactor (eugenp#1724) * BAEL-850 use lambda * BAEL-850 no need to casting * Build optimization (eugenp#1746) * Build optimization * Build optimization * [BAEL-820] Difference between wait() and sleep() in Java (eugenp#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 66471cf. * Revert of evaluation task "injecting beans" This reverts commit d2ac201. * [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 (eugenp#1733) * remove utility classes * remove httpclient, extend http * move code to core-java
1 parent e83002c commit 01cad34

3 files changed

Lines changed: 69 additions & 0 deletions

File tree

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.mappedbytebuffer;
2+
3+
import org.junit.Test;
4+
5+
import java.nio.CharBuffer;
6+
import java.nio.MappedByteBuffer;
7+
import java.nio.channels.FileChannel;
8+
import java.nio.charset.Charset;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.nio.file.StandardOpenOption;
13+
import java.util.EnumSet;
14+
import java.util.List;
15+
16+
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertNotNull;
18+
19+
public class MappedByteBufferTest {
20+
21+
22+
@Test
23+
public void givenFileChannel_whenReadToTheMappedByteBuffer_thenShouldSuccess() throws Exception {
24+
//given
25+
CharBuffer charBuffer = null;
26+
Path pathToRead = getFileURIFromResources("fileToRead.txt");
27+
28+
//when
29+
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToRead, EnumSet.of(StandardOpenOption.READ))) {
30+
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
31+
32+
if (mappedByteBuffer != null) {
33+
charBuffer = Charset.forName("UTF-8").decode(mappedByteBuffer);
34+
}
35+
}
36+
37+
//then
38+
assertNotNull(charBuffer);
39+
assertEquals(charBuffer.toString(), "This is a content of the file");
40+
}
41+
42+
@Test
43+
public void givenPath_whenWriteToItUsingMappedByteBuffer_thenShouldSuccessfullyWrite() throws Exception {
44+
//given
45+
CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file");
46+
Path pathToWrite = getFileURIFromResources("fileToWriteTo.txt");
47+
48+
//when
49+
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(pathToWrite,
50+
EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING))) {
51+
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, charBuffer.length());
52+
53+
if (mappedByteBuffer != null) {
54+
mappedByteBuffer.put(Charset.forName("utf-8").encode(charBuffer));
55+
}
56+
}
57+
58+
//then
59+
List<String> fileContent = Files.readAllLines(pathToWrite);
60+
assertEquals(fileContent.get(0), "This will be written to the file");
61+
62+
}
63+
64+
public Path getFileURIFromResources(String fileName) throws Exception {
65+
ClassLoader classLoader = getClass().getClassLoader();
66+
return Paths.get(classLoader.getResource(fileName).getPath());
67+
}
68+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a content of the file

‎core-java/src/test/resources/fileToWriteTo.txt‎

Whitespace-only changes.

0 commit comments

Comments
 (0)