1/*
2 * Copyright (c) 2003, 2012, 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 4865031
26 * @summary Test ScatteringByteChannel/GatheringByteChannel read/write
27 * @library ..
28 */
29
30import java.io.*;
31import java.net.*;
32import java.nio.*;
33import java.nio.channels.*;
34
35public class VectorParams {
36
37    static java.io.PrintStream out = System.out;
38
39    static final int testSize = 10;
40    static ByteBuffer[] bufs = null;
41    static InetSocketAddress isa = null;
42
43    public static void main(String[] args) throws Exception {
44        try (TestServers.DayTimeServer daytimeServer
45                = TestServers.DayTimeServer.startNewServer(100)) {
46            initBufs(daytimeServer);
47            testSocketChannelVectorParams();
48            testDatagramChannelVectorParams();
49            testPipeVectorParams();
50            testFileVectorParams();
51        }
52    }
53
54    static void initBufs(TestServers.DayTimeServer daytimeServer) throws Exception {
55        bufs = new ByteBuffer[testSize];
56        for(int i=0; i<testSize; i++) {
57            String source = "buffer" + i;
58            bufs[i] = ByteBuffer.allocate(source.length());
59            bufs[i].put(source.getBytes("8859_1"));
60            bufs[i].flip();
61        }
62        isa = new InetSocketAddress(daytimeServer.getAddress(),
63                                    daytimeServer.getPort());
64    }
65
66    static void testSocketChannelVectorParams() throws Exception {
67        SocketChannel sc = SocketChannel.open(isa);
68        tryBadWrite(sc, bufs, 0, -1);
69        tryBadWrite(sc, bufs, -1, 0);
70        tryBadWrite(sc, bufs, 0, 1000);
71        tryBadWrite(sc, bufs, 1000, 1);
72        tryBadRead(sc, bufs, 0, -1);
73        tryBadRead(sc, bufs, -1, 0);
74        tryBadRead(sc, bufs, 0, 1000);
75        tryBadRead(sc, bufs, 1000, 1);
76        sc.close();
77    }
78
79    static void testDatagramChannelVectorParams() throws Exception {
80        DatagramChannel dc = DatagramChannel.open();
81        dc.connect(isa);
82        tryBadRead(dc, bufs, 0, -1);
83        tryBadRead(dc, bufs, -1, 0);
84        tryBadRead(dc, bufs, 0, 1000);
85        tryBadRead(dc, bufs, 1000, 1);
86        tryBadWrite(dc, bufs, 0, -1);
87        tryBadWrite(dc, bufs, -1, 0);
88        tryBadWrite(dc, bufs, 0, 1000);
89        tryBadWrite(dc, bufs, 1000, 1);
90        dc.close();
91    }
92
93    static void testPipeVectorParams() throws Exception {
94        Pipe p = Pipe.open();
95        Pipe.SinkChannel sink = p.sink();
96        Pipe.SourceChannel source = p.source();
97        tryBadWrite(sink, bufs, 0, -1);
98        tryBadWrite(sink, bufs, -1, 0);
99        tryBadWrite(sink, bufs, 0, 1000);
100        tryBadWrite(sink, bufs, 1000, 1);
101        tryBadRead(source, bufs, 0, -1);
102        tryBadRead(source, bufs, -1, 0);
103        tryBadRead(source, bufs, 0, 1000);
104        tryBadRead(source, bufs, 1000, 1);
105        sink.close();
106        source.close();
107    }
108
109    static void testFileVectorParams() throws Exception {
110        File testFile = File.createTempFile("filevec", null);
111        testFile.deleteOnExit();
112        RandomAccessFile raf = new RandomAccessFile(testFile, "rw");
113        FileChannel fc = raf.getChannel();
114        tryBadWrite(fc, bufs, 0, -1);
115        tryBadWrite(fc, bufs, -1, 0);
116        tryBadWrite(fc, bufs, 0, 1000);
117        tryBadWrite(fc, bufs, 1000, 1);
118        tryBadRead(fc, bufs, 0, -1);
119        tryBadRead(fc, bufs, -1, 0);
120        tryBadRead(fc, bufs, 0, 1000);
121        tryBadRead(fc, bufs, 1000, 1);
122        fc.close();
123    }
124
125    private static void tryBadWrite(GatheringByteChannel gbc,
126                                    ByteBuffer[] bufs, int offset, int len)
127        throws Exception
128    {
129        try {
130            gbc.write(bufs, offset, len);
131            throw new RuntimeException("Expected exception not thrown");
132        } catch (IndexOutOfBoundsException ioobe) {
133            // Correct result
134        }
135    }
136
137    private static void tryBadRead(ScatteringByteChannel sbc,
138                                   ByteBuffer[] bufs, int offset, int len)
139        throws Exception
140    {
141        try {
142            sbc.read(bufs, offset, len);
143            throw new RuntimeException("Expected exception not thrown");
144        } catch (IndexOutOfBoundsException ioobe) {
145            // Correct result
146        }
147    }
148
149}
150