test/jdk/java/nio/channels/FileChannel/Transfer4GBFile.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:
44273
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     1
/*
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     2
 * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     4
 *
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     8
 *
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    13
 * accompanied this code).
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    14
 *
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    18
 *
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    21
 * questions.
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    22
 */
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    23
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    24
/* @test
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    25
 * @bug 4638365
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    26
 * @summary Test FileChannel.transferFrom and transferTo for 4GB files
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    27
 * @run testng/timeout=300 Transfer4GBFile
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    28
 */
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    29
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    30
import java.io.BufferedWriter;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    31
import java.io.File;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    32
import java.io.FileInputStream;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    33
import java.io.FileOutputStream;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    34
import java.io.IOException;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    35
import java.io.OutputStreamWriter;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    36
import java.io.PrintStream;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    37
import java.io.RandomAccessFile;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    38
import java.nio.ByteBuffer;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    39
import java.nio.channels.FileChannel;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    40
import java.nio.file.StandardOpenOption;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    41
import java.nio.file.FileAlreadyExistsException;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    42
import java.util.concurrent.TimeUnit;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    43
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    44
import org.testng.annotations.Test;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    45
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    46
public class Transfer4GBFile {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    47
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    48
    private static PrintStream err = System.err;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    49
    private static PrintStream out = System.out;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    50
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    51
    // Test transferTo with large file
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    52
    @Test
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    53
    public void xferTest04() throws Exception { // for bug 4638365
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    54
        File source = File.createTempFile("blah", null);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    55
        source.deleteOnExit();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    56
        long testSize = ((long)Integer.MAX_VALUE) * 2;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    57
        initTestFile(source, 10);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    58
        RandomAccessFile raf = new RandomAccessFile(source, "rw");
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    59
        FileChannel fc = raf.getChannel();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    60
        out.println("  Writing large file...");
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    61
        long t0 = System.nanoTime();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    62
        fc.write(ByteBuffer.wrap("Use the source!".getBytes()), testSize - 40);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    63
        long t1 = System.nanoTime();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    64
        out.printf("  Wrote large file in %d ns (%d ms) %n",
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    65
            t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    66
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    67
        fc.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    68
        raf.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    69
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    70
        File sink = File.createTempFile("sink", null);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    71
        sink.deleteOnExit();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    72
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    73
        FileInputStream fis = new FileInputStream(source);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    74
        FileChannel sourceChannel = fis.getChannel();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    75
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    76
        raf = new RandomAccessFile(sink, "rw");
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    77
        FileChannel sinkChannel = raf.getChannel();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    78
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    79
        long bytesWritten = sourceChannel.transferTo(testSize -40, 10,
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    80
                                                     sinkChannel);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    81
        if (bytesWritten != 10) {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    82
            throw new RuntimeException("Transfer test 4 failed " +
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    83
                                       bytesWritten);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    84
        }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    85
        sourceChannel.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    86
        sinkChannel.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    87
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    88
        source.delete();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    89
        sink.delete();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    90
    }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    91
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    92
    // Test transferFrom with large file
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    93
    @Test
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    94
    public void xferTest05() throws Exception { // for bug 4638365
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    95
        // Create a source file & large sink file for the test
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    96
        File source = File.createTempFile("blech", null);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    97
        source.deleteOnExit();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    98
        initTestFile(source, 100);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
    99
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   100
        // Create the sink file as a sparse file if possible
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   101
        File sink = null;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   102
        FileChannel fc = null;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   103
        while (fc == null) {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   104
            sink = File.createTempFile("sink", null);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   105
            // re-create as a sparse file
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   106
            sink.delete();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   107
            try {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   108
                fc = FileChannel.open(sink.toPath(),
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   109
                                      StandardOpenOption.CREATE_NEW,
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   110
                                      StandardOpenOption.WRITE,
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   111
                                      StandardOpenOption.SPARSE);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   112
            } catch (FileAlreadyExistsException ignore) {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   113
                // someone else got it
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   114
            }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   115
        }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   116
        sink.deleteOnExit();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   117
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   118
        long testSize = ((long)Integer.MAX_VALUE) * 2;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   119
        try {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   120
            out.println("  Writing large file...");
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   121
            long t0 = System.nanoTime();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   122
            fc.write(ByteBuffer.wrap("Use the source!".getBytes()),
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   123
                     testSize - 40);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   124
            long t1 = System.nanoTime();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   125
            out.printf("  Wrote large file in %d ns (%d ms) %n",
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   126
            t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0));
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   127
        } catch (IOException e) {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   128
            // Can't set up the test, abort it
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   129
            err.println("xferTest05 was aborted.");
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   130
            return;
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   131
        } finally {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   132
            fc.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   133
        }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   134
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   135
        // Get new channels for the source and sink and attempt transfer
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   136
        FileChannel sourceChannel = new FileInputStream(source).getChannel();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   137
        try {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   138
            FileChannel sinkChannel = new RandomAccessFile(sink, "rw").getChannel();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   139
            try {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   140
                long bytesWritten = sinkChannel.transferFrom(sourceChannel,
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   141
                                                             testSize - 40, 10);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   142
                if (bytesWritten != 10) {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   143
                    throw new RuntimeException("Transfer test 5 failed " +
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   144
                                               bytesWritten);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   145
                }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   146
            } finally {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   147
                sinkChannel.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   148
            }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   149
        } finally {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   150
            sourceChannel.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   151
        }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   152
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   153
        source.delete();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   154
        sink.delete();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   155
    }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   156
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   157
    /**
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   158
     * Creates file blah of specified size in bytes.
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   159
     */
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   160
    private static void initTestFile(File blah, long size) throws Exception {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   161
        if (blah.exists())
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   162
            blah.delete();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   163
        FileOutputStream fos = new FileOutputStream(blah);
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   164
        BufferedWriter awriter
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   165
            = new BufferedWriter(new OutputStreamWriter(fos, "8859_1"));
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   166
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   167
        for(int i=0; i<size; i++) {
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   168
            awriter.write("e");
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   169
        }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   170
        awriter.flush();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   171
        awriter.close();
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   172
    }
a2e3e08c5468 8176895: (fc) Split java/nio/channels/FileChannel/Transfer.java into smaller tests
bpb
parents:
diff changeset
   173
}