Path: blob/master/test/jdk/java/nio/channels/FileChannel/LongTransferTest.java
41154 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 5105464 6269047 654163125* @summary Test to transfer bytes with a size bigger than Integer.MAX_VALUE26*/272829import java.io.*;30import java.net.*;31import java.nio.channels.*;3233public class LongTransferTest {34public static void main(String[] args) throws Exception {35System.out.println("LongTransferTest-main: "+36"Test to transfer bytes with a size bigger than Integer.MAX_VALUE.");3738System.out.println("LongTransferTest-main: Test at first "+39"the private method transferFromFileChannel with files...");4041final String dir = (String)System.getProperty("java.io.tmpdir");42System.out.println(43"LongTransferTest-main: using the temp dir (java.io.tmpdir) "+dir);4445File inFile = new File(dir, "LongTransferTest_channelTestInFile_tmp");46if (!inFile.exists()) {47inFile.createNewFile();48}4950File outFile = new File(dir, "LongTransferTest_channelTestOutFile_tmp");51if (!outFile.exists()) {52outFile.createNewFile();53}5455FileInputStream inStream = new FileInputStream(inFile);56FileChannel inChannel = inStream.getChannel();5758FileOutputStream outStream = new FileOutputStream(outFile);59FileChannel outChannel = outStream.getChannel();6061outChannel.transferFrom(inChannel, 0, (long)Integer.MAX_VALUE+1L);6263System.out.println("LongTransferTest-main: Test the method transferTo with files.");6465inChannel.transferTo(0, (long)Integer.MAX_VALUE+1L, outChannel);666768System.out.println("LongTransferTest-main: Test the "+69"private method transferFromArbitraryChannel with sockets ...");7071ServerSocket server = new ServerSocket(0);72MyJob job = new MyJob(server);73job.start();7475SocketChannel socket = SocketChannel.open();76socket.socket().connect(new InetSocketAddress(server.getInetAddress(), server.getLocalPort()));7778outChannel.transferFrom(socket, 0, (long)Integer.MAX_VALUE + 1L);7980System.out.println("LongTransferTest-main: OK!");8182socket.close();83server.close();8485inChannel.close();86outChannel.close();8788inFile.delete();89outFile.delete();90}9192private static class MyJob extends Thread {93public MyJob(ServerSocket server) {94setDaemon(true);95this.server = server;96}9798public void run() {99try {100Socket s = server.accept();101System.out.println("MyJob-run: client connected: "+s);102103byte[] bs = new byte[10];104System.out.println("MyJob-run: write some bytes to client.");105106s.getOutputStream().write(bs);107s.getOutputStream().flush();108109// no need to write all Integer.MAX_VALUE + 1 bytes110// it will take too much time111System.out.println("MyJob-run: close the client socket.");112s.close();113} catch (Exception e) {114// unexpected115e.printStackTrace();116117System.exit(1);118}119}120121private ServerSocket server;122}123}124125126