test/jdk/java/nio/channels/FileChannel/FileExtensionAndMap.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42447
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     1
/*
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     4
 *
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     8
 *
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    13
 * accompanied this code).
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    14
 *
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    18
 *
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    21
 * questions.
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    22
 */
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    23
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    24
/* @test
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    25
 * @ignore This test has huge disk space requirements
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    26
 * @bug 8168628
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    27
 * @summary Test extending files to very large sizes without hitting a SIGBUS
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    28
 * @requires (os.family == "linux")
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    29
 * @run main/othervm/timeout=600 -Xms4g -Xmx4g FileExtensionAndMap
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    30
 * @run main/othervm/timeout=600 -Xms4g -Xmx4g FileExtensionAndMap true
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    31
 */
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    32
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    33
import java.io.File;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    34
import java.io.IOException;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    35
import java.io.RandomAccessFile;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    36
import java.nio.MappedByteBuffer;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    37
import java.nio.channels.ClosedChannelException;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    38
import java.nio.channels.FileChannel;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    39
import java.nio.channels.FileChannel.MapMode;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    40
import java.nio.file.Files;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    41
import java.nio.file.Path;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    42
import java.nio.file.Paths;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    43
import java.nio.file.StandardCopyOption;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    44
import java.nio.file.StandardOpenOption;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    45
import java.util.concurrent.ExecutorService;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    46
import java.util.concurrent.Executors;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    47
import java.util.concurrent.ForkJoinPool;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    48
import java.util.concurrent.Semaphore;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    49
import java.util.stream.IntStream;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    50
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    51
public class FileExtensionAndMap {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    52
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    53
    private static final ExecutorService CACHED_EXECUTORSERVICE =
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    54
        Executors.newCachedThreadPool();
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    55
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    56
    private static final String TMPDIR = System.getProperty("test.dir", ".");
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    57
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    58
    private static boolean useRaf = false;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    59
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    60
    public static void main(String args[]) throws Exception {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    61
        if (args.length > 2) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    62
            throw new IllegalArgumentException
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    63
                ("Arguments: [true|false [targetFolder]]");
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    64
        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    65
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    66
        String defaultFolder = TMPDIR + File.separator + "target";
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    67
        if (args.length > 0) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    68
            useRaf = Boolean.valueOf(args[0]);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    69
            if (args.length > 1) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    70
                defaultFolder = args[1];
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    71
            }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    72
        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    73
        final String targetFolder = defaultFolder;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    74
        Path p = Paths.get(targetFolder);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    75
        boolean targetExists = Files.exists(p);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    76
        if (!targetExists) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    77
            Files.createDirectory(p);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    78
        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    79
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    80
        System.out.printf("Using RandomAccessFile: %s; target folder: %s%n",
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    81
            useRaf, targetFolder);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    82
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    83
        ForkJoinPool fjPool = new ForkJoinPool(3);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    84
        fjPool.submit(() -> {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    85
            IntStream.range(0, 20).parallel().forEach((index) -> {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    86
                String fileName = "testBigFile_" + index + ".dat";
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    87
                Path source = null;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    88
                Path target = null;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    89
                try {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    90
                    source = Paths.get(TMPDIR, fileName);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    91
                    testCreateBigFile(source);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    92
                    target = Paths.get(targetFolder, fileName);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    93
                    testFileCopy(source, target);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    94
                } catch (Throwable th) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    95
                    System.err.println("Error copying file with fileName: "
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    96
                        + fileName + " : " + th.getMessage());
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    97
                    th.printStackTrace(System.err);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    98
                } finally {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
    99
                    try {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   100
                        if (source != null) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   101
                            Files.deleteIfExists(source);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   102
                        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   103
                    } catch (Throwable ignored) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   104
                    }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   105
                    try {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   106
                        if (target != null) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   107
                            Files.deleteIfExists(target);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   108
                        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   109
                    } catch (Throwable ignored) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   110
                    }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   111
                }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   112
            });
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   113
        }).join();
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   114
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   115
        if (!targetExists) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   116
            Files.delete(p);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   117
        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   118
    }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   119
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   120
    private static void testFileCopy(Path source, Path target)
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   121
        throws IOException {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   122
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   123
        System.out.println("Finished copying file with fileName: "
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   124
                + source.getFileName());
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   125
    }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   126
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   127
    private static void testCreateBigFile(Path segmentFile)
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   128
        throws IOException {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   129
        final Semaphore concurrencySemaphore = new Semaphore(5);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   130
        long fileSize = 3L * 1024L * 1024L * 1024L;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   131
        int blockSize = 10 * 1024 * 1024;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   132
        int loopCount = (int) Math.floorDiv(fileSize, blockSize);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   133
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   134
        String fileName = segmentFile.getFileName().toString();
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   135
        if (useRaf) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   136
            try (RandomAccessFile raf
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   137
                = new RandomAccessFile(segmentFile.toFile(), "rw")) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   138
                raf.setLength(fileSize);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   139
                try (FileChannel fc = raf.getChannel()) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   140
                    for (int i = 0; i < loopCount; i++) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   141
                        final long startPosition = 1L * blockSize * i;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   142
                        concurrencySemaphore.acquireUninterruptibly();
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   143
                        CACHED_EXECUTORSERVICE.submit(() -> {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   144
                            writeTemplateData(fileName, fc, startPosition,
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   145
                                    blockSize, concurrencySemaphore);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   146
                        });
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   147
                    }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   148
                } finally {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   149
                    concurrencySemaphore.acquireUninterruptibly(5);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   150
                }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   151
            }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   152
        } else {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   153
            Path file = Files.createFile(segmentFile);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   154
            try (FileChannel fc = FileChannel.open(file,
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   155
                StandardOpenOption.READ, StandardOpenOption.WRITE)) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   156
                for (int i = 0; i < loopCount; i++) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   157
                    final long startPosition = 1L * blockSize * i;
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   158
                    concurrencySemaphore.acquireUninterruptibly();
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   159
                    CACHED_EXECUTORSERVICE.submit(() -> {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   160
                        writeTemplateData(fileName, fc, startPosition,
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   161
                                blockSize, concurrencySemaphore);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   162
                    });
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   163
                }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   164
            }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   165
        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   166
    }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   167
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   168
    private static void writeTemplateData(String fileName,
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   169
        FileChannel fc, long startPosition, int blockSize,
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   170
        Semaphore concurrencySemaphore) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   171
        try {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   172
            byte[] EMPTY_RECORD = new byte[blockSize / 256];
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   173
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   174
            MappedByteBuffer mappedByteBuffer = fc.map(MapMode.READ_WRITE,
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   175
                startPosition, blockSize);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   176
            IntStream.range(0, 256).forEach((recordIndex) -> {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   177
                try {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   178
                    mappedByteBuffer.position((int) (recordIndex *
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   179
                        EMPTY_RECORD.length));
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   180
                    mappedByteBuffer.put(EMPTY_RECORD, 0, EMPTY_RECORD.length);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   181
                } catch (Throwable th) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   182
                    System.err.println
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   183
                        ("Error in FileExtensionAndMap.writeTemplateData empty record for fileName: "
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   184
                        + fileName + ", startPosition: " + startPosition + ", recordIndex: "
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   185
                        + recordIndex + " : " + th.getMessage());
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   186
                    th.printStackTrace(System.err);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   187
                }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   188
            });
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   189
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   190
            mappedByteBuffer.force();
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   191
        } catch (Throwable th) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   192
            if (!(th instanceof ClosedChannelException)) {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   193
                System.err.println
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   194
                    ("Error in FileExtensionAndMap.writeTemplateData empty record for fileName: "
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   195
                    + fileName + ", startPosition: " + startPosition + " : "
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   196
                    + th.getMessage());
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   197
                th.printStackTrace(System.err);
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   198
            }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   199
        } finally {
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   200
            concurrencySemaphore.release();
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   201
        }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   202
    }
36db92901ae9 8168628: (fc) SIGBUS when extending file size to map it
bpb
parents:
diff changeset
   203
}