test/jdk/java/nio/channels/FileChannel/LongTransferTest.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
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 789
diff changeset
     2
 * Copyright (c) 2005, 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: 789
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 789
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 789
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
789
218b7e32fe38 6541631: (fc) java/nio/channels/Filechannel/LongTransferTest.java should clean up after itself
sherman
parents: 2
diff changeset
    25
   @bug 5105464 6269047 6541631
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Test to transfer bytes with a size bigger than Integer.MAX_VALUE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class LongTransferTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        System.out.println("LongTransferTest-main: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
         "Test to transfer bytes with a size bigger than Integer.MAX_VALUE.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        System.out.println("LongTransferTest-main: Test at first "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
               "the private method transferFromFileChannel with files...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        final String dir = (String)System.getProperty("java.io.tmpdir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            "LongTransferTest-main: using the temp dir (java.io.tmpdir) "+dir);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        File inFile = new File(dir, "LongTransferTest_channelTestInFile_tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (!inFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            inFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        File outFile = new File(dir, "LongTransferTest_channelTestOutFile_tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (!outFile.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            outFile.createNewFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        FileInputStream inStream = new FileInputStream(inFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        FileChannel inChannel = inStream.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        FileOutputStream outStream = new FileOutputStream(outFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        FileChannel outChannel = outStream.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        outChannel.transferFrom(inChannel, 0, (long)Integer.MAX_VALUE+1L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        System.out.println("LongTransferTest-main: Test the method transferTo with files.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        inChannel.transferTo(0, (long)Integer.MAX_VALUE+1L, outChannel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        System.out.println("LongTransferTest-main: Test the "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
             "private method transferFromArbitraryChannel with sockets ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ServerSocket server = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        MyJob job = new MyJob(server);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        job.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        SocketChannel socket = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        socket.socket().connect(new InetSocketAddress(server.getInetAddress(), server.getLocalPort()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        outChannel.transferFrom(socket, 0, (long)Integer.MAX_VALUE + 1L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        System.out.println("LongTransferTest-main: OK!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        socket.close();
789
218b7e32fe38 6541631: (fc) java/nio/channels/Filechannel/LongTransferTest.java should clean up after itself
sherman
parents: 2
diff changeset
    84
        server.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
789
218b7e32fe38 6541631: (fc) java/nio/channels/Filechannel/LongTransferTest.java should clean up after itself
sherman
parents: 2
diff changeset
    86
        inChannel.close();
218b7e32fe38 6541631: (fc) java/nio/channels/Filechannel/LongTransferTest.java should clean up after itself
sherman
parents: 2
diff changeset
    87
        outChannel.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        inFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        outFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private static class MyJob extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public MyJob(ServerSocket server) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                Socket s = server.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                System.out.println("MyJob-run: client connected: "+s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                byte[] bs = new byte[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                System.out.println("MyJob-run: write some bytes to client.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                s.getOutputStream().write(bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                s.getOutputStream().flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // no need to write all Integer.MAX_VALUE + 1 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // it will take too much time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                System.out.println("MyJob-run: close the client socket.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // unexpected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        private ServerSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}