jdk/test/java/nio/channels/FileChannel/Write.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5970 d4e98bbfb0be
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4475533 4698138 4638365 4796221
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test FileChannel write
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    28
 * @run main/othervm Write
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Write {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
   public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
       test1(); // for bug 4475533
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
       test2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
       test3(); // for bug 4698138
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
       // This test is not suitable for automated testing at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
       // I am commenting it out but it will be easy to manually
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
       // test for a regression in this area. See also 4796221.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
       //test4(); // for bug 4638365
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // Test to see that offset > length does not throw exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static void test1() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        ByteBuffer[] dsts = new ByteBuffer[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        for (int i=0; i<4; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            dsts[i] = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    54
        File testFile = File.createTempFile("test1", null);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    55
        try {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    56
            FileOutputStream fos = new FileOutputStream(testFile);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    57
            FileChannel fc = fos.getChannel();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    58
            fc.write(dsts, 2, 1);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    59
            fos.close();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    60
        } finally {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    61
            testFile.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    62
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Test to see that the appropriate buffers are updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static void test2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        File testFile = File.createTempFile("test2", null);
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    68
        testFile.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ByteBuffer[] srcs = new ByteBuffer[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        for (int i=0; i<4; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            srcs[i] = ByteBuffer.allocateDirect(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        srcs[0].put((byte)1); srcs[0].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        srcs[1].put((byte)2); srcs[1].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        srcs[2].put((byte)3); srcs[2].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        srcs[3].put((byte)4); srcs[3].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        FileOutputStream fos = new FileOutputStream(testFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        FileChannel fc = fos.getChannel();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    80
        try {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    81
            fc.write(srcs, 1, 2);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    82
        } finally {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    83
            fc.close();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    84
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        FileInputStream fis = new FileInputStream(testFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        fc = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    89
            ByteBuffer bb = ByteBuffer.allocateDirect(10);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    90
            fc.read(bb);
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    91
            bb.flip();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    92
            if (bb.get() != 2)
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    93
                throw new RuntimeException("Write failure");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    94
            if (bb.get() != 3)
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    95
                throw new RuntimeException("Write failure");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    96
            try {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    97
                bb.get();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    98
                throw new RuntimeException("Write failure");
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    99
            } catch (BufferUnderflowException bufe) {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   100
                // correct result
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   101
            }
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   102
        } finally {
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   103
            fc.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   105
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   106
        // eagerly clean-up
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   107
        testFile.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // Test write to a negative position (bug 4698138).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static void test3() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        File testFile = File.createTempFile("test1", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        testFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ByteBuffer dst = ByteBuffer.allocate(10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        FileOutputStream fos = new FileOutputStream(testFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        FileChannel fc = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            fc.write(dst, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            throw new RuntimeException("Expected IAE not thrown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // Correct result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final int TEST4_NUM_BUFFERS = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final int TEST4_BUF_CAP = Integer.MAX_VALUE / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Test to see that vector write can return > Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Note that under certain circumstances disk space problems occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * with this test. It typically relies upon adequate disk space and/or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * a Solaris disk space optimization where empty files take up less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * space than their logical size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Note that if this test fails it is not necessarily a violation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * spec: the value returned by fc.write can be smaller than the number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * of bytes requested to write. It is testing an optimization that allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * for larger return values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static void test4() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // Only works on 64 bit Solaris
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (!osName.startsWith("SunOS"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        String dataModel = System.getProperty("sun.arch.data.model");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (!dataModel.startsWith("64"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        File testFile = File.createTempFile("test4", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        testFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        FileChannel[] fcs = new FileChannel[TEST4_NUM_BUFFERS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        ByteBuffer[] dsts = new ByteBuffer[TEST4_NUM_BUFFERS];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        // Map these buffers from a file so we don't run out of memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        for (int i=0; i<TEST4_NUM_BUFFERS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            File f = File.createTempFile("test4." + i, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            f.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            prepTest4File(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            FileInputStream fis = new FileInputStream(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            FileChannel fc = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                          TEST4_BUF_CAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            dsts[i] = mbb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        FileOutputStream fos = new FileOutputStream(testFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        FileChannel fc = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            long bytesWritten = fc.write(dsts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (bytesWritten < Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                // Note: this is not a violation of the spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw new RuntimeException("Test 4 failed but wrote " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                                           bytesWritten);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static void prepTest4File(File blah) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        RandomAccessFile raf = new RandomAccessFile(blah, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        FileChannel fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                 TEST4_BUF_CAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}