jdk/test/java/nio/channels/FileChannel/LongTransferTest.java
changeset 2 90ce3da70b43
child 789 218b7e32fe38
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /* @test
       
    25    @bug 5105464 6269047
       
    26  * @summary Test to transfer bytes with a size bigger than Integer.MAX_VALUE
       
    27  */
       
    28 
       
    29 
       
    30 import java.io.*;
       
    31 import java.net.*;
       
    32 import java.nio.channels.*;
       
    33 
       
    34 public class LongTransferTest {
       
    35     public static void main(String[] args) throws Exception {
       
    36         System.out.println("LongTransferTest-main: "+
       
    37          "Test to transfer bytes with a size bigger than Integer.MAX_VALUE.");
       
    38 
       
    39         System.out.println("LongTransferTest-main: Test at first "+
       
    40                "the private method transferFromFileChannel with files...");
       
    41 
       
    42         final String dir = (String)System.getProperty("java.io.tmpdir");
       
    43         System.out.println(
       
    44             "LongTransferTest-main: using the temp dir (java.io.tmpdir) "+dir);
       
    45 
       
    46         File inFile = new File(dir, "LongTransferTest_channelTestInFile_tmp");
       
    47         if (!inFile.exists()) {
       
    48             inFile.createNewFile();
       
    49         }
       
    50 
       
    51         File outFile = new File(dir, "LongTransferTest_channelTestOutFile_tmp");
       
    52         if (!outFile.exists()) {
       
    53             outFile.createNewFile();
       
    54         }
       
    55 
       
    56         FileInputStream inStream = new FileInputStream(inFile);
       
    57         FileChannel inChannel = inStream.getChannel();
       
    58 
       
    59         FileOutputStream outStream = new FileOutputStream(outFile);
       
    60         FileChannel outChannel = outStream.getChannel();
       
    61 
       
    62         outChannel.transferFrom(inChannel, 0, (long)Integer.MAX_VALUE+1L);
       
    63 
       
    64         System.out.println("LongTransferTest-main: Test the method transferTo with files.");
       
    65 
       
    66         inChannel.transferTo(0, (long)Integer.MAX_VALUE+1L, outChannel);
       
    67 
       
    68 
       
    69         System.out.println("LongTransferTest-main: Test the "+
       
    70              "private method transferFromArbitraryChannel with sockets ...");
       
    71 
       
    72         ServerSocket server = new ServerSocket(0);
       
    73         MyJob job = new MyJob(server);
       
    74         job.start();
       
    75 
       
    76         SocketChannel socket = SocketChannel.open();
       
    77         socket.socket().connect(new InetSocketAddress(server.getInetAddress(), server.getLocalPort()));
       
    78 
       
    79         outChannel.transferFrom(socket, 0, (long)Integer.MAX_VALUE + 1L);
       
    80 
       
    81         System.out.println("LongTransferTest-main: OK!");
       
    82 
       
    83         socket.close();
       
    84 
       
    85         server.close();
       
    86 
       
    87         inFile.delete();
       
    88         outFile.delete();
       
    89     }
       
    90 
       
    91     private static class MyJob extends Thread {
       
    92         public MyJob(ServerSocket server) {
       
    93             setDaemon(true);
       
    94             this.server = server;
       
    95         }
       
    96 
       
    97         public void run() {
       
    98             try {
       
    99                 Socket s = server.accept();
       
   100                 System.out.println("MyJob-run: client connected: "+s);
       
   101 
       
   102                 byte[] bs = new byte[10];
       
   103                 System.out.println("MyJob-run: write some bytes to client.");
       
   104 
       
   105                 s.getOutputStream().write(bs);
       
   106                 s.getOutputStream().flush();
       
   107 
       
   108                 // no need to write all Integer.MAX_VALUE + 1 bytes
       
   109                 // it will take too much time
       
   110                 System.out.println("MyJob-run: close the client socket.");
       
   111                 s.close();
       
   112             } catch (Exception e) {
       
   113                 // unexpected
       
   114                 e.printStackTrace();
       
   115 
       
   116                 System.exit(1);
       
   117             }
       
   118         }
       
   119 
       
   120         private ServerSocket server;
       
   121     }
       
   122 }