diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..0036b29
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,5 @@
+/Chapter01/P01_CountDuplicateCharacters/target/
+/Chapter01/P03_ReverseWords/target/
+/Chapter01/P05_CountVowelsAndConsonants/target/
+/Chapter01/P05_CountVowelsAndConsonants1/target/
+/Chapter01/P02_TextBlockDelimiters/target/
diff --git a/Chapter03/README.md b/Chapter03/README.md
index 0a01b76..9e889c5 100644
--- a/Chapter03/README.md
+++ b/Chapter03/README.md
@@ -1,2 +1,2 @@
# Working with date and time
-This chapter includes 20 problems that involves date and time. These problems are meant to cover a wide range of topics (e.g., converting, formatting, adding, subtracting, defining periods/durations, computing, etc) via **Date**, **Calendar**, **LocalDate**, **LocalTime**, **LocalDateTime**, **ZoneDateTime**, **OffsetDateTime**, **OffsetTime**, **Instant** and so on.
+This chapter includes 20 problems that involves date and time. These problems are meant to cover a wide range of topics (e.g., converting, formatting, adding, subtracting, defining periods/durations, computing, etc) via **Date**, **Calendar**, **LocalDate**, **LocalTime**, **LocalDateTime**, **ZoneDateTime**, **OffsetDateTime**, **OffsetTime**, **Instant** and so on.
diff --git a/Chapter06/BONUS_2_CopyFileBenchmark/nb-configuration.xml b/Chapter06/BONUS_2_CopyFileBenchmark/nb-configuration.xml
new file mode 100644
index 0000000..ec4540c
--- /dev/null
+++ b/Chapter06/BONUS_2_CopyFileBenchmark/nb-configuration.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+ none
+
+
diff --git a/Chapter06/BONUS_2_CopyFileBenchmark/pom.xml b/Chapter06/BONUS_2_CopyFileBenchmark/pom.xml
new file mode 100644
index 0000000..7eb5ecb
--- /dev/null
+++ b/Chapter06/BONUS_2_CopyFileBenchmark/pom.xml
@@ -0,0 +1,72 @@
+
+
+ 4.0.0
+ java.modern.challenge
+ BONUS_2_CopyFileBenchmark
+ 1.0
+ jar
+
+ UTF-8
+ 20
+ 20
+ 3.8.0
+ 1.35
+ 3.10.1
+ 3.1.0
+
+ BONUS_2_CopyFileBenchmark
+
+
+ org.openjdk.jmh
+ jmh-core
+ ${jmh.version}
+
+
+ org.openjdk.jmh
+ jmh-generator-annprocess
+ ${jmh.version}
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${oamp.version}
+
+
+
+ org.openjdk.jmh
+ jmh-generator-annprocess
+ ${jmh.version}
+
+
+
+
+
+ org.codehaus.mojo
+ exec-maven-plugin
+ ${ocm.version}
+
+
+ run-benchmarks
+ integration-test
+
+ exec
+
+
+ test
+ java
+
+ -classpath
+
+ org.openjdk.jmh.Main
+ .*
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Chapter06/BONUS_2_CopyFileBenchmark/rafa_copy/README.txt b/Chapter06/BONUS_2_CopyFileBenchmark/rafa_copy/README.txt
new file mode 100644
index 0000000..ac6d0fc
--- /dev/null
+++ b/Chapter06/BONUS_2_CopyFileBenchmark/rafa_copy/README.txt
@@ -0,0 +1 @@
+In this folder we copy the file used by benchmark.
\ No newline at end of file
diff --git a/Chapter06/BONUS_2_CopyFileBenchmark/rafa_org/Rafa Best Shots.mp4 b/Chapter06/BONUS_2_CopyFileBenchmark/rafa_org/Rafa Best Shots.mp4
new file mode 100644
index 0000000..166777e
Binary files /dev/null and b/Chapter06/BONUS_2_CopyFileBenchmark/rafa_org/Rafa Best Shots.mp4 differ
diff --git a/Chapter06/BONUS_2_CopyFileBenchmark/results/benchmark.png b/Chapter06/BONUS_2_CopyFileBenchmark/results/benchmark.png
new file mode 100644
index 0000000..e91d5bd
Binary files /dev/null and b/Chapter06/BONUS_2_CopyFileBenchmark/results/benchmark.png differ
diff --git a/Chapter06/BONUS_2_CopyFileBenchmark/src/main/java/modern/challenge/Main.java b/Chapter06/BONUS_2_CopyFileBenchmark/src/main/java/modern/challenge/Main.java
new file mode 100644
index 0000000..3f8042d
--- /dev/null
+++ b/Chapter06/BONUS_2_CopyFileBenchmark/src/main/java/modern/challenge/Main.java
@@ -0,0 +1,246 @@
+package modern.challenge;
+
+import java.nio.MappedByteBuffer;
+import java.io.OutputStream;
+import java.io.InputStream;
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+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 static java.nio.file.LinkOption.NOFOLLOW_LINKS;
+import java.util.Random;
+import java.util.concurrent.TimeUnit;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+
+@OutputTimeUnit(TimeUnit.SECONDS)
+@BenchmarkMode(Mode.AverageTime)
+@Fork(value = 1, warmups = 1) //, jvmArgsPrepend = {"-Djdk.net.usePlainSocketImpl=true"})
+@Measurement(iterations = 1, time = 1)
+@State(Scope.Benchmark)
+public class Main {
+
+ private static final Path COPY_FROM = Paths.get("rafa_org/Rafa Best Shots.mp4");
+ private static final Path COPY_TO = Paths.get("rafa_copy/");
+ private static final int BUFFER_SIZE_KB = 4;
+ private static final int BUFFER_SIZE = BUFFER_SIZE_KB * 1024;
+
+ private static final Random rnd = new Random();
+
+ @Benchmark
+ public static void fileChannelIndirectBuffer() {
+
+ System.out.println("Using FileChannel and non-direct buffer ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try (FileChannel fileChannel_from = (FileChannel.open(COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
+ FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
+
+ // Allocate an non-direct ByteBuffer
+ ByteBuffer bytebuffer = ByteBuffer.allocate(BUFFER_SIZE);
+
+ // Read data from file into ByteBuffer
+ while ((fileChannel_from.read(bytebuffer)) > 0) {
+
+ //flip the buffer which set the limit to current position, and position to 0
+ bytebuffer.flip();
+
+ //write data from ByteBuffer to file
+ fileChannel_to.write(bytebuffer);
+
+ //for the next read
+ bytebuffer.clear();
+ }
+ } catch (IOException ex) {
+ System.err.println(ex);
+ }
+ }
+
+ @Benchmark
+ public static void fileChannelDirectBuffer() {
+
+ System.out.println("Using FileChannel and direct buffer ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try (FileChannel fileChannel_from = (FileChannel.open(COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
+ FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
+
+ // Allocate an direct ByteBuffer
+ ByteBuffer bytebuffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
+
+ // Read data from file into ByteBuffer
+ while ((fileChannel_from.read(bytebuffer)) > 0) {
+
+ //flip the buffer which set the limit to current position, and position to 0
+ bytebuffer.flip();
+
+ //write data from ByteBuffer to file
+ fileChannel_to.write(bytebuffer);
+
+ //for the next read
+ bytebuffer.clear();
+ }
+ } catch (IOException ex) {
+ System.err.println(ex);
+ }
+ }
+
+ @Benchmark
+ public static void fileChannelTransferTo() {
+
+ System.out.println("Using FileChannel.transferTo method ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try (FileChannel fileChannel_from = (FileChannel.open(
+ COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
+ FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(
+ StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
+
+ fileChannel_from.transferTo(0L, fileChannel_from.size(), fileChannel_to);
+
+ } catch (IOException ex) {
+ System.err.println(ex);
+ }
+ }
+
+ @Benchmark
+ public static void fileChannelTransferFrom() {
+
+ System.out.println("Using FileChannel.transferFrom method ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try (FileChannel fileChannel_from = (FileChannel.open(
+ COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
+ FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(
+ StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
+
+ fileChannel_to.transferFrom(fileChannel_from, 0L, (int) fileChannel_from.size());
+ } catch (IOException ex) {
+ System.err.println(ex);
+ }
+ }
+
+ @Benchmark
+ public static void fileChannelMap() {
+
+ System.out.println("Using FileChannel.map method ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try (FileChannel fileChannel_from = (FileChannel.open(COPY_FROM, EnumSet.of(StandardOpenOption.READ)));
+ FileChannel fileChannel_to = (FileChannel.open(copyTo, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) {
+
+ MappedByteBuffer buffer = fileChannel_from.map(
+ FileChannel.MapMode.READ_ONLY, 0, fileChannel_from.size());
+
+ fileChannel_to.write(buffer);
+ buffer.clear();
+
+ } catch (IOException ex) {
+ System.err.println(ex);
+ }
+ }
+
+ @Benchmark
+ public static void bufferedStreamIO() {
+
+ System.out.println("Using buffered streams and byte array ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ File inFileStr = COPY_FROM.toFile();
+ File outFileStr = copyTo.toFile();
+
+ try (BufferedInputStream in = new BufferedInputStream(
+ new FileInputStream(inFileStr)); BufferedOutputStream out
+ = new BufferedOutputStream(new FileOutputStream(outFileStr))) {
+
+ byte[] byteArray = new byte[BUFFER_SIZE];
+ int bytesCount;
+ while ((bytesCount = in.read(byteArray)) != -1) {
+ out.write(byteArray, 0, bytesCount);
+ }
+ } catch (IOException ex) {
+ System.err.println(ex);
+ }
+ }
+
+ @Benchmark
+ public static void bufferedStreamByteArray() {
+
+ System.out.println("Using un-buffered streams and byte array ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ File inFileStr = COPY_FROM.toFile();
+ File outFileStr = copyTo.toFile();
+
+ try (FileInputStream in = new FileInputStream(inFileStr);
+ FileOutputStream out = new FileOutputStream(outFileStr)) {
+
+ byte[] byteArray = new byte[BUFFER_SIZE];
+ int bytesCount;
+ while ((bytesCount = in.read(byteArray)) != -1) {
+ out.write(byteArray, 0, bytesCount);
+ }
+ } catch (IOException ex) {
+ System.err.println(ex);
+ }
+ }
+
+ @Benchmark
+ public static void filesCopyPathToPath() {
+
+ System.out.println("Using Files.copy (Path to Path) method ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try {
+ Files.copy(COPY_FROM, copyTo, NOFOLLOW_LINKS);
+ } catch (IOException e) {
+ System.err.println(e);
+ }
+ }
+
+ @Benchmark
+ public static void filesCopyIStreamToPath() {
+
+ System.out.println("Using Files.copy (InputStream to Path) ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try (InputStream is = new FileInputStream(COPY_FROM.toFile())) {
+ Files.copy(is, copyTo);
+ } catch (IOException e) {
+ System.err.println(e);
+ }
+ }
+
+ @Benchmark
+ public static void filesCopyPathToOStream() {
+
+ System.out.println("Using Files.copy (Path to OutputStream) ...");
+
+ Path copyTo = COPY_TO.resolve("Rafa Best Shots " + rnd.nextInt() + ".mp4");
+ try (OutputStream os = new FileOutputStream(copyTo.toFile())) {
+ Files.copy(COPY_FROM, os);
+ } catch (IOException e) {
+ System.err.println(e);
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+
+ org.openjdk.jmh.Main.main(args);
+ }
+}
\ No newline at end of file
diff --git a/Chapter06/README.md b/Chapter06/README.md
index d66c864..8eb91c4 100644
--- a/Chapter06/README.md
+++ b/Chapter06/README.md
@@ -1,3 +1,3 @@
-# Java I/O - Paths, files, buffers, scanning and formatting
+# Java I/O - Paths, files, buffers, scanning and formatting
This chapter includes 20 problems that involve Java I/O for files. From manipulating, walking and watching paths to streaming files and efficient ways for reading/writing text and binary files, we will cover a hand of problems that are a must in the arsenal of any Java developer.
diff --git a/README.md b/README.md
index 0419ea5..aab522f 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,9 @@
+
+
+
# Java Coding Problems
-
+
This is the code repository for [Java Coding Problems ](https://www.packtpub.com/programming/java-coding-problems?utm_source=github&utm_medium=repository&utm_campaign=), published by Packt.
@@ -68,3 +71,7 @@ He is the author of several books, videos, and dozens of articles related to Jav
[Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions.
+### Download a free PDF
+
+ If you have already purchased a print or Kindle version of this book, you can get a DRM-free PDF version at no cost.
Simply click on the link to claim your free PDF.
+
https://packt.link/free-ebook/9781789801415