jdk/test/java/nio/channels/FileChannel/TransferToChannel.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 5970 d4e98bbfb0be
child 27937 0c9f63e42e91
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5970
diff changeset
     2
 * Copyright (c) 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.channels.FileChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.WritableByteChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class TransferToChannel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static File file;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static File outFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static FileChannel in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    // Chunk size should be larger than FileChannelImpl.TRANSFER_SIZE for good test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static int CHUNK_SIZE = 1024 * 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        file = File.createTempFile("readingin", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        outFile = File.createTempFile("writingout", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        file.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        outFile.deleteOnExit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        generateBigFile(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        FileInputStream fis = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        in = fis.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        test1();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        test2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        in.close();
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    54
        file.delete();
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    55
        outFile.delete();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    static void test1() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        for (int i=0; i<10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            transferFileToUserChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.err.println("Transferred file...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static void test2() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for (int i=0; i<10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            transferFileToTrustedChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            System.gc();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            System.err.println("Transferred file...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    static void transferFileToUserChannel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        long remainingBytes = in.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        long size = remainingBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        WritableByteChannel wbc = new WritableByteChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                Random rand = new Random(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                public int write(ByteBuffer src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    int read = src.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    byte[] incoming = new byte[read];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    src.get(incoming);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    checkData(incoming, read);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    return read == 0 ? -1 : read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                public boolean isOpen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                void checkData(byte[] incoming, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    byte[] expected = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    rand.nextBytes(expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    for (int i=0; i<size; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        if (incoming[i] != expected[i])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                            throw new RuntimeException("Data corrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        while (remainingBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            long bytesTransferred = in.transferTo(size - remainingBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                              Math.min(CHUNK_SIZE, remainingBytes), wbc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (bytesTransferred >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                remainingBytes -= bytesTransferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                throw new Exception("transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static void transferFileToTrustedChannel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        long remainingBytes = in.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        long size = remainingBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        FileOutputStream fos = new FileOutputStream(outFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        FileChannel out = fos.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        while (remainingBytes > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            long bytesTransferred = in.transferTo(size - remainingBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                                                  CHUNK_SIZE, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (bytesTransferred >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                remainingBytes -= bytesTransferred;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                throw new Exception("transfer failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static void generateBigFile(File file) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        OutputStream out = new BufferedOutputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                           new FileOutputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        byte[] randomBytes = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Random rand = new Random(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        for (int i = 0; i < 1000; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            rand.nextBytes(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            out.write(randomBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}