test/jdk/java/nio/channels/FileChannel/ScatteringRead.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) 2001, 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 4452020 4629048 4638365 4869859
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test FileChannel scattering reads
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    27
 * @run main/othervm ScatteringRead
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.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class ScatteringRead {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static final int NUM_BUFFERS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private static final int BUFFER_CAP = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static final int BIG_BUFFER_CAP = Integer.MAX_VALUE / 3 + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        test1(); // for bug 4452020
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        test2(); // for bug 4629048
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        // Test 3 proves that the system is capable of reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // more than MAX_INT bytes in one shot. But it is unsuitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        // for automated testing because oftentimes less bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // read for various reasons, and this is allowed by the spec.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        // test3(); // for bug 4638365
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static void test1() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        for (int i=0; i<NUM_BUFFERS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            dstBuffers[i] = ByteBuffer.allocateDirect(BUFFER_CAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        File blah = File.createTempFile("blah1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        blah.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        createTestFile(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        FileInputStream fis = new FileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        FileChannel fc = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        byte expectedResult = -128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (int k=0; k<20; k++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            long bytesRead = fc.read(dstBuffers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            for (int i=0; i<NUM_BUFFERS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                for (int j=0; j<BUFFER_CAP; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    byte b = dstBuffers[i].get(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                    if (b != expectedResult++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        throw new RuntimeException("Test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                dstBuffers[i].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private static void createTestFile(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for(int i=-128; i<128; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            fos.write((byte)i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        fos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static void test2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        ByteBuffer dstBuffers[] = new ByteBuffer[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        for (int i=0; i<2; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            dstBuffers[i] = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        File blah = File.createTempFile("blah2", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        blah.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        for(int i=0; i<15; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            fos.write((byte)92);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        fos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        FileInputStream fis = new FileInputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        FileChannel fc = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        long bytesRead = fc.read(dstBuffers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (dstBuffers[1].limit() != 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new Exception("Scattering read changed buf limit.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private static void test3() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // Only works on 64 bit Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!osName.startsWith("SunOS"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        String dataModel = System.getProperty("sun.arch.data.model");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (!dataModel.startsWith("64"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        ByteBuffer dstBuffers[] = new ByteBuffer[NUM_BUFFERS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        File f = File.createTempFile("test3", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        f.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        prepTest3File(f, (long)BIG_BUFFER_CAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        FileChannel fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                      BIG_BUFFER_CAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        for (int i=0; i<NUM_BUFFERS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            dstBuffers[i] = mbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        // Source must be large
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        FileInputStream fis = new FileInputStream("/dev/zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        fc = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        long bytesRead = fc.read(dstBuffers);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (bytesRead <= Integer.MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            throw new RuntimeException("Test 3 failed "+bytesRead+" < "+Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static void prepTest3File(File blah, long testSize) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        RandomAccessFile raf = new RandomAccessFile(blah, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        FileChannel fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
}