Merge branch 'eugenp:master' into master
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
### Relevant articles:
|
||||
|
||||
- [Collect a Java Stream to an Immutable Collection](https://www.baeldung.com/java-stream-immutable-collection)
|
||||
- [Guide to mapMulti in Stream API](https://www.baeldung.com/java-mapmulti)
|
||||
|
||||
@@ -10,3 +10,4 @@ This module contains articles about advanced operations on arrays in Java. They
|
||||
- [Intersection Between two Integer Arrays](https://www.baeldung.com/java-array-intersection)
|
||||
- [Comparing Arrays in Java](https://www.baeldung.com/java-comparing-arrays)
|
||||
- [Concatenate Two Arrays in Java](https://www.baeldung.com/java-concatenate-arrays)
|
||||
- [Performance of System.arraycopy() vs. Arrays.copyOf()](https://www.baeldung.com/java-system-arraycopy-arrays-copyof-performance)
|
||||
|
||||
@@ -26,10 +26,50 @@
|
||||
<version>${assertj-core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<assertj-core.version>3.10.0</assertj-core.version>
|
||||
<jmh.version>1.33</jmh.version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>jar-with-dependencies</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>com.baeldung.copyarraymethodsperformance.BenchmarkRunner</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.copyarraymethodsperformance;
|
||||
|
||||
public class BenchmarkRunner {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.baeldung.copyarraymethodsperformance;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 10)
|
||||
@Fork(1)
|
||||
@Measurement(iterations = 100)
|
||||
public class ObjectsCopyBenchmark {
|
||||
|
||||
@Param({ "10", "1000000" })
|
||||
public int SIZE;
|
||||
Integer[] src;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
Random r = new Random();
|
||||
src = new Integer[SIZE];
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
src[i] = r.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Integer[] systemArrayCopyBenchmark() {
|
||||
Integer[] target = new Integer[SIZE];
|
||||
System.arraycopy(src, 0, target, 0, SIZE);
|
||||
return target;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public Integer[] arraysCopyOfBenchmark() {
|
||||
return Arrays.copyOf(src, SIZE);
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.copyarraymethodsperformance;
|
||||
|
||||
import org.openjdk.jmh.annotations.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@State(Scope.Thread)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 10)
|
||||
@Fork(1)
|
||||
@Measurement(iterations = 100)
|
||||
public class PrimitivesCopyBenchmark {
|
||||
|
||||
@Param({ "10", "1000000" })
|
||||
public int SIZE;
|
||||
|
||||
int[] src;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
Random r = new Random();
|
||||
src = new int[SIZE];
|
||||
|
||||
for (int i = 0; i < SIZE; i++) {
|
||||
src[i] = r.nextInt();
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int[] systemArrayCopyBenchmark() {
|
||||
int[] target = new int[SIZE];
|
||||
System.arraycopy(src, 0, target, 0, SIZE);
|
||||
return target;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int[] arraysCopyOfBenchmark() {
|
||||
return Arrays.copyOf(src, SIZE);
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,5 @@ This module contains articles about networking in Java
|
||||
- [Downloading Email Attachments in Java](https://www.baeldung.com/java-download-email-attachments)
|
||||
- [Connection Timeout vs. Read Timeout for Java Sockets](https://www.baeldung.com/java-socket-connection-read-timeout)
|
||||
- [Find Whether an IP Address Is in the Specified Range or Not in Java](https://www.baeldung.com/java-check-ip-address-range)
|
||||
- [Find the IP Address of a Client Connected to a Server](https://www.baeldung.com/java-client-get-ip-address)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-networking-2)
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.clientaddress;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ApplicationClient {
|
||||
|
||||
private Socket clientSocket;
|
||||
private PrintWriter out;
|
||||
private BufferedReader in;
|
||||
|
||||
public void connect(String ip, int port) throws IOException {
|
||||
clientSocket = new Socket(ip, port);
|
||||
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||
}
|
||||
|
||||
public void sendGreetings(String msg) throws IOException {
|
||||
out.println(msg);
|
||||
String reply = in.readLine();
|
||||
System.out.println("Reply received from the server :: " + reply);
|
||||
}
|
||||
|
||||
public void disconnect() throws IOException {
|
||||
in.close();
|
||||
out.close();
|
||||
clientSocket.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
ApplicationClient client = new ApplicationClient();
|
||||
client.connect(args[0], Integer.parseInt(args[1])); // IP address and port number of the server
|
||||
client.sendGreetings(args[2]); // greetings message
|
||||
client.disconnect();
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.clientaddress;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
public class ApplicationServer {
|
||||
|
||||
private ServerSocket serverSocket;
|
||||
private Socket connectedSocket;
|
||||
private PrintWriter out;
|
||||
private BufferedReader in;
|
||||
|
||||
public void startServer(int port) throws IOException {
|
||||
serverSocket = new ServerSocket(port);
|
||||
connectedSocket = serverSocket.accept();
|
||||
|
||||
InetSocketAddress socketAddress = (InetSocketAddress) connectedSocket.getRemoteSocketAddress();
|
||||
String clientIpAddress = socketAddress.getAddress()
|
||||
.getHostAddress();
|
||||
System.out.println("IP address of the connected client :: " + clientIpAddress);
|
||||
|
||||
out = new PrintWriter(connectedSocket.getOutputStream(), true);
|
||||
in = new BufferedReader(new InputStreamReader(connectedSocket.getInputStream()));
|
||||
String msg = in.readLine();
|
||||
System.out.println("Message received from the client :: " + msg);
|
||||
out.println("Hello Client !!");
|
||||
|
||||
closeIO();
|
||||
stopServer();
|
||||
}
|
||||
|
||||
private void closeIO() throws IOException {
|
||||
in.close();
|
||||
out.close();
|
||||
}
|
||||
|
||||
private void stopServer() throws IOException {
|
||||
connectedSocket.close();
|
||||
serverSocket.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
ApplicationServer server = new ApplicationServer();
|
||||
server.startServer(5000);
|
||||
}
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package com.baeldung.java9.process;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ProcessAPIEnhancements {
|
||||
|
||||
static Logger log = LoggerFactory.getLogger(ProcessAPIEnhancements.class);
|
||||
|
||||
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
|
||||
infoOfCurrentProcess();
|
||||
infoOfLiveProcesses();
|
||||
infoOfSpawnProcess();
|
||||
infoOfExitCallback();
|
||||
infoOfChildProcess();
|
||||
}
|
||||
|
||||
private static void infoOfCurrentProcess() {
|
||||
ProcessHandle processHandle = ProcessHandle.current();
|
||||
ProcessHandle.Info processInfo = processHandle.info();
|
||||
|
||||
log.info("PID: " + processHandle.pid());
|
||||
log.info("Arguments: " + processInfo.arguments());
|
||||
log.info("Command: " + processInfo.command());
|
||||
log.info("Instant: " + processInfo.startInstant());
|
||||
log.info("Total CPU duration: " + processInfo.totalCpuDuration());
|
||||
log.info("User: " + processInfo.user());
|
||||
}
|
||||
|
||||
private static void infoOfSpawnProcess() throws IOException {
|
||||
|
||||
String javaCmd = ProcessUtils.getJavaCmd().getAbsolutePath();
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version");
|
||||
Process process = processBuilder.inheritIO().start();
|
||||
ProcessHandle processHandle = process.toHandle();
|
||||
ProcessHandle.Info processInfo = processHandle.info();
|
||||
|
||||
log.info("PID: " + processHandle.pid());
|
||||
log.info("Arguments: " + processInfo.arguments());
|
||||
log.info("Command: " + processInfo.command());
|
||||
log.info("Instant: " + processInfo.startInstant());
|
||||
log.info("Total CPU duration: " + processInfo.totalCpuDuration());
|
||||
log.info("User: " + processInfo.user());
|
||||
}
|
||||
|
||||
private static void infoOfLiveProcesses() {
|
||||
Stream<ProcessHandle> liveProcesses = ProcessHandle.allProcesses();
|
||||
liveProcesses.filter(ProcessHandle::isAlive)
|
||||
.forEach(ph -> {
|
||||
log.info("PID: " + ph.pid());
|
||||
log.info("Instance: " + ph.info().startInstant());
|
||||
log.info("User: " + ph.info().user());
|
||||
});
|
||||
}
|
||||
|
||||
private static void infoOfChildProcess() throws IOException {
|
||||
int childProcessCount = 5;
|
||||
for (int i = 0; i < childProcessCount; i++) {
|
||||
String javaCmd = ProcessUtils.getJavaCmd()
|
||||
.getAbsolutePath();
|
||||
ProcessBuilder processBuilder
|
||||
= new ProcessBuilder(javaCmd, "-version");
|
||||
processBuilder.inheritIO().start();
|
||||
}
|
||||
|
||||
Stream<ProcessHandle> children = ProcessHandle.current()
|
||||
.children();
|
||||
children.filter(ProcessHandle::isAlive)
|
||||
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info()
|
||||
.command()));
|
||||
Stream<ProcessHandle> descendants = ProcessHandle.current()
|
||||
.descendants();
|
||||
descendants.filter(ProcessHandle::isAlive)
|
||||
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info()
|
||||
.command()));
|
||||
}
|
||||
|
||||
private static void infoOfExitCallback() throws IOException, InterruptedException, ExecutionException {
|
||||
String javaCmd = ProcessUtils.getJavaCmd()
|
||||
.getAbsolutePath();
|
||||
ProcessBuilder processBuilder
|
||||
= new ProcessBuilder(javaCmd, "-version");
|
||||
Process process = processBuilder.inheritIO()
|
||||
.start();
|
||||
ProcessHandle processHandle = process.toHandle();
|
||||
|
||||
log.info("PID: {} has started", processHandle.pid());
|
||||
CompletableFuture<ProcessHandle> onProcessExit = processHandle.onExit();
|
||||
onProcessExit.get();
|
||||
log.info("Alive: " + processHandle.isAlive());
|
||||
onProcessExit.thenAccept(ph -> {
|
||||
log.info("PID: {} has stopped", ph.pid());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
-132
@@ -1,132 +0,0 @@
|
||||
package com.baeldung.java9.process;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Created by sanaulla on 2/23/2017.
|
||||
*/
|
||||
|
||||
public class ProcessAPIEnhancementsUnitTest {
|
||||
|
||||
Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsUnitTest.class);
|
||||
|
||||
// @Test
|
||||
// OS / Java version dependent
|
||||
public void givenCurrentProcess_whenInvokeGetInfo_thenSuccess() throws IOException {
|
||||
ProcessHandle processHandle = ProcessHandle.current();
|
||||
ProcessHandle.Info processInfo = processHandle.info();
|
||||
assertNotNull(processHandle.pid());
|
||||
assertEquals(true, processInfo.arguments()
|
||||
.isPresent());
|
||||
assertEquals(true, processInfo.command()
|
||||
.isPresent());
|
||||
assertTrue(processInfo.command()
|
||||
.get()
|
||||
.contains("java"));
|
||||
|
||||
assertEquals(true, processInfo.startInstant()
|
||||
.isPresent());
|
||||
assertEquals(true, processInfo.totalCpuDuration()
|
||||
.isPresent());
|
||||
assertEquals(true, processInfo.user()
|
||||
.isPresent());
|
||||
}
|
||||
|
||||
// @Test
|
||||
// OS / Java version dependent
|
||||
public void givenSpawnProcess_whenInvokeGetInfo_thenSuccess() throws IOException {
|
||||
|
||||
String javaCmd = ProcessUtils.getJavaCmd()
|
||||
.getAbsolutePath();
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version");
|
||||
Process process = processBuilder.inheritIO()
|
||||
.start();
|
||||
ProcessHandle processHandle = process.toHandle();
|
||||
ProcessHandle.Info processInfo = processHandle.info();
|
||||
assertNotNull(processHandle.pid());
|
||||
assertEquals(true, processInfo.arguments()
|
||||
.isPresent());
|
||||
assertEquals(true, processInfo.command()
|
||||
.isPresent());
|
||||
assertTrue(processInfo.command()
|
||||
.get()
|
||||
.contains("java"));
|
||||
assertEquals(true, processInfo.startInstant()
|
||||
.isPresent());
|
||||
assertEquals(false, processInfo.totalCpuDuration()
|
||||
.isPresent());
|
||||
assertEquals(true, processInfo.user()
|
||||
.isPresent());
|
||||
}
|
||||
|
||||
// @Test
|
||||
// OS / Java version dependent
|
||||
public void givenLiveProcesses_whenInvokeGetInfo_thenSuccess() {
|
||||
Stream<ProcessHandle> liveProcesses = ProcessHandle.allProcesses();
|
||||
liveProcesses.filter(ProcessHandle::isAlive)
|
||||
.forEach(ph -> {
|
||||
assertNotNull(ph.pid());
|
||||
assertEquals(true, ph.info()
|
||||
.startInstant()
|
||||
.isPresent());
|
||||
assertEquals(true, ph.info()
|
||||
.user()
|
||||
.isPresent());
|
||||
});
|
||||
}
|
||||
|
||||
// @Test
|
||||
// OS / Java version dependent
|
||||
public void givenProcess_whenGetChildProcess_thenSuccess() throws IOException {
|
||||
int childProcessCount = 5;
|
||||
for (int i = 0; i < childProcessCount; i++) {
|
||||
String javaCmd = ProcessUtils.getJavaCmd()
|
||||
.getAbsolutePath();
|
||||
ProcessBuilder processBuilder
|
||||
= new ProcessBuilder(javaCmd, "-version");
|
||||
processBuilder.inheritIO().start();
|
||||
}
|
||||
|
||||
Stream<ProcessHandle> children = ProcessHandle.current()
|
||||
.children();
|
||||
children.filter(ProcessHandle::isAlive)
|
||||
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info()
|
||||
.command()));
|
||||
Stream<ProcessHandle> descendants = ProcessHandle.current()
|
||||
.descendants();
|
||||
descendants.filter(ProcessHandle::isAlive)
|
||||
.forEach(ph -> log.info("PID: {}, Cmd: {}", ph.pid(), ph.info()
|
||||
.command()));
|
||||
}
|
||||
|
||||
// @Test
|
||||
// OS / Java version dependent
|
||||
public void givenProcess_whenAddExitCallback_thenSuccess() throws Exception {
|
||||
String javaCmd = ProcessUtils.getJavaCmd()
|
||||
.getAbsolutePath();
|
||||
ProcessBuilder processBuilder
|
||||
= new ProcessBuilder(javaCmd, "-version");
|
||||
Process process = processBuilder.inheritIO()
|
||||
.start();
|
||||
ProcessHandle processHandle = process.toHandle();
|
||||
|
||||
log.info("PID: {} has started", processHandle.pid());
|
||||
CompletableFuture<ProcessHandle> onProcessExit = processHandle.onExit();
|
||||
onProcessExit.get();
|
||||
assertEquals(false, processHandle.isAlive());
|
||||
onProcessExit.thenAccept(ph -> {
|
||||
log.info("PID: {} has stopped", ph.pid());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,4 +5,5 @@ This module contains articles about core Java Security
|
||||
### Relevant Articles:
|
||||
|
||||
- [Secret Key and String Conversion in Java](https://www.baeldung.com/java-secret-key-to-string)
|
||||
- [Enabling Unlimited Strength Cryptography in Java](https://www.baeldung.com/jce-enable-unlimited-strength)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-security-2)
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.cryptography;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CryptographyStrengthUnitTest {
|
||||
private static final int UNLIMITED_KEY_SIZE = 2147483647;
|
||||
|
||||
@Test
|
||||
public void whenDefaultCheck_thenUnlimitedReturned() throws NoSuchAlgorithmException {
|
||||
int maxKeySize = javax.crypto.Cipher.getMaxAllowedKeyLength("AES");
|
||||
assertThat(maxKeySize).isEqualTo(UNLIMITED_KEY_SIZE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user