jdk/test/java/nio/channels/FileChannel/TransferToChannel.java
author coffeys
Thu, 08 Jan 2015 11:44:10 +0000
changeset 28303 641c5c22c491
parent 27937 0c9f63e42e91
permissions -rw-r--r--
8068507: (fc) Rename the new jdk.net.enableFastFileTransfer system property to jdk.nio.enableFastFileTransfer Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4652496
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test transferTo with different target channels
27937
0c9f63e42e91 8064407: (fc) FileChannel transferTo should use TransmitFile on Windows
alanb
parents: 7668
diff changeset
    27
 * @run main TransferToChannel
28303
641c5c22c491 8068507: (fc) Rename the new jdk.net.enableFastFileTransfer system property to jdk.nio.enableFastFileTransfer
coffeys
parents: 27937
diff changeset
    28
 * @run main/othervm -Djdk.nio.enableFastFileTransfer TransferToChannel
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.WritableByteChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class TransferToChannel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static File file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static File outFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static FileChannel in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // Chunk size should be larger than FileChannelImpl.TRANSFER_SIZE for good test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static int CHUNK_SIZE = 1024 * 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        file = File.createTempFile("readingin", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        outFile = File.createTempFile("writingout", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        file.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        outFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        generateBigFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        FileInputStream fis = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        in = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        test1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        test2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        in.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    56
        file.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    57
        outFile.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    static void test1() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for (int i=0; i<10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            transferFileToUserChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.err.println("Transferred file...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static void test2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        for (int i=0; i<10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            transferFileToTrustedChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            System.err.println("Transferred file...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    static void transferFileToUserChannel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        long remainingBytes = in.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        long size = remainingBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        WritableByteChannel wbc = new WritableByteChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                Random rand = new Random(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                public int write(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    int read = src.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    byte[] incoming = new byte[read];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    src.get(incoming);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    checkData(incoming, read);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    return read == 0 ? -1 : read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                public boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                void checkData(byte[] incoming, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    byte[] expected = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    rand.nextBytes(expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    for (int i=0; i<size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        if (incoming[i] != expected[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                            throw new RuntimeException("Data corrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        while (remainingBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            long bytesTransferred = in.transferTo(size - remainingBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                              Math.min(CHUNK_SIZE, remainingBytes), wbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (bytesTransferred >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                remainingBytes -= bytesTransferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                throw new Exception("transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static void transferFileToTrustedChannel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        long remainingBytes = in.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        long size = remainingBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        FileOutputStream fos = new FileOutputStream(outFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        FileChannel out = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        while (remainingBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            long bytesTransferred = in.transferTo(size - remainingBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                                                  CHUNK_SIZE, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (bytesTransferred >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                remainingBytes -= bytesTransferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                throw new Exception("transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    static void generateBigFile(File file) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        OutputStream out = new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                           new FileOutputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        byte[] randomBytes = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        Random rand = new Random(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        for (int i = 0; i < 1000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            rand.nextBytes(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            out.write(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}