jdk/test/java/nio/channels/FileChannel/Transfer.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8158 77d9c0f1c19f
child 12047 320a714614e9
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8158
diff changeset
     2
 * Copyright (c) 2001, 2011, 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: 4234
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4234
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 4434723 4482726 4559072 4638365 4795550 5081340 5103988 6253145
6545
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
    26
 *   6984545
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test FileChannel.transferFrom and transferTo
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @library ..
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.channels.spi.SelectorProvider;
5977
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
    36
import java.nio.file.StandardOpenOption;
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
    37
import java.nio.file.FileAlreadyExistsException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class Transfer {
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 int[] testSizes = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        testFileChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        for (int i=0; i<testSizes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            testReadableByteChannel(testSizes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        xferTest02(); // for bug 4482726
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        xferTest03(); // for bug 4559072
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        xferTest04(); // for bug 4638365
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        xferTest05(); // for bug 4638365
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        xferTest06(); // for bug 5081340
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        xferTest07(); // for bug 5103988
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        xferTest08(); // for bug 6253145
6545
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
    59
        xferTest09(); // for bug 6984545
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static void testFileChannel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        File sink = File.createTempFile("sink", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        sink.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        FileOutputStream fos = new FileOutputStream(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        FileChannel sourceChannel = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        sourceChannel.write(ByteBuffer.wrap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            "Use the source, Luke!".getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        FileInputStream fis = new FileInputStream(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        sourceChannel = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        RandomAccessFile raf = new RandomAccessFile(sink, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        FileChannel sinkChannel = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        long oldSinkPosition = sinkChannel.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        long oldSourcePosition = sourceChannel.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (bytesWritten != 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new RuntimeException("Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (sourceChannel.position() == oldSourcePosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            throw new RuntimeException("Source position didn't change");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (sinkChannel.position() != oldSinkPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new RuntimeException("Sink position changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (sinkChannel.size() != 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new RuntimeException("Unexpected sink size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (bytesWritten > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new RuntimeException("Wrote past file size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        sinkChannel.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   102
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   103
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   104
        sink.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private static void testReadableByteChannel(int size) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        SelectorProvider sp = SelectorProvider.provider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        Pipe p = sp.openPipe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Pipe.SinkChannel sink = p.sink();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        Pipe.SourceChannel source = p.source();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        sink.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        byte[] someBytes = new byte[size + 10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        generator.nextBytes(someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        outgoingdata.put(someBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        outgoingdata.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int totalWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        while (totalWritten < size + 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            int written = sink.write(outgoingdata);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (written < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                throw new Exception("Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            totalWritten += written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        File f = File.createTempFile("blah"+size, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        f.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        FileChannel fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        long oldPosition = fc.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        long bytesWritten = fc.transferFrom(source, 0, size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        fc.force(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (bytesWritten != size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            throw new RuntimeException("Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (fc.position() != oldPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new RuntimeException("Position changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (fc.size() != size)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new RuntimeException("Unexpected sink size "+ fc.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        sink.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        source.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   148
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   149
        f.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static void xferTest02() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        byte[] srcData = new byte[5000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        for (int i=0; i<5000; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            srcData[i] = (byte)generator.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // get filechannel for the source file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        FileChannel fc1 = raf1.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        // write out data to the file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        long bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        while (bytesWritten < 5000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // get filechannel for the dst file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        File dest = File.createTempFile("dest", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        dest.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        FileChannel fc2 = raf2.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        int bytesToWrite = 3000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int startPosition = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        fc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        fc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        raf1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        raf2.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   184
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   185
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   186
        dest.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static void xferTest03() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        byte[] srcData = new byte[] {1,2,3,4} ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // get filechannel for the source file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        FileChannel fc1 = raf1.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        fc1.truncate(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // write out data to the file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        while (bytesWritten < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        // get filechannel for the dst file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        File dest = File.createTempFile("dest", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        dest.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        FileChannel fc2 = raf2.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        fc2.truncate(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        fc1.transferTo(0, srcData.length + 1, fc2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (fc2.size() > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            throw new Exception("xferTest03 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        fc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        fc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        raf1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        raf2.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   221
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   222
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   223
        dest.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    // Test transferTo with large file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public static void xferTest04() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        // Windows and Linux can't handle the really large file sizes for a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // truncate or a positional write required by the test for 4563125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (!osName.startsWith("SunOS"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        File source = File.createTempFile("blah", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        long testSize = ((long)Integer.MAX_VALUE) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        initTestFile(source, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        RandomAccessFile raf = new RandomAccessFile(source, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        FileChannel fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        File sink = File.createTempFile("sink", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        sink.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        FileInputStream fis = new FileInputStream(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        FileChannel sourceChannel = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        raf = new RandomAccessFile(sink, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        FileChannel sinkChannel = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        long bytesWritten = sourceChannel.transferTo(testSize -40, 10,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                                     sinkChannel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        if (bytesWritten != 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new RuntimeException("Transfer test 4 failed " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                                       bytesWritten);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        sinkChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   262
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   263
        sink.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // Test transferFrom with large file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static void xferTest05() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Create a source file & large sink file for the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        File source = File.createTempFile("blech", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        initTestFile(source, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
5977
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   273
        // Create the sink file as a sparse file if possible
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   274
        File sink = null;
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   275
        FileChannel fc = null;
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   276
        while (fc == null) {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   277
            sink = File.createTempFile("sink", null);
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   278
            // re-create as a sparse file
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   279
            sink.delete();
5977
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   280
            try {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   281
                fc = FileChannel.open(sink.toPath(),
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   282
                                      StandardOpenOption.CREATE_NEW,
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   283
                                      StandardOpenOption.WRITE,
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   284
                                      StandardOpenOption.SPARSE);
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   285
            } catch (FileAlreadyExistsException ignore) {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   286
                // someone else got it
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   287
            }
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   288
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        sink.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        long testSize = ((long)Integer.MAX_VALUE) * 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                     testSize - 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // Can't set up the test, abort it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            System.err.println("xferTest05 was aborted.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        // Get new channels for the source and sink and attempt transfer
5977
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   304
        FileChannel sourceChannel = new FileInputStream(source).getChannel();
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   305
        try {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   306
            FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   307
            try {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   308
                long bytesWritten = sinkChannel.transferFrom(sourceChannel,
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   309
                                                             testSize - 40, 10);
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   310
                if (bytesWritten != 10) {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   311
                    throw new RuntimeException("Transfer test 5 failed " +
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   312
                                               bytesWritten);
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   313
                }
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   314
            } finally {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   315
                sinkChannel.close();
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   316
            }
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   317
        } finally {
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   318
            sourceChannel.close();
7fd96e483d69 6963828: TEST_BUG: java/nio/channels/FileTransfer.java takes too long (win)
alanb
parents: 5970
diff changeset
   319
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   321
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   322
        sink.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    static void checkFileData(File file, String expected) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        FileInputStream fis = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        while ((c = r.read()) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            sb.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        String contents = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        if (! contents.equals(expected))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            throw new Exception("expected: " + expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                                + ", got: " + contents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    // Test transferFrom asking for more bytes than remain in source
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public static void xferTest06() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        String data = "Use the source, Luke!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        File sink = File.createTempFile("sink", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        sink.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        FileOutputStream fos = new FileOutputStream(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        fos.write(data.getBytes("ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        FileChannel sourceChannel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            new RandomAccessFile(source, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        sourceChannel.position(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        long remaining = sourceChannel.size() - sourceChannel.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        FileChannel sinkChannel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            new RandomAccessFile(sink, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        long n = sinkChannel.transferFrom(sourceChannel, 0L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                                          sourceChannel.size()); // overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        if (n != remaining)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throw new Exception("n == " + n + ", remaining == " + remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        sinkChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        checkFileData(source, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        checkFileData(sink, data.substring(7,data.length()));
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   368
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   369
        source.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    // Test transferTo to non-blocking socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    public static void xferTest07() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        FileChannel sourceChannel = new RandomAccessFile(source, "rw")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            .getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        sourceChannel.position(32000L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            .write(ByteBuffer.wrap("The End".getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        // The sink is a non-blocking socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        InetSocketAddress sa = new InetSocketAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            InetAddress.getLocalHost(), ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        SocketChannel sink = SocketChannel.open(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        sink.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        SocketChannel other = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        long size = sourceChannel.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // keep sending until congested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        long n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            n = sourceChannel.transferTo(0, size, sink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        sink.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        other.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        ssc.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   403
        source.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    // Test transferTo with file positions larger than 2 and 4GB
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    public static void xferTest08() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        // Creating a sparse 6GB file on Windows takes too long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        String osName = System.getProperty("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (osName.startsWith("Windows"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        final long G = 1024L * 1024L * 1024L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        // Create 6GB file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        File file = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        file.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        FileChannel fc = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            fc.write(ByteBuffer.wrap("0123456789012345".getBytes("UTF-8")), 6*G);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            System.err.println("Unable to create test file:" + x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            fc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        // Setup looback connection and echo server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        InetAddress lh = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        InetSocketAddress isa = new InetSocketAddress(lh, ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        SocketChannel source = SocketChannel.open(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        SocketChannel sink = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        Thread thr = new Thread(new EchoServer(sink));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        thr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        // Test data is array of positions and counts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        long testdata[][] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            { 2*G-1,    1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            { 2*G-1,    10 },       // across 2GB boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            { 2*G,      1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            { 2*G,      10 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            { 2*G+1,    1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            { 4*G-1,    1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            { 4*G-1,    10 },       // across 4GB boundary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            { 4*G,      1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            { 4*G,      10 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            { 4*G+1,    1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            { 5*G-1,    1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            { 5*G-1,    10 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            { 5*G,      1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            { 5*G,      10 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            { 5*G+1,    1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            { 6*G,      1 },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        ByteBuffer sendbuf = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        ByteBuffer readbuf = ByteBuffer.allocateDirect(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            byte value = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            for (int i=0; i<testdata.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                long position = testdata[(int)i][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                long count = testdata[(int)i][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                // generate bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                for (long j=0; j<count; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    sendbuf.put(++value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                sendbuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // write to file and transfer to echo server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                fc.write(sendbuf, position);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                fc.transferTo(position, count, source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                // read from echo server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                long nread = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                while (nread < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    int n = source.read(readbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    if (n < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                        throw new RuntimeException("Premature EOF!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    nread += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                // check reply from echo server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                readbuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                sendbuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (!readbuf.equals(sendbuf))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    throw new RuntimeException("Echo'ed bytes do not match!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                readbuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                sendbuf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            source.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            fc.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   506
            file.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
6545
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   510
    // Test that transferFrom with FileChannel source that is not readable
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   511
    // throws NonReadableChannelException
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   512
    static void xferTest09() throws Exception {
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   513
        File source = File.createTempFile("source", null);
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   514
        source.deleteOnExit();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   515
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   516
        File target = File.createTempFile("target", null);
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   517
        target.deleteOnExit();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   518
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   519
        FileChannel fc1 = new FileOutputStream(source).getChannel();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   520
        FileChannel fc2 = new RandomAccessFile(target, "rw").getChannel();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   521
        try {
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   522
            fc2.transferFrom(fc1, 0L, 0);
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   523
            throw new RuntimeException("NonReadableChannelException expected");
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   524
        } catch (NonReadableChannelException expected) {
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   525
        } finally {
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   526
            fc1.close();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   527
            fc2.close();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   528
        }
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   529
    }
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   530
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * Creates file blah of specified size in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    private static void initTestFile(File blah, long size) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (blah.exists())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            blah.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        FileOutputStream fos = new FileOutputStream(blah);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        BufferedWriter awriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        for(int i=0; i<size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            awriter.write("e");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        awriter.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        awriter.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * Simple in-process server to echo bytes read by a given socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    static class EchoServer implements Runnable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        private SocketChannel sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        public EchoServer(SocketChannel sc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            this.sc = sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            ByteBuffer bb = ByteBuffer.allocateDirect(1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    int n = sc.read(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    if (n < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    bb.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    while (bb.remaining() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        sc.write(bb);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    bb.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                } catch (IOException ignore) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
}