[BAEL-15998] - Move articles out of core-java-io part 1
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.selector;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SocketChannel;
|
||||
|
||||
public class EchoClient {
|
||||
private static SocketChannel client;
|
||||
private static ByteBuffer buffer;
|
||||
private static EchoClient instance;
|
||||
|
||||
public static EchoClient start() {
|
||||
if (instance == null)
|
||||
instance = new EchoClient();
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static void stop() throws IOException {
|
||||
client.close();
|
||||
buffer = null;
|
||||
}
|
||||
|
||||
private EchoClient() {
|
||||
try {
|
||||
client = SocketChannel.open(new InetSocketAddress("localhost", 5454));
|
||||
buffer = ByteBuffer.allocate(256);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public String sendMessage(String msg) {
|
||||
buffer = ByteBuffer.wrap(msg.getBytes());
|
||||
String response = null;
|
||||
try {
|
||||
client.write(buffer);
|
||||
buffer.clear();
|
||||
client.read(buffer);
|
||||
response = new String(buffer.array()).trim();
|
||||
System.out.println("response=" + response);
|
||||
buffer.clear();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return response;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.selector;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.SelectionKey;
|
||||
import java.nio.channels.Selector;
|
||||
import java.nio.channels.ServerSocketChannel;
|
||||
import java.nio.channels.SocketChannel;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
public class EchoServer {
|
||||
|
||||
private static final String POISON_PILL = "POISON_PILL";
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Selector selector = Selector.open();
|
||||
ServerSocketChannel serverSocket = ServerSocketChannel.open();
|
||||
serverSocket.bind(new InetSocketAddress("localhost", 5454));
|
||||
serverSocket.configureBlocking(false);
|
||||
serverSocket.register(selector, SelectionKey.OP_ACCEPT);
|
||||
ByteBuffer buffer = ByteBuffer.allocate(256);
|
||||
|
||||
while (true) {
|
||||
selector.select();
|
||||
Set<SelectionKey> selectedKeys = selector.selectedKeys();
|
||||
Iterator<SelectionKey> iter = selectedKeys.iterator();
|
||||
while (iter.hasNext()) {
|
||||
|
||||
SelectionKey key = iter.next();
|
||||
|
||||
if (key.isAcceptable()) {
|
||||
register(selector, serverSocket);
|
||||
}
|
||||
|
||||
if (key.isReadable()) {
|
||||
answerWithEcho(buffer, key);
|
||||
}
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void answerWithEcho(ByteBuffer buffer, SelectionKey key) throws IOException {
|
||||
SocketChannel client = (SocketChannel) key.channel();
|
||||
client.read(buffer);
|
||||
if (new String(buffer.array()).trim().equals(POISON_PILL)) {
|
||||
client.close();
|
||||
System.out.println("Not accepting client messages anymore");
|
||||
}
|
||||
|
||||
buffer.flip();
|
||||
client.write(buffer);
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
private static void register(Selector selector, ServerSocketChannel serverSocket) throws IOException {
|
||||
SocketChannel client = serverSocket.accept();
|
||||
client.configureBlocking(false);
|
||||
client.register(selector, SelectionKey.OP_READ);
|
||||
}
|
||||
|
||||
public static Process start() throws IOException, InterruptedException {
|
||||
String javaHome = System.getProperty("java.home");
|
||||
String javaBin = javaHome + File.separator + "bin" + File.separator + "java";
|
||||
String classpath = System.getProperty("java.class.path");
|
||||
String className = EchoServer.class.getCanonicalName();
|
||||
|
||||
ProcessBuilder builder = new ProcessBuilder(javaBin, "-cp", classpath, className);
|
||||
|
||||
return builder.start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.baeldung.symlink;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static java.nio.file.StandardOpenOption.CREATE;
|
||||
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
|
||||
|
||||
public class SymLinkExample {
|
||||
|
||||
public void createSymbolicLink(Path link, Path target) throws IOException {
|
||||
if (Files.exists(link)) {
|
||||
Files.delete(link);
|
||||
}
|
||||
Files.createSymbolicLink(link, target);
|
||||
}
|
||||
|
||||
public void createHardLink(Path link, Path target) throws IOException {
|
||||
if (Files.exists(link)) {
|
||||
Files.delete(link);
|
||||
}
|
||||
Files.createLink(link, target);
|
||||
}
|
||||
|
||||
public Path createTextFile() throws IOException {
|
||||
byte[] content = IntStream.range(0, 10000)
|
||||
.mapToObj(i -> i + System.lineSeparator())
|
||||
.reduce("", String::concat)
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
Path filePath = Paths.get(".", "target_link.txt");
|
||||
Files.write(filePath, content, CREATE, TRUNCATE_EXISTING);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
public void printLinkFiles(Path path) throws IOException {
|
||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
|
||||
for (Path file : stream) {
|
||||
if (Files.isDirectory(file)) {
|
||||
printLinkFiles(file);
|
||||
} else if (Files.isSymbolicLink(file)) {
|
||||
System.out.format("File link '%s' with target '%s'%n", file, Files.readSymbolicLink(file));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
new SymLinkExample().printLinkFiles(Paths.get("."));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.watcher;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.*;
|
||||
|
||||
public class DirectoryWatcherExample {
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
WatchService watchService = FileSystems.getDefault().newWatchService();
|
||||
Path path = Paths.get(System.getProperty("user.home"));
|
||||
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
|
||||
WatchKey key;
|
||||
while ((key = watchService.take()) != null) {
|
||||
for (WatchEvent<?> event : key.pollEvents()) {
|
||||
System.out.println("Event kind:" + event.kind() + ". File affected: " + event.context() + ".");
|
||||
}
|
||||
key.reset();
|
||||
}
|
||||
|
||||
watchService.close();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user