test/jdk/java/nio/channels/FileChannel/TruncateRAF.java
author jbhateja
Tue, 26 Nov 2019 16:09:25 +0300
changeset 59278 8375560db76b
parent 50901 9f62267e79df
permissions -rw-r--r--
8234394: C2: Dynamic register class support in ADLC Reviewed-by: vlivanov, sviswanathan, thartmann, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50901
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     1
/*
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     4
 *
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     8
 *
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    13
 * accompanied this code).
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    14
 *
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    18
 *
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    21
 * questions.
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    22
 */
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    23
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    24
/* @test
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    25
 * @bug 8204310
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    26
 * @summary Check how FileChannel behaves if the file size/offset change via
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    27
 *          RAF.setLength() and other methods.
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    28
 * @run main TruncateRAF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    29
 */
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    30
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    31
import java.io.File;
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    32
import java.io.RandomAccessFile;
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    33
import java.io.IOException;
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    34
import java.nio.ByteBuffer;
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    35
import java.nio.channels.FileChannel;
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    36
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    37
public class TruncateRAF {
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    38
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    39
    static void checkState(RandomAccessFile raf, FileChannel fch,
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    40
            long expectedOffset, long expectedLength)
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    41
        throws IOException
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    42
    {
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    43
        long rafLength = raf.length();
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    44
        long rafOffset = raf.getFilePointer();
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    45
        long fchLength = fch.size();
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    46
        long fchOffset = fch.position();
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    47
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    48
        if (rafLength != expectedLength)
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    49
            throw new RuntimeException("rafLength (" + rafLength + ") != " +
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    50
                    "expectedLength (" + expectedLength + ")");
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    51
        if (rafOffset != expectedOffset)
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    52
            throw new RuntimeException("rafOffset (" + rafOffset + ") != " +
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    53
                    "expectedOffset (" + expectedOffset + ")");
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    54
        if (fchLength != expectedLength)
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    55
            throw new RuntimeException("fchLength (" + fchLength + ") != " +
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    56
                    "expectedLength (" + expectedLength + ")");
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    57
        if (fchOffset != expectedOffset)
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    58
            throw new RuntimeException("fchOffset (" + fchOffset + ") != " +
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    59
                    "expectedOffset (" + expectedOffset + ")");
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    60
    }
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    61
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    62
    public static void main(String[] args) throws Throwable {
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    63
        File file = new File("tmp");
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    64
        try (RandomAccessFile raf = new RandomAccessFile(file, "rw");
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    65
             FileChannel fch = raf.getChannel()) {
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    66
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    67
            // initially empty
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    68
            checkState(raf, fch, 0, 0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    69
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    70
            // seeking beyond EOF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    71
            raf.seek(42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    72
            checkState(raf, fch, 42, 0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    73
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    74
            // seeking beyond EOF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    75
            fch.position(84);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    76
            checkState(raf, fch, 84, 0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    77
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    78
            // writing at offset beyond EOF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    79
            raf.write(1);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    80
            checkState(raf, fch, 85, 85);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    81
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    82
            // truncating
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    83
            raf.setLength(63);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    84
            checkState(raf, fch, 63, 63);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    85
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    86
            // writing at EOF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    87
            fch.write(ByteBuffer.wrap(new byte[1]));
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    88
            checkState(raf, fch, 64, 64);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    89
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    90
            // seeking at the middle
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    91
            fch.position(32);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    92
            checkState(raf, fch, 32, 64);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    93
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    94
            // truncating beyond offset
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    95
            fch.truncate(42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    96
            checkState(raf, fch, 32, 42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    97
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    98
            // truncating before offset
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
    99
            fch.truncate(16);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   100
            checkState(raf, fch, 16, 16);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   101
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   102
            // writing at position beyond EOF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   103
            fch.write(ByteBuffer.wrap(new byte[1]), 127);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   104
            checkState(raf, fch, 16, 128);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   105
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   106
            // writing at position before EOF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   107
            fch.write(ByteBuffer.wrap(new byte[1]), 42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   108
            checkState(raf, fch, 16, 128);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   109
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   110
            // truncating
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   111
            raf.setLength(64);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   112
            checkState(raf, fch, 16, 64);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   113
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   114
            // changing offset
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   115
            raf.seek(21);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   116
            checkState(raf, fch, 21, 64);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   117
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   118
            // skipping should change offset
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   119
            raf.skipBytes(4);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   120
            checkState(raf, fch, 25, 64);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   121
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   122
            // reading should change offset
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   123
            raf.read();
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   124
            checkState(raf, fch, 26, 64);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   125
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   126
            // truncating to zero
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   127
            raf.setLength(0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   128
            checkState(raf, fch, 0, 0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   129
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   130
            // FileChannel cannot expand size
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   131
            fch.truncate(42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   132
            checkState(raf, fch, 0, 0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   133
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   134
            // expanding
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   135
            raf.setLength(42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   136
            checkState(raf, fch, 0, 42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   137
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   138
            // seeking beyond EOF
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   139
            raf.seek(512);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   140
            checkState(raf, fch, 512, 42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   141
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   142
            // truncating to the same size
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   143
            fch.truncate(256);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   144
            checkState(raf, fch, 256, 42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   145
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   146
            // truncating to the same size
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   147
            fch.truncate(42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   148
            checkState(raf, fch, 42, 42);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   149
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   150
            // truncating to zero
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   151
            fch.truncate(0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   152
            checkState(raf, fch, 0, 0);
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   153
        }
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   154
    }
9f62267e79df 8204310: Simpler RandomAccessFile.setLength() on Windows
igerasim
parents:
diff changeset
   155
}