changeset 13235:b94449779451

Merge
author asaha
date Tue, 08 May 2018 11:54:54 -0700
parents 446351642449 010852525831
children 183336a30729
files .hgtags src/windows/native/sun/windows/awt_Component.cpp
diffstat 13 files changed, 375 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/.hgtags	Tue May 08 10:28:58 2018 -0700
+++ b/.hgtags	Tue May 08 11:54:54 2018 -0700
@@ -857,6 +857,13 @@
 95df717479b19f5ea244afc67434827f2f851287 jdk8u162-b11
 ddae5cb11d6c04130b8002b852bc7f80e0c8bcd2 jdk8u162-b12
 8e40acfcc41a631f5922824712d4336742652eac jdk8u162-b31
+c00bdbbd9a77150f565298af9c305d7e6863eb59 jdk8u162-b32
+70a653814e61a5552312345308b85330fa8f27bc jdk8u162-b33
+f1f949ac13549c6fb3766279848539b124ad835e jdk8u162-b34
+b3de2b1e82fb2427cd40bb230aa26d9b7d8fb09c jdk8u162-b35
+e03fff22900242d92a97839a9c095bb106bdc68f jdk8u162-b36
+19696e8204b0e77e0f10af106c384b7ba6dd5bda jdk8u162-b37
+305c40e81a6fd59699036d4c35d8994f8501f116 jdk8u162-b38
 b6195815c4bbbf275f1aefd337d805eb66f2b5b8 jdk8u171-b00
 f1792a59f1fa20e47fe5d4561754012440564bec jdk8u171-b01
 cac020298633fc736f5e21afddf00145665ef0a7 jdk8u171-b02
@@ -882,6 +889,11 @@
 f52ece1d8708024735f06e7e3bdc771efbc073d0 jdk8u172-b09
 9e9009034e5ce97a97f72c00cd37cf2a638fa1a4 jdk8u172-b10
 2a041b1f858dc6a372ac07b8cf5bf6fab879dcc8 jdk8u172-b11
+f3185b46a35b82727f39d52ac7fad7c4c78ddcdd jdk8u172-b31
+0939503b7477ba081484c54f26f177f9a66d92e4 jdk8u172-b32
+f4b138a6c95cf17f6138598fe04a90dfe1086bf4 jdk8u172-b33
+93b4d31ee70576a45a87607e2f5474d90ba31653 jdk8u172-b34
+6b4e239c74929b86b46113e92fe36d355157e235 jdk8u172-b35
 d902fae6241006af3c4cfc4ce82ebcb3efb9d725 jdk8u181-b01
 baac18e216fb47b4cfa04169b3c3de58d667de7c jdk8u181-b02
 d237c59d14e1c1fb1f750e9cdabcea6e711f4d34 jdk8u181-b03
--- a/make/mapfiles/libnio/mapfile-linux	Tue May 08 10:28:58 2018 -0700
+++ b/make/mapfiles/libnio/mapfile-linux	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -59,6 +59,7 @@
                 Java_sun_nio_ch_FileChannelImpl_position0;
                 Java_sun_nio_ch_FileChannelImpl_transferTo0;
                 Java_sun_nio_ch_FileChannelImpl_unmap0;
+		Java_sun_nio_ch_FileDispatcherImpl_allocate0;
                 Java_sun_nio_ch_FileDispatcherImpl_close0;
                 Java_sun_nio_ch_FileDispatcherImpl_closeIntFD;
 		Java_sun_nio_ch_FileDispatcherImpl_force0;
--- a/make/mapfiles/libnio/mapfile-macosx	Tue May 08 10:28:58 2018 -0700
+++ b/make/mapfiles/libnio/mapfile-macosx	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -42,6 +42,7 @@
                 Java_sun_nio_ch_FileChannelImpl_position0;
                 Java_sun_nio_ch_FileChannelImpl_transferTo0;
                 Java_sun_nio_ch_FileChannelImpl_unmap0;
+		Java_sun_nio_ch_FileDispatcherImpl_allocate0;
                 Java_sun_nio_ch_FileDispatcherImpl_close0;
                 Java_sun_nio_ch_FileDispatcherImpl_closeIntFD;
 		Java_sun_nio_ch_FileDispatcherImpl_force0;
--- a/make/mapfiles/libnio/mapfile-solaris	Tue May 08 10:28:58 2018 -0700
+++ b/make/mapfiles/libnio/mapfile-solaris	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -47,6 +47,7 @@
                 Java_sun_nio_ch_FileChannelImpl_position0;
                 Java_sun_nio_ch_FileChannelImpl_transferTo0;
                 Java_sun_nio_ch_FileChannelImpl_unmap0;
+		Java_sun_nio_ch_FileDispatcherImpl_allocate0;
                 Java_sun_nio_ch_FileDispatcherImpl_close0;
                 Java_sun_nio_ch_FileDispatcherImpl_closeIntFD;
 		Java_sun_nio_ch_FileDispatcherImpl_force0;
--- a/src/share/classes/sun/nio/ch/FileChannelImpl.java	Tue May 08 10:28:58 2018 -0700
+++ b/src/share/classes/sun/nio/ch/FileChannelImpl.java	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -889,57 +889,62 @@
             if (!isOpen())
                 return null;
 
-            long filesize;
-            do {
-                filesize = nd.size(fd);
-            } while ((filesize == IOStatus.INTERRUPTED) && isOpen());
-            if (!isOpen())
-                return null;
-
-            if (filesize < position + size) { // Extend file size
-                if (!writable) {
-                    throw new IOException("Channel not open for writing " +
-                        "- cannot extend file to required size");
-                }
-                int rv;
+            long mapSize;
+            int pagePosition;
+            synchronized (positionLock) {
+                long filesize;
                 do {
-                    rv = nd.truncate(fd, position + size);
-                } while ((rv == IOStatus.INTERRUPTED) && isOpen());
+                    filesize = nd.size(fd);
+                } while ((filesize == IOStatus.INTERRUPTED) && isOpen());
                 if (!isOpen())
                     return null;
-            }
-            if (size == 0) {
-                addr = 0;
-                // a valid file descriptor is not required
-                FileDescriptor dummy = new FileDescriptor();
-                if ((!writable) || (imode == MAP_RO))
-                    return Util.newMappedByteBufferR(0, 0, dummy, null);
-                else
-                    return Util.newMappedByteBuffer(0, 0, dummy, null);
-            }
 
-            int pagePosition = (int)(position % allocationGranularity);
-            long mapPosition = position - pagePosition;
-            long mapSize = size + pagePosition;
-            try {
-                // If no exception was thrown from map0, the address is valid
-                addr = map0(imode, mapPosition, mapSize);
-            } catch (OutOfMemoryError x) {
-                // An OutOfMemoryError may indicate that we've exhausted memory
-                // so force gc and re-attempt map
-                System.gc();
+                if (filesize < position + size) { // Extend file size
+                    if (!writable) {
+                        throw new IOException("Channel not open for writing " +
+                            "- cannot extend file to required size");
+                    }
+                    int rv;
+                    do {
+                        rv = nd.allocate(fd, position + size);
+                    } while ((rv == IOStatus.INTERRUPTED) && isOpen());
+                    if (!isOpen())
+                        return null;
+                }
+
+                if (size == 0) {
+                    addr = 0;
+                    // a valid file descriptor is not required
+                    FileDescriptor dummy = new FileDescriptor();
+                    if ((!writable) || (imode == MAP_RO))
+                        return Util.newMappedByteBufferR(0, 0, dummy, null);
+                    else
+                        return Util.newMappedByteBuffer(0, 0, dummy, null);
+                }
+
+                pagePosition = (int)(position % allocationGranularity);
+                long mapPosition = position - pagePosition;
+                mapSize = size + pagePosition;
                 try {
-                    Thread.sleep(100);
-                } catch (InterruptedException y) {
-                    Thread.currentThread().interrupt();
+                    // If map0 did not throw an exception, the address is valid
+                    addr = map0(imode, mapPosition, mapSize);
+                } catch (OutOfMemoryError x) {
+                    // An OutOfMemoryError may indicate that we've exhausted
+                    // memory so force gc and re-attempt map
+                    System.gc();
+                    try {
+                        Thread.sleep(100);
+                    } catch (InterruptedException y) {
+                        Thread.currentThread().interrupt();
+                    }
+                    try {
+                        addr = map0(imode, mapPosition, mapSize);
+                    } catch (OutOfMemoryError y) {
+                        // After a second OOME, fail
+                        throw new IOException("Map failed", y);
+                    }
                 }
-                try {
-                    addr = map0(imode, mapPosition, mapSize);
-                } catch (OutOfMemoryError y) {
-                    // After a second OOME, fail
-                    throw new IOException("Map failed", y);
-                }
-            }
+            } // synchronized
 
             // On Windows, and potentially other platforms, we need an open
             // file descriptor for some mapping operations.
--- a/src/share/classes/sun/nio/ch/FileDispatcher.java	Tue May 08 10:28:58 2018 -0700
+++ b/src/share/classes/sun/nio/ch/FileDispatcher.java	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -40,6 +40,8 @@
 
     abstract int truncate(FileDescriptor fd, long size) throws IOException;
 
+    abstract int allocate(FileDescriptor fd, long size) throws IOException;
+
     abstract long size(FileDescriptor fd) throws IOException;
 
     abstract int lock(FileDescriptor fd, boolean blocking, long pos, long size,
--- a/src/share/classes/sun/security/krb5/internal/rcache/AuthList.java	Tue May 08 10:28:58 2018 -0700
+++ b/src/share/classes/sun/security/krb5/internal/rcache/AuthList.java	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -55,6 +55,9 @@
     private final LinkedList<AuthTimeWithHash> entries;
     private final int lifespan;
 
+    // entries.getLast().ctime, updated after each cleanup.
+    private volatile int oldestTime = Integer.MIN_VALUE;
+
     /**
      * Constructs a AuthList.
      */
@@ -67,11 +70,13 @@
      * Puts the authenticator timestamp into the cache in descending order,
      * and throw an exception if it's already there.
      */
-    public void put(AuthTimeWithHash t, KerberosTime currentTime)
+    public synchronized void put(AuthTimeWithHash t, KerberosTime currentTime)
             throws KrbApErrException {
 
         if (entries.isEmpty()) {
             entries.addFirst(t);
+            oldestTime = t.ctime;
+            return;
         } else {
             AuthTimeWithHash temp = entries.getFirst();
             int cmp = temp.compareTo(t);
@@ -106,24 +111,26 @@
 
         // let us cleanup while we are here
         long timeLimit = currentTime.getSeconds() - lifespan;
-        ListIterator<AuthTimeWithHash> it = entries.listIterator(0);
-        AuthTimeWithHash temp = null;
-        int index = -1;
-        while (it.hasNext()) {
-            // search expired timestamps.
-            temp = it.next();
-            if (temp.ctime < timeLimit) {
-                index = entries.indexOf(temp);
-                break;
+
+        // Only trigger a cleanup when the earliest entry is
+        // lifespan + 5 sec ago. This ensures a cleanup is done
+        // at most every 5 seconds so that we don't always
+        // addLast(removeLast).
+        if (oldestTime > timeLimit - 5) {
+            return;
+        }
+
+        // and we remove the *enough* old ones (1 lifetime ago)
+        while (!entries.isEmpty()) {
+            AuthTimeWithHash removed = entries.removeLast();
+            if (removed.ctime >= timeLimit) {
+                entries.addLast(removed);
+                oldestTime = removed.ctime;
+                return;
             }
         }
-        // It would be nice if LinkedList has a method called truncate(index).
-        if (index > -1) {
-            do {
-                // remove expired timestamps from the list.
-                entries.removeLast();
-            } while(entries.size() > index);
-        }
+
+        oldestTime = Integer.MIN_VALUE;
     }
 
     public boolean isEmpty() {
--- a/src/share/classes/sun/security/krb5/internal/rcache/MemoryCache.java	Tue May 08 10:28:58 2018 -0700
+++ b/src/share/classes/sun/security/krb5/internal/rcache/MemoryCache.java	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,7 +31,9 @@
 
 package sun.security.krb5.internal.rcache;
 
-import java.util.*;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
 import sun.security.krb5.internal.KerberosTime;
 import sun.security.krb5.internal.KrbApErrException;
 import sun.security.krb5.internal.ReplayCache;
@@ -48,31 +50,18 @@
     private static final int lifespan = KerberosTime.getDefaultSkew();
     private static final boolean DEBUG = sun.security.krb5.internal.Krb5.DEBUG;
 
-    private final Map<String,AuthList> content = new HashMap<>();
+    private final Map<String,AuthList> content = new ConcurrentHashMap<>();
 
     @Override
     public synchronized void checkAndStore(KerberosTime currTime, AuthTimeWithHash time)
             throws KrbApErrException {
         String key = time.client + "|" + time.server;
-        AuthList rc = content.get(key);
+        content.computeIfAbsent(key, k -> new AuthList(lifespan))
+                .put(time, currTime);
         if (DEBUG) {
             System.out.println("MemoryCache: add " + time + " to " + key);
         }
-        if (rc == null) {
-            rc = new AuthList(lifespan);
-            rc.put(time, currTime);
-            if (!rc.isEmpty()) {
-                content.put(key, rc);
-            }
-        } else {
-            if (DEBUG) {
-                System.out.println("MemoryCache: Existing AuthList:\n" + rc);
-            }
-            rc.put(time, currTime);
-            if (rc.isEmpty()) {
-                content.remove(key);
-            }
-        }
+        // TODO: clean up AuthList entries with only expired AuthTimeWithHash objects.
     }
 
     public String toString() {
--- a/src/solaris/classes/sun/nio/ch/FileDispatcherImpl.java	Tue May 08 10:28:58 2018 -0700
+++ b/src/solaris/classes/sun/nio/ch/FileDispatcherImpl.java	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -80,6 +80,10 @@
         return truncate0(fd, size);
     }
 
+    int allocate(FileDescriptor fd, long size) throws IOException {
+        return allocate0(fd, size);
+    }
+
     long size(FileDescriptor fd) throws IOException {
         return size0(fd);
     }
@@ -142,6 +146,9 @@
     static native int truncate0(FileDescriptor fd, long size)
         throws IOException;
 
+    static native int allocate0(FileDescriptor fd, long size)
+        throws IOException;
+
     static native long size0(FileDescriptor fd) throws IOException;
 
     static native int lock0(FileDescriptor fd, boolean blocking, long pos,
--- a/src/solaris/native/java/io/io_util_md.c	Tue May 08 10:28:58 2018 -0700
+++ b/src/solaris/native/java/io/io_util_md.c	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,10 @@
  * questions.
  */
 
+#if defined(__linux__)
+#define _FILE_OFFSET_BITS 64
+#endif
+
 #include "jni.h"
 #include "jni_util.h"
 #include "jvm.h"
@@ -208,6 +212,25 @@
 handleSetLength(FD fd, jlong length)
 {
     int result;
+#if defined(__linux__)
+    /*
+     * On Linux, if the file size is being increased, then ftruncate64()
+     * will modify the metadata value of the size without actually allocating
+     * any blocks which can cause a SIGBUS error if the file is subsequently
+     * memory-mapped.
+     */
+    struct stat64 sb;
+
+    if (fstat64(fd, &sb) == 0 && length > sb.st_blocks*512) {
+        RESTARTABLE(posix_fallocate(fd, 0, length), result);
+        // Return on success or if errno is neither EOPNOTSUPP nor ENOSYS
+        if (result == 0) {
+            return 0;
+        } else if (errno != EOPNOTSUPP && errno != ENOSYS) {
+            return result;
+        }
+    }
+#endif
     RESTARTABLE(ftruncate64(fd, length), result);
     return result;
 }
--- a/src/solaris/native/sun/nio/ch/FileDispatcherImpl.c	Tue May 08 10:28:58 2018 -0700
+++ b/src/solaris/native/sun/nio/ch/FileDispatcherImpl.c	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,6 +23,10 @@
  * questions.
  */
 
+#if defined(__linux__)
+#define _FILE_OFFSET_BITS 64
+#endif
+
 #include "jni.h"
 #include "jni_util.h"
 #include "jvm.h"
@@ -178,6 +182,30 @@
                   "Truncation failed");
 }
 
+JNIEXPORT jint JNICALL
+Java_sun_nio_ch_FileDispatcherImpl_allocate0(JNIEnv *env, jobject this,
+                                             jobject fdo, jlong size)
+{
+#if defined(__linux__)
+    /*
+     * On Linux, if the file size is being increased, then ftruncate64()
+     * will modify the metadata value of the size without actually allocating
+     * any blocks which can cause a SIGBUS error if the file is subsequently
+     * memory-mapped.
+     */
+    // Return on success or if errno is neither EOPNOTSUPP nor ENOSYS
+    int result = posix_fallocate(fdval(env, fdo), 0, size);
+    if (result == 0) {
+        return 0;
+    } else if (errno != EOPNOTSUPP && errno != ENOSYS) {
+        return handle(env, result, "Allocation failed");
+    }
+#endif
+    return handle(env,
+                  ftruncate64(fdval(env, fdo), size),
+                  "Truncation failed");
+}
+
 JNIEXPORT jlong JNICALL
 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
 {
--- a/src/windows/classes/sun/nio/ch/FileDispatcherImpl.java	Tue May 08 10:28:58 2018 -0700
+++ b/src/windows/classes/sun/nio/ch/FileDispatcherImpl.java	Tue May 08 11:54:54 2018 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -93,6 +93,11 @@
         return truncate0(fd, size);
     }
 
+    int allocate(FileDescriptor fd, long size) throws IOException {
+        // truncate0() works for extending and truncating file size
+        return truncate0(fd, size);
+    }
+
     long size(FileDescriptor fd) throws IOException {
         return size0(fd);
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/java/nio/channels/FileChannel/FileExtensionAndMap.java	Tue May 08 11:54:54 2018 -0700
@@ -0,0 +1,203 @@
+/*
+ * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ * @ignore This test has huge disk space requirements
+ * @bug 8168628
+ * @summary Test extending files to very large sizes without hitting a SIGBUS
+ * @requires (os.family == "linux")
+ * @run main/othervm/timeout=600 -Xms4g -Xmx4g FileExtensionAndMap
+ * @run main/othervm/timeout=600 -Xms4g -Xmx4g FileExtensionAndMap true
+ */
+
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.nio.MappedByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileChannel.MapMode;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.StandardOpenOption;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.Semaphore;
+import java.util.stream.IntStream;
+
+public class FileExtensionAndMap {
+
+    private static final ExecutorService CACHED_EXECUTORSERVICE =
+        Executors.newCachedThreadPool();
+
+    private static final String TMPDIR = System.getProperty("test.dir", ".");
+
+    private static boolean useRaf = false;
+
+    public static void main(String args[]) throws Exception {
+        if (args.length > 2) {
+            throw new IllegalArgumentException
+                ("Arguments: [true|false [targetFolder]]");
+        }
+
+        String defaultFolder = TMPDIR + File.separator + "target";
+        if (args.length > 0) {
+            useRaf = Boolean.valueOf(args[0]);
+            if (args.length > 1) {
+                defaultFolder = args[1];
+            }
+        }
+        final String targetFolder = defaultFolder;
+        Path p = Paths.get(targetFolder);
+        boolean targetExists = Files.exists(p);
+        if (!targetExists) {
+            Files.createDirectory(p);
+        }
+
+        System.out.printf("Using RandomAccessFile: %s; target folder: %s%n",
+            useRaf, targetFolder);
+
+        ForkJoinPool fjPool = new ForkJoinPool(3);
+        fjPool.submit(() -> {
+            IntStream.range(0, 20).parallel().forEach((index) -> {
+                String fileName = "testBigFile_" + index + ".dat";
+                Path source = null;
+                Path target = null;
+                try {
+                    source = Paths.get(TMPDIR, fileName);
+                    testCreateBigFile(source);
+                    target = Paths.get(targetFolder, fileName);
+                    testFileCopy(source, target);
+                } catch (Throwable th) {
+                    System.err.println("Error copying file with fileName: "
+                        + fileName + " : " + th.getMessage());
+                    th.printStackTrace(System.err);
+                } finally {
+                    try {
+                        if (source != null) {
+                            Files.deleteIfExists(source);
+                        }
+                    } catch (Throwable ignored) {
+                    }
+                    try {
+                        if (target != null) {
+                            Files.deleteIfExists(target);
+                        }
+                    } catch (Throwable ignored) {
+                    }
+                }
+            });
+        }).join();
+
+        if (!targetExists) {
+            Files.delete(p);
+        }
+    }
+
+    private static void testFileCopy(Path source, Path target)
+        throws IOException {
+        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
+        System.out.println("Finished copying file with fileName: "
+                + source.getFileName());
+    }
+
+    private static void testCreateBigFile(Path segmentFile)
+        throws IOException {
+        final Semaphore concurrencySemaphore = new Semaphore(5);
+        long fileSize = 3L * 1024L * 1024L * 1024L;
+        int blockSize = 10 * 1024 * 1024;
+        int loopCount = (int) Math.floorDiv(fileSize, blockSize);
+
+        String fileName = segmentFile.getFileName().toString();
+        if (useRaf) {
+            try (RandomAccessFile raf
+                = new RandomAccessFile(segmentFile.toFile(), "rw")) {
+                raf.setLength(fileSize);
+                try (FileChannel fc = raf.getChannel()) {
+                    for (int i = 0; i < loopCount; i++) {
+                        final long startPosition = 1L * blockSize * i;
+                        concurrencySemaphore.acquireUninterruptibly();
+                        CACHED_EXECUTORSERVICE.submit(() -> {
+                            writeTemplateData(fileName, fc, startPosition,
+                                    blockSize, concurrencySemaphore);
+                        });
+                    }
+                } finally {
+                    concurrencySemaphore.acquireUninterruptibly(5);
+                }
+            }
+        } else {
+            Path file = Files.createFile(segmentFile);
+            try (FileChannel fc = FileChannel.open(file,
+                StandardOpenOption.READ, StandardOpenOption.WRITE)) {
+                for (int i = 0; i < loopCount; i++) {
+                    final long startPosition = 1L * blockSize * i;
+                    concurrencySemaphore.acquireUninterruptibly();
+                    CACHED_EXECUTORSERVICE.submit(() -> {
+                        writeTemplateData(fileName, fc, startPosition,
+                                blockSize, concurrencySemaphore);
+                    });
+                }
+            }
+        }
+    }
+
+    private static void writeTemplateData(String fileName,
+        FileChannel fc, long startPosition, int blockSize,
+        Semaphore concurrencySemaphore) {
+        try {
+            byte[] EMPTY_RECORD = new byte[blockSize / 256];
+
+            MappedByteBuffer mappedByteBuffer = fc.map(MapMode.READ_WRITE,
+                startPosition, blockSize);
+            IntStream.range(0, 256).forEach((recordIndex) -> {
+                try {
+                    mappedByteBuffer.position((int) (recordIndex *
+                        EMPTY_RECORD.length));
+                    mappedByteBuffer.put(EMPTY_RECORD, 0, EMPTY_RECORD.length);
+                } catch (Throwable th) {
+                    System.err.println
+                        ("Error in FileExtensionAndMap.writeTemplateData empty record for fileName: "
+                        + fileName + ", startPosition: " + startPosition + ", recordIndex: "
+                        + recordIndex + " : " + th.getMessage());
+                    th.printStackTrace(System.err);
+                }
+            });
+
+            mappedByteBuffer.force();
+        } catch (Throwable th) {
+            if (!(th instanceof ClosedChannelException)) {
+                System.err.println
+                    ("Error in FileExtensionAndMap.writeTemplateData empty record for fileName: "
+                    + fileName + ", startPosition: " + startPosition + " : "
+                    + th.getMessage());
+                th.printStackTrace(System.err);
+            }
+        } finally {
+            concurrencySemaphore.release();
+        }
+    }
+}