1/*
2 * Copyright (c) 2005, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25   @bug 5105464 6269047 6541631
26 * @summary Test to transfer bytes with a size bigger than Integer.MAX_VALUE
27 */
28
29
30import java.io.*;
31import java.net.*;
32import java.nio.channels.*;
33
34public 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        server.close();
85
86        inChannel.close();
87        outChannel.close();
88
89        inFile.delete();
90        outFile.delete();
91    }
92
93    private static class MyJob extends Thread {
94        public MyJob(ServerSocket server) {
95            setDaemon(true);
96            this.server = server;
97        }
98
99        public void run() {
100            try {
101                Socket s = server.accept();
102                System.out.println("MyJob-run: client connected: "+s);
103
104                byte[] bs = new byte[10];
105                System.out.println("MyJob-run: write some bytes to client.");
106
107                s.getOutputStream().write(bs);
108                s.getOutputStream().flush();
109
110                // no need to write all Integer.MAX_VALUE + 1 bytes
111                // it will take too much time
112                System.out.println("MyJob-run: close the client socket.");
113                s.close();
114            } catch (Exception e) {
115                // unexpected
116                e.printStackTrace();
117
118                System.exit(1);
119            }
120        }
121
122        private ServerSocket server;
123    }
124}
125