test/jdk/java/nio/channels/FileChannel/Transfer.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
/*
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
     2
 * Copyright (c) 2001, 2017, 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
44273
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents: 44266
diff changeset
    25
 * @bug 4434723 4482726 4559072 4795550 5081340 5103988 6984545
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    26
 * @summary Test FileChannel.transferFrom and transferTo (use -Dseed=X to set PRNG seed)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @library ..
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 44857
diff changeset
    28
 * @library /test/lib
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 45288
diff changeset
    29
 * @build jdk.test.lib.RandomFactory
44266
2b9cc0e5e79f 8176846: (fc) Increase timeouts of and instrument some tests using FileChannel#write
bpb
parents: 44107
diff changeset
    30
 * @run testng/timeout=300 Transfer
30046
cf2c86e1819e 8078334: Mark regression tests using randomness
darcy
parents: 12538
diff changeset
    31
 * @key randomness
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    34
import java.io.BufferedReader;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    35
import java.io.File;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    36
import java.io.FileInputStream;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    37
import java.io.FileOutputStream;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    38
import java.io.InputStreamReader;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    39
import java.io.IOException;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    40
import java.io.RandomAccessFile;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    41
import java.io.Reader;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    42
import java.net.InetAddress;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    43
import java.net.InetSocketAddress;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    44
import java.nio.ByteBuffer;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    45
import java.nio.channels.FileChannel;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    46
import java.nio.channels.NonReadableChannelException;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    47
import java.nio.channels.Pipe;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    48
import java.nio.channels.ServerSocketChannel;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    49
import java.nio.channels.SocketChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.nio.channels.spi.SelectorProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.Random;
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    52
import java.util.concurrent.TimeUnit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
45288
58be10a068c2 8180805: move RandomFactory to the top level testlibrary
iignatyev
parents: 44857
diff changeset
    54
import jdk.test.lib.RandomFactory;
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    55
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    56
import org.testng.annotations.Test;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class Transfer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    60
    private static Random generator = RandomFactory.getRandom();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    62
    @Test
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
    63
    public void testFileChannel() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        File sink = File.createTempFile("sink", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        sink.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        FileOutputStream fos = new FileOutputStream(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        FileChannel sourceChannel = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        sourceChannel.write(ByteBuffer.wrap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            "Use the source, Luke!".getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        FileInputStream fis = new FileInputStream(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        sourceChannel = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        RandomAccessFile raf = new RandomAccessFile(sink, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        FileChannel sinkChannel = raf.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        long oldSinkPosition = sinkChannel.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        long oldSourcePosition = sourceChannel.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (bytesWritten != 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new RuntimeException("Transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (sourceChannel.position() == oldSourcePosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new RuntimeException("Source position didn't change");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (sinkChannel.position() != oldSinkPosition)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new RuntimeException("Sink position changed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (sinkChannel.size() != 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new RuntimeException("Unexpected sink size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (bytesWritten > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new RuntimeException("Wrote past file size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        sinkChannel.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   103
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   104
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   105
        sink.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   108
    @Test
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   109
    public void testReadableByteChannel() throws Exception {
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   110
        int[] testSizes = { 0, 10, 1023, 1024, 1025, 2047, 2048, 2049 };
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   112
        for (int size : testSizes) {
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   113
            SelectorProvider sp = SelectorProvider.provider();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   114
            Pipe p = sp.openPipe();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   115
            Pipe.SinkChannel sink = p.sink();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   116
            Pipe.SourceChannel source = p.source();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   117
            sink.configureBlocking(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   119
            ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   120
            byte[] someBytes = new byte[size + 10];
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   121
            generator.nextBytes(someBytes);
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   122
            outgoingdata.put(someBytes);
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   123
            outgoingdata.flip();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   125
            int totalWritten = 0;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   126
            while (totalWritten < size + 10) {
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   127
                int written = sink.write(outgoingdata);
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   128
                if (written < 0)
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   129
                    throw new Exception("Write failed");
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   130
                totalWritten += written;
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   131
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   133
            File f = File.createTempFile("blah"+size, null);
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   134
            f.deleteOnExit();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   135
            RandomAccessFile raf = new RandomAccessFile(f, "rw");
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   136
            FileChannel fc = raf.getChannel();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   137
            long oldPosition = fc.position();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   139
            long bytesWritten = fc.transferFrom(source, 0, size);
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   140
            fc.force(true);
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   141
            if (bytesWritten != size)
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   142
                throw new RuntimeException("Transfer failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   144
            if (fc.position() != oldPosition)
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   145
                throw new RuntimeException("Position changed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   147
            if (fc.size() != size)
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   148
                throw new RuntimeException("Unexpected sink size "+ fc.size());
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   149
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   150
            fc.close();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   151
            sink.close();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   152
            source.close();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   153
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   154
            f.delete();
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   155
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   158
    @Test
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   159
    public void xferTest02() throws Exception { // for bug 4482726
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        byte[] srcData = new byte[5000];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i=0; i<5000; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            srcData[i] = (byte)generator.nextInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // get filechannel for the source file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        FileChannel fc1 = raf1.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // write out data to the file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        long bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        while (bytesWritten < 5000) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // get filechannel for the dst file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        File dest = File.createTempFile("dest", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        dest.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        FileChannel fc2 = raf2.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        int bytesToWrite = 3000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int startPosition = 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        bytesWritten = fc1.transferTo(startPosition, bytesToWrite, fc2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        fc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        fc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        raf1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        raf2.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   191
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   192
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   193
        dest.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   196
    @Test
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   197
    public void xferTest03() throws Exception { // for bug 4559072
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        byte[] srcData = new byte[] {1,2,3,4} ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // get filechannel for the source file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        RandomAccessFile raf1 = new RandomAccessFile(source, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        FileChannel fc1 = raf1.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        fc1.truncate(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // write out data to the file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        while (bytesWritten < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            bytesWritten = fc1.write(ByteBuffer.wrap(srcData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        // get filechannel for the dst file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        File dest = File.createTempFile("dest", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        dest.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        RandomAccessFile raf2 = new RandomAccessFile(dest, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        FileChannel fc2 = raf2.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        fc2.truncate(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        fc1.transferTo(0, srcData.length + 1, fc2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (fc2.size() > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            throw new Exception("xferTest03 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        fc1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        fc2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        raf1.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        raf2.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   229
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   230
        source.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   231
        dest.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
44273
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents: 44266
diff changeset
   234
    // xferTest04() and xferTest05() moved to Transfer4GBFile.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    static void checkFileData(File file, String expected) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        FileInputStream fis = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        Reader r = new BufferedReader(new InputStreamReader(fis, "ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        StringBuilder sb = new StringBuilder();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        while ((c = r.read()) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            sb.append((char)c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        String contents = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (! contents.equals(expected))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            throw new Exception("expected: " + expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                + ", got: " + contents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        r.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    // Test transferFrom asking for more bytes than remain in source
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   251
    @Test
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   252
    public void xferTest06() throws Exception { // for bug 5081340
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        String data = "Use the source, Luke!";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        File sink = File.createTempFile("sink", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        sink.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        FileOutputStream fos = new FileOutputStream(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        fos.write(data.getBytes("ASCII"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        FileChannel sourceChannel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            new RandomAccessFile(source, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        sourceChannel.position(7);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        long remaining = sourceChannel.size() - sourceChannel.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        FileChannel sinkChannel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            new RandomAccessFile(sink, "rw").getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        long n = sinkChannel.transferFrom(sourceChannel, 0L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                          sourceChannel.size()); // overflow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (n != remaining)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            throw new Exception("n == " + n + ", remaining == " + remaining);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        sinkChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        checkFileData(source, data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        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
   280
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   281
        source.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    // Test transferTo to non-blocking socket channel
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   285
    @Test
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   286
    public void xferTest07() throws Exception { // for bug 5103988
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        File source = File.createTempFile("source", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        source.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        FileChannel sourceChannel = new RandomAccessFile(source, "rw")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            .getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        sourceChannel.position(32000L)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            .write(ByteBuffer.wrap("The End".getBytes()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        // The sink is a non-blocking socket channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        ssc.socket().bind(new InetSocketAddress(0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        InetSocketAddress sa = new InetSocketAddress(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            InetAddress.getLocalHost(), ssc.socket().getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        SocketChannel sink = SocketChannel.open(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        sink.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        SocketChannel other = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        long size = sourceChannel.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // keep sending until congested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        long n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            n = sourceChannel.transferTo(0, size, sink);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        sourceChannel.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        sink.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        other.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        ssc.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
   316
        source.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
44273
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents: 44266
diff changeset
   319
    // xferTest08() moved to TransferTo6GBFile.java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
6545
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   321
    // 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
   322
    // throws NonReadableChannelException
43708
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   323
    @Test
1f5975d73136 8174127: (ch) Add instrumentation to java/nio/channels/FileChannel/Transfer.java
bpb
parents: 30046
diff changeset
   324
    public void xferTest09() throws Exception { // for bug 6984545
6545
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   325
        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
   326
        source.deleteOnExit();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   327
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   328
        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
   329
        target.deleteOnExit();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   330
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   331
        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
   332
        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
   333
        try {
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   334
            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
   335
            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
   336
        } catch (NonReadableChannelException expected) {
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   337
        } finally {
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   338
            fc1.close();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   339
            fc2.close();
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   340
        }
9d2efd6ddd0c 6984545: (fc) transferFrom does not throw NonReadableChannelException when target is size 0 and non-readable
alanb
parents: 5977
diff changeset
   341
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}