jdk/test/java/io/RandomAccessFile/FileLengthTest.java
author duke
Wed, 05 Jul 2017 22:06:37 +0200
changeset 40471 ad45133ff99e
parent 34890 0e8281ef3550
child 45028 b0ea3c0bfb81
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34890
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     1
/*
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     4
 *
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     8
 *
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE randomAccessFile that
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    13
 * accompanied this code).
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    14
 *
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    18
 *
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    21
 * questions.
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    22
 */
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    23
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    24
/*
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    25
 * @test
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    26
 * @bug 4823133
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    27
 * @summary optimize RandomAccessFile.length() and length() is thread safe now.
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    28
 */
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    29
import java.io.File;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    30
import java.io.FileNotFoundException;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    31
import java.io.FileOutputStream;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    32
import java.io.IOException;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    33
import java.io.RandomAccessFile;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    34
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    35
/**
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    36
 *
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    37
 * @author vyom.tewari@oracle.com
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    38
 */
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    39
public class FileLengthTest {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    40
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    41
    private static final int BUF_SIZE = 4096;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    42
    private static RandomAccessFile randomAccessFile;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    43
    private static Thread fileLengthCaller;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    44
    private static Thread fileContentReader;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    45
    private static StringBuilder fileContents;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    46
    private static volatile boolean isFailed = false;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    47
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    48
    /**
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    49
     * this thread will call length() in loop
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    50
     */
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    51
    private static void startLengthThread() {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    52
        if (randomAccessFile == null) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    53
            return;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    54
        }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    55
        fileLengthCaller = new Thread(() -> {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    56
            while (true) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    57
                try {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    58
                    long length = randomAccessFile.length();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    59
                    if (length < 0) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    60
                        return;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    61
                    }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    62
                } catch (IOException ex) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    63
                    return;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    64
                }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    65
            }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    66
        });
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    67
        fileLengthCaller.setName("RandomAccessFile-length-caller");
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    68
        fileLengthCaller.setDaemon(true);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    69
        fileLengthCaller.start();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    70
    }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    71
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    72
    /**
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    73
     * this thread will call read() and store the content in internal buffer.
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    74
     */
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    75
    private static void startReaderThread() {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    76
        if (randomAccessFile == null) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    77
            return;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    78
        }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    79
        fileContentReader = new Thread(() -> {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    80
            StringBuilder sb = new StringBuilder(BUF_SIZE);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    81
            int i;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    82
            byte arr[] = new byte[8];
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    83
            try {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    84
                while ((i = randomAccessFile.read(arr)) != -1) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    85
                    sb.append(new String(arr, 0, i));
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    86
                }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    87
                if (!sb.toString().equals(fileContents.toString())) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    88
                    isFailed = true;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    89
                }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    90
            } catch (IOException ex) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    91
            }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    92
        });
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    93
        fileContentReader.setName("RandomAccessFile-content-reader");
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    94
        fileContentReader.setDaemon(true);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    95
        fileContentReader.start();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    96
    }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    97
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    98
    public static void main(String args[]) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
    99
        byte arr[] = new byte[BUF_SIZE];
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   100
        String testFile = "testfile.txt";
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   101
        try {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   102
            createDummyFile(testFile);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   103
            File file = new File(testFile);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   104
            file.deleteOnExit();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   105
            randomAccessFile = new RandomAccessFile(file, "r");
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   106
            int count = randomAccessFile.read(arr);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   107
            randomAccessFile.seek(0);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   108
            fileContents = new StringBuilder(BUF_SIZE);
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   109
            fileContents.append(new String(arr, 0, count));
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   110
            startLengthThread();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   111
            startReaderThread();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   112
            fileContentReader.join();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   113
        } catch (FileNotFoundException | InterruptedException ex) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   114
        } catch (IOException ex) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   115
        } finally {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   116
            try {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   117
                randomAccessFile.close();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   118
            } catch (IOException ex) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   119
            }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   120
        }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   121
        if (isFailed) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   122
            throw new RuntimeException("RandomAccessFile.length() changed the underlying file pointer.");
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   123
        }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   124
    }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   125
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   126
    private static void createDummyFile(String fileName) throws FileNotFoundException, IOException {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   127
        try (FileOutputStream outputStream = new FileOutputStream(new File(fileName))) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   128
            String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   129
            int count = 0;
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   130
            while ((count + str.length()) < BUF_SIZE) {
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   131
                outputStream.write(str.getBytes());
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   132
                count += str.length();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   133
            }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   134
            outputStream.flush();
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   135
        }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   136
    }
0e8281ef3550 4823133: RandomAccessFile.length() is not thread-safe
vtewari
parents:
diff changeset
   137
}