Path: blob/master/test/jdk/java/nio/channels/SocketChannel/VectorParams.java
41154 views
/*1* Copyright (c) 2003, 2018, 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 486503125* @summary Test ScatteringByteChannel/GatheringByteChannel read/write26* @library .. /test/lib27* @build jdk.test.lib.Utils TestServers28* @run main VectorParams29*/3031import java.io.*;32import java.net.*;33import java.nio.*;34import java.nio.channels.*;3536public class VectorParams {3738static java.io.PrintStream out = System.out;3940static final int testSize = 10;41static ByteBuffer[] bufs = null;42static InetSocketAddress isa = null;4344public static void main(String[] args) throws Exception {45try (TestServers.DayTimeServer daytimeServer46= TestServers.DayTimeServer.startNewServer(100)) {47initBufs(daytimeServer);48testSocketChannelVectorParams();49testDatagramChannelVectorParams();50testPipeVectorParams();51testFileVectorParams();52}53}5455static void initBufs(TestServers.DayTimeServer daytimeServer) throws Exception {56bufs = new ByteBuffer[testSize];57for(int i=0; i<testSize; i++) {58String source = "buffer" + i;59bufs[i] = ByteBuffer.allocate(source.length());60bufs[i].put(source.getBytes("8859_1"));61bufs[i].flip();62}63isa = new InetSocketAddress(daytimeServer.getAddress(),64daytimeServer.getPort());65}6667static void testSocketChannelVectorParams() throws Exception {68SocketChannel sc = SocketChannel.open(isa);69tryBadWrite(sc, bufs, 0, -1);70tryBadWrite(sc, bufs, -1, 0);71tryBadWrite(sc, bufs, 0, 1000);72tryBadWrite(sc, bufs, 1000, 1);73tryBadRead(sc, bufs, 0, -1);74tryBadRead(sc, bufs, -1, 0);75tryBadRead(sc, bufs, 0, 1000);76tryBadRead(sc, bufs, 1000, 1);77sc.close();78}7980static void testDatagramChannelVectorParams() throws Exception {81DatagramChannel dc = DatagramChannel.open();82dc.connect(isa);83tryBadRead(dc, bufs, 0, -1);84tryBadRead(dc, bufs, -1, 0);85tryBadRead(dc, bufs, 0, 1000);86tryBadRead(dc, bufs, 1000, 1);87tryBadWrite(dc, bufs, 0, -1);88tryBadWrite(dc, bufs, -1, 0);89tryBadWrite(dc, bufs, 0, 1000);90tryBadWrite(dc, bufs, 1000, 1);91dc.close();92}9394static void testPipeVectorParams() throws Exception {95Pipe p = Pipe.open();96Pipe.SinkChannel sink = p.sink();97Pipe.SourceChannel source = p.source();98tryBadWrite(sink, bufs, 0, -1);99tryBadWrite(sink, bufs, -1, 0);100tryBadWrite(sink, bufs, 0, 1000);101tryBadWrite(sink, bufs, 1000, 1);102tryBadRead(source, bufs, 0, -1);103tryBadRead(source, bufs, -1, 0);104tryBadRead(source, bufs, 0, 1000);105tryBadRead(source, bufs, 1000, 1);106sink.close();107source.close();108}109110static void testFileVectorParams() throws Exception {111File testFile = File.createTempFile("filevec", null);112testFile.deleteOnExit();113RandomAccessFile raf = new RandomAccessFile(testFile, "rw");114FileChannel fc = raf.getChannel();115tryBadWrite(fc, bufs, 0, -1);116tryBadWrite(fc, bufs, -1, 0);117tryBadWrite(fc, bufs, 0, 1000);118tryBadWrite(fc, bufs, 1000, 1);119tryBadRead(fc, bufs, 0, -1);120tryBadRead(fc, bufs, -1, 0);121tryBadRead(fc, bufs, 0, 1000);122tryBadRead(fc, bufs, 1000, 1);123fc.close();124}125126private static void tryBadWrite(GatheringByteChannel gbc,127ByteBuffer[] bufs, int offset, int len)128throws Exception129{130try {131gbc.write(bufs, offset, len);132throw new RuntimeException("Expected exception not thrown");133} catch (IndexOutOfBoundsException ioobe) {134// Correct result135}136}137138private static void tryBadRead(ScatteringByteChannel sbc,139ByteBuffer[] bufs, int offset, int len)140throws Exception141{142try {143sbc.read(bufs, offset, len);144throw new RuntimeException("Expected exception not thrown");145} catch (IndexOutOfBoundsException ioobe) {146// Correct result147}148}149150}151152153