Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/VectorParams.java
38828 views
/*1* Copyright (c) 2003, 2012, 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 ..27*/2829import java.io.*;30import java.net.*;31import java.nio.*;32import java.nio.channels.*;3334public class VectorParams {3536static java.io.PrintStream out = System.out;3738static final int testSize = 10;39static ByteBuffer[] bufs = null;40static InetSocketAddress isa = null;4142public static void main(String[] args) throws Exception {43try (TestServers.DayTimeServer daytimeServer44= TestServers.DayTimeServer.startNewServer(100)) {45initBufs(daytimeServer);46testSocketChannelVectorParams();47testDatagramChannelVectorParams();48testPipeVectorParams();49testFileVectorParams();50}51}5253static void initBufs(TestServers.DayTimeServer daytimeServer) throws Exception {54bufs = new ByteBuffer[testSize];55for(int i=0; i<testSize; i++) {56String source = "buffer" + i;57bufs[i] = ByteBuffer.allocate(source.length());58bufs[i].put(source.getBytes("8859_1"));59bufs[i].flip();60}61isa = new InetSocketAddress(daytimeServer.getAddress(),62daytimeServer.getPort());63}6465static void testSocketChannelVectorParams() throws Exception {66SocketChannel sc = SocketChannel.open(isa);67tryBadWrite(sc, bufs, 0, -1);68tryBadWrite(sc, bufs, -1, 0);69tryBadWrite(sc, bufs, 0, 1000);70tryBadWrite(sc, bufs, 1000, 1);71tryBadRead(sc, bufs, 0, -1);72tryBadRead(sc, bufs, -1, 0);73tryBadRead(sc, bufs, 0, 1000);74tryBadRead(sc, bufs, 1000, 1);75sc.close();76}7778static void testDatagramChannelVectorParams() throws Exception {79DatagramChannel dc = DatagramChannel.open();80dc.connect(isa);81tryBadRead(dc, bufs, 0, -1);82tryBadRead(dc, bufs, -1, 0);83tryBadRead(dc, bufs, 0, 1000);84tryBadRead(dc, bufs, 1000, 1);85tryBadWrite(dc, bufs, 0, -1);86tryBadWrite(dc, bufs, -1, 0);87tryBadWrite(dc, bufs, 0, 1000);88tryBadWrite(dc, bufs, 1000, 1);89dc.close();90}9192static void testPipeVectorParams() throws Exception {93Pipe p = Pipe.open();94Pipe.SinkChannel sink = p.sink();95Pipe.SourceChannel source = p.source();96tryBadWrite(sink, bufs, 0, -1);97tryBadWrite(sink, bufs, -1, 0);98tryBadWrite(sink, bufs, 0, 1000);99tryBadWrite(sink, bufs, 1000, 1);100tryBadRead(source, bufs, 0, -1);101tryBadRead(source, bufs, -1, 0);102tryBadRead(source, bufs, 0, 1000);103tryBadRead(source, bufs, 1000, 1);104sink.close();105source.close();106}107108static void testFileVectorParams() throws Exception {109File testFile = File.createTempFile("filevec", null);110testFile.deleteOnExit();111RandomAccessFile raf = new RandomAccessFile(testFile, "rw");112FileChannel fc = raf.getChannel();113tryBadWrite(fc, bufs, 0, -1);114tryBadWrite(fc, bufs, -1, 0);115tryBadWrite(fc, bufs, 0, 1000);116tryBadWrite(fc, bufs, 1000, 1);117tryBadRead(fc, bufs, 0, -1);118tryBadRead(fc, bufs, -1, 0);119tryBadRead(fc, bufs, 0, 1000);120tryBadRead(fc, bufs, 1000, 1);121fc.close();122}123124private static void tryBadWrite(GatheringByteChannel gbc,125ByteBuffer[] bufs, int offset, int len)126throws Exception127{128try {129gbc.write(bufs, offset, len);130throw new RuntimeException("Expected exception not thrown");131} catch (IndexOutOfBoundsException ioobe) {132// Correct result133}134}135136private static void tryBadRead(ScatteringByteChannel sbc,137ByteBuffer[] bufs, int offset, int len)138throws Exception139{140try {141sbc.read(bufs, offset, len);142throw new RuntimeException("Expected exception not thrown");143} catch (IndexOutOfBoundsException ioobe) {144// Correct result145}146}147148}149150151