J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003 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 4865031 |
| 26 | * @summary Test ScatteringByteChannel/GatheringByteChannel read/write |
| 27 | * @library .. |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | import java.nio.*; |
| 33 | import java.nio.channels.*; |
| 34 | import java.nio.charset.*; |
| 35 | |
| 36 | public class VectorParams { |
| 37 | |
| 38 | static java.io.PrintStream out = System.out; |
| 39 | |
| 40 | static final int DAYTIME_PORT = 13; |
| 41 | static final String DAYTIME_HOST = TestUtil.HOST; |
| 42 | static final int testSize = 10; |
| 43 | static ByteBuffer[] bufs = null; |
| 44 | static InetSocketAddress isa = null; |
| 45 | |
| 46 | public static void main(String[] args) throws Exception { |
| 47 | initBufs(); |
| 48 | testSocketChannelVectorParams(); |
| 49 | testDatagramChannelVectorParams(); |
| 50 | testPipeVectorParams(); |
| 51 | testFileVectorParams(); |
| 52 | } |
| 53 | |
| 54 | static void initBufs() 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(InetAddress.getByName(DAYTIME_HOST), |
| 63 | DAYTIME_PORT); |
| 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 | } |