test/jdk/java/nio/channels/FileChannel/Pwrite.java
author bpb
Mon, 30 Apr 2018 13:40:39 -0700
changeset 49930 3aaaa5370999
parent 47216 71c04702a3d5
permissions -rw-r--r--
8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4862411
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test positional write method of FileChannel
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 7668
diff changeset
    27
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.CharBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Testing FileChannel's positional write method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class Pwrite {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static File blah;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        genericTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        testUnwritableChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // This test for bug 4862411
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static void testUnwritableChannel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        File blah = File.createTempFile("blah2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        blah.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        fos.write(new byte[128]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        FileInputStream fis = new FileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        FileChannel fc = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            fc.write(ByteBuffer.allocate(256),1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            throw new RuntimeException("Expected exception not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } catch(NonWritableChannelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            // Correct result
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    66
        } finally {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    67
            fc.close();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    68
            blah.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static void genericTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        sb.setLength(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        blah = File.createTempFile("blah", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        blah.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        initTestFile(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        RandomAccessFile raf = new RandomAccessFile(blah, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        FileChannel c = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int x=0; x<100; x++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            long offset = generator.nextInt(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            ByteBuffer bleck = ByteBuffer.allocateDirect(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // Write known sequence out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            for (byte i=0; i<4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                bleck.put(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            bleck.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            long originalPosition = c.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int totalWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            while (totalWritten < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                int written = c.write(bleck, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (written < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    throw new Exception("Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                totalWritten += written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            long newPosition = c.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // Ensure that file pointer position has not changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (originalPosition != newPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw new Exception("File position modified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            // Attempt to read sequence back in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            bleck = ByteBuffer.allocateDirect(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            originalPosition = c.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            while (totalRead < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                int read = c.read(bleck, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                if (read < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                    throw new Exception("Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                totalRead += read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            newPosition = c.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // Ensure that file pointer position has not changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (originalPosition != newPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                throw new Exception("File position modified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            for (byte i=0; i<4; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                if (bleck.get(i) != i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    throw new Exception("Write test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        c.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        raf.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   130
        blah.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Creates file blah:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * 0000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * 0001
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * 0002
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * 0003
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * 3999
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Blah extends beyond a single page of memory so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * ability to index into a file of multiple pages is tested.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private static void initTestFile(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        BufferedWriter awriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        for(int i=0; i<4000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            String number = new Integer(i).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            for (int h=0; h<4-number.length(); h++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                awriter.write("0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            awriter.write(""+i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            awriter.newLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
       awriter.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
       awriter.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}