[BAEL-15998] - Move articles out of core-java-io part 1
This commit is contained in:
@@ -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 MappedByteBufferUnitTest {
|
||||
|
||||
@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
|
||||
final CharBuffer charBuffer = CharBuffer.wrap("This will be written to the file");
|
||||
final 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
|
||||
final List<String> fileContent = Files.readAllLines(pathToWrite);
|
||||
assertEquals(fileContent.get(0), "This will be written to the file");
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
private final Path getFileURIFromResources(String fileName) throws Exception {
|
||||
final ClassLoader classLoader = getClass().getClassLoader();
|
||||
return Paths.get(classLoader.getResource(fileName).toURI());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.selector;
|
||||
|
||||
import com.baeldung.selector.EchoClient;
|
||||
import com.baeldung.selector.EchoServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class NioEchoLiveTest {
|
||||
|
||||
private Process server;
|
||||
private EchoClient client;
|
||||
|
||||
@Before
|
||||
public void setup() throws IOException, InterruptedException {
|
||||
server = EchoServer.start();
|
||||
client = EchoClient.start();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServerClient_whenServerEchosMessage_thenCorrect() {
|
||||
String resp1 = client.sendMessage("hello");
|
||||
String resp2 = client.sendMessage("world");
|
||||
assertEquals("hello", resp1);
|
||||
assertEquals("world", resp2);
|
||||
}
|
||||
|
||||
@After
|
||||
public void teardown() throws IOException {
|
||||
server.destroy();
|
||||
EchoClient.stop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.symlink;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SymLinkExampleManualTest {
|
||||
|
||||
@Test
|
||||
public void whenUsingFiles_thenCreateSymbolicLink() throws IOException {
|
||||
SymLinkExample example = new SymLinkExample();
|
||||
Path filePath = example.createTextFile();
|
||||
Path linkPath = Paths.get(".", "symbolic_link.txt");
|
||||
example.createSymbolicLink(linkPath, filePath);
|
||||
assertTrue(Files.isSymbolicLink(linkPath));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFiles_thenCreateHardLink() throws IOException {
|
||||
SymLinkExample example = new SymLinkExample();
|
||||
Path filePath = example.createTextFile();
|
||||
Path linkPath = Paths.get(".", "hard_link.txt");
|
||||
example.createHardLink(linkPath, filePath);
|
||||
assertFalse(Files.isSymbolicLink(linkPath));
|
||||
assertEquals(filePath.toFile()
|
||||
.length(),
|
||||
linkPath.toFile()
|
||||
.length());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
This is a content of the file
|
||||
Reference in New Issue
Block a user