Skip to content

Commit f74f450

Browse files
authored
Core java 12 improvements (eugenp#11090)
* Commit source code to branch * Fix to BAEL-5072 Change package definition and implementation of compactValues junit * BAEL-5053 Compare file contents
1 parent 31bbc5c commit f74f450

27 files changed

Lines changed: 1014 additions & 51 deletions

File tree

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,60 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
6-
<artifactId>core-java-12</artifactId>
7-
<version>0.1.0-SNAPSHOT</version>
8-
<name>core-java-12</name>
9-
<packaging>jar</packaging>
10-
<url>http://maven.apache.org</url>
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<artifactId>core-java-12</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>core-java-12</name>
9+
<packaging>jar</packaging>
10+
<url>http://maven.apache.org</url>
1111

12-
<parent>
13-
<groupId>com.baeldung</groupId>
14-
<artifactId>parent-modules</artifactId>
15-
<version>1.0.0-SNAPSHOT</version>
16-
<relativePath>../../</relativePath>
17-
</parent>
12+
<parent>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>parent-modules</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
16+
<relativePath>../../</relativePath>
17+
</parent>
1818

19-
<dependencies>
20-
<dependency>
21-
<groupId>org.assertj</groupId>
22-
<artifactId>assertj-core</artifactId>
23-
<version>${assertj.version}</version>
24-
<scope>test</scope>
25-
</dependency>
26-
</dependencies>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.assertj</groupId>
22+
<artifactId>assertj-core</artifactId>
23+
<version>${assertj.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>commons-io</groupId>
28+
<artifactId>commons-io</artifactId>
29+
<version>2.11.0</version>
30+
</dependency>
31+
</dependencies>
2732

28-
<build>
29-
<plugins>
30-
<plugin>
31-
<groupId>org.apache.maven.plugins</groupId>
32-
<artifactId>maven-compiler-plugin</artifactId>
33-
<version>${maven-compiler-plugin.version}</version>
34-
<configuration>
35-
<source>${maven.compiler.source.version}</source>
36-
<target>${maven.compiler.target.version}</target>
37-
<compilerArgs>--enable-preview</compilerArgs>
38-
</configuration>
39-
</plugin>
40-
</plugins>
41-
</build>
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<version>${maven-compiler-plugin.version}</version>
39+
<configuration>
40+
<source>${maven.compiler.source.version}</source>
41+
<target>${maven.compiler.target.version}</target>
42+
<compilerArgs>--enable-preview</compilerArgs>
43+
</configuration>
44+
</plugin>
45+
<plugin>
46+
<artifactId>maven-surefire-plugin</artifactId>
47+
<configuration>
48+
<argLine>--enable-preview</argLine>
49+
</configuration>
50+
</plugin>
51+
</plugins>
52+
</build>
4253

43-
<properties>
44-
<maven.compiler.source.version>12</maven.compiler.source.version>
45-
<maven.compiler.target.version>12</maven.compiler.target.version>
46-
<assertj.version>3.6.1</assertj.version>
47-
</properties>
54+
<properties>
55+
<maven.compiler.source.version>12</maven.compiler.source.version>
56+
<maven.compiler.target.version>12</maven.compiler.target.version>
57+
<assertj.version>3.6.1</assertj.version>
58+
</properties>
4859

4960
</project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.baeldung.file.content.comparison;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedReader;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.RandomAccessFile;
8+
import java.nio.ByteBuffer;
9+
import java.nio.MappedByteBuffer;
10+
import java.nio.channels.FileChannel;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
14+
public class CompareFileContents {
15+
16+
public static long filesCompareByByte(Path path1, Path path2) throws IOException {
17+
18+
if (path1.getFileSystem()
19+
.provider()
20+
.isSameFile(path1, path2)) {
21+
return -1;
22+
}
23+
24+
try (BufferedInputStream fis1 = new BufferedInputStream(new FileInputStream(path1.toFile()));
25+
BufferedInputStream fis2 = new BufferedInputStream(new FileInputStream(path2.toFile()))) {
26+
int ch = 0;
27+
long pos = 1;
28+
while ((ch = fis1.read()) != -1) {
29+
if (ch != fis2.read()) {
30+
return pos;
31+
}
32+
pos++;
33+
}
34+
if (fis2.read() == -1) {
35+
return -1;
36+
} else {
37+
return pos;
38+
}
39+
}
40+
}
41+
42+
public static long filesCompareByLine(Path path1, Path path2) throws IOException {
43+
44+
if (path1.getFileSystem()
45+
.provider()
46+
.isSameFile(path1, path2)) {
47+
return -1;
48+
}
49+
50+
try (BufferedReader bf1 = Files.newBufferedReader(path1);
51+
BufferedReader bf2 = Files.newBufferedReader(path2)) {
52+
53+
long lineNumber = 1;
54+
String line1 = "", line2 = "";
55+
while ((line1 = bf1.readLine()) != null) {
56+
line2 = bf2.readLine();
57+
if (line2 == null || !line1.equals(line2)) {
58+
return lineNumber;
59+
}
60+
lineNumber++;
61+
}
62+
if (bf2.readLine() == null) {
63+
return -1;
64+
} else {
65+
return lineNumber;
66+
}
67+
}
68+
}
69+
70+
public static boolean compareByMemoryMappedFiles(Path path1, Path path2) throws IOException {
71+
72+
try (RandomAccessFile randomAccessFile1 = new RandomAccessFile(path1.toFile(), "r");
73+
RandomAccessFile randomAccessFile2 = new RandomAccessFile(path2.toFile(), "r")) {
74+
75+
FileChannel ch1 = randomAccessFile1.getChannel();
76+
FileChannel ch2 = randomAccessFile2.getChannel();
77+
if (ch1.size() != ch2.size()) {
78+
79+
return false;
80+
}
81+
long size = ch1.size();
82+
MappedByteBuffer m1 = ch1.map(FileChannel.MapMode.READ_ONLY, 0L, size);
83+
MappedByteBuffer m2 = ch2.map(FileChannel.MapMode.READ_ONLY, 0L, size);
84+
85+
return m1.equals(m2);
86+
}
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.baeldung.file.content.comparison;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileInputStream;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.Reader;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
15+
import org.apache.commons.io.IOUtils;
16+
import org.junit.jupiter.api.AfterAll;
17+
import org.junit.jupiter.api.BeforeAll;
18+
import org.junit.jupiter.api.Test;
19+
20+
public class CampareFileContentsApacheIOUnitTest {
21+
22+
public static Path path1 = null;
23+
public static Path path2 = null;
24+
25+
@BeforeAll
26+
public static void setup() throws IOException {
27+
28+
path1 = Files.createTempFile("file1Test", ".txt");
29+
path2 = Files.createTempFile("file2Test", ".txt");
30+
}
31+
32+
@Test
33+
public void whenFilesIdentical_thenReturnTrue() throws IOException {
34+
35+
InputStream inputStream1 = new FileInputStream(path1.toFile());
36+
InputStream inputStream2 = new FileInputStream(path2.toFile());
37+
38+
Files.writeString(path1, "testing line 1" + System.lineSeparator() + "line 2");
39+
Files.writeString(path2, "testing line 1" + System.lineSeparator() + "line 2");
40+
41+
assertTrue(IOUtils.contentEquals(inputStream1, inputStream2));
42+
}
43+
44+
@Test
45+
public void whenFilesDifferent_thenReturnFalse() throws IOException {
46+
47+
InputStream inputStream1 = new FileInputStream(path1.toFile());
48+
InputStream inputStream2 = new FileInputStream(path2.toFile());
49+
50+
Files.writeString(path1, "testing line " + System.lineSeparator() + "line 2");
51+
Files.writeString(path2, "testing line 1" + System.lineSeparator() + "line 2");
52+
53+
assertFalse(IOUtils.contentEquals(inputStream1, inputStream2));
54+
}
55+
56+
@Test
57+
public void whenFilesIdenticalIgnoreEOF_thenReturnTrue() throws IOException {
58+
59+
Files.writeString(path1, "testing line 1 \n line 2");
60+
Files.writeString(path2, "testing line 1 \r\n line 2");
61+
62+
Reader reader1 = new BufferedReader(new FileReader(path1.toFile()));
63+
Reader reader2 = new BufferedReader(new FileReader(path2.toFile()));
64+
65+
assertTrue(IOUtils.contentEqualsIgnoreEOL(reader1, reader2));
66+
}
67+
68+
@Test
69+
public void whenFilesNotIdenticalIgnoreEOF_thenReturnFalse() throws IOException {
70+
71+
Files.writeString(path1, "testing line \n line 2");
72+
Files.writeString(path2, "testing line 1 \r\n line 2");
73+
74+
Reader reader1 = new BufferedReader(new FileReader(path1.toFile()));
75+
Reader reader2 = new BufferedReader(new FileReader(path2.toFile()));
76+
77+
assertFalse(IOUtils.contentEqualsIgnoreEOL(reader1, reader2));
78+
}
79+
80+
@AfterAll
81+
public static void shutDown() {
82+
83+
path1.toFile()
84+
.deleteOnExit();
85+
path2.toFile()
86+
.deleteOnExit();
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.file.content.comparison;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
10+
import org.junit.jupiter.api.BeforeAll;
11+
import org.junit.jupiter.api.Test;
12+
13+
public class CompareByMemoryMappedFilesUnitTest {
14+
15+
public static Path path1 = null;
16+
public static Path path2 = null;
17+
18+
@BeforeAll
19+
public static void setup() throws IOException {
20+
21+
path1 = Files.createTempFile("file1Test", ".txt");
22+
path2 = Files.createTempFile("file2Test", ".txt");
23+
}
24+
25+
@Test
26+
public void whenFilesIdentical_thenReturnTrue() throws IOException {
27+
28+
Files.writeString(path1, "testing line 1" + System.lineSeparator() + "line 2");
29+
Files.writeString(path2, "testing line 1" + System.lineSeparator() + "line 2");
30+
31+
assertTrue(CompareFileContents.compareByMemoryMappedFiles(path1, path2));
32+
}
33+
34+
@Test
35+
public void whenFilesDifferent_thenReturnFalse() throws IOException {
36+
37+
Files.writeString(path1, "testing line " + System.lineSeparator() + "line 2");
38+
Files.writeString(path2, "testing line 1" + System.lineSeparator() + "line 2");
39+
40+
assertFalse(CompareFileContents.compareByMemoryMappedFiles(path1, path2));
41+
}
42+
}

0 commit comments

Comments
 (0)