test/java/nio/channels/SocketChannel/VectorParams.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 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 
  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);


   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 

  30 import java.io.*;
  31 import java.net.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;

  34 
  35 public 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 (TestClass.DayTimeServer daytimeServer
  45                 = TestClass.DayTimeServer.startNewServer(100)) {
  46             initBufs(daytimeServer);
  47             testSocketChannelVectorParams();
  48             testDatagramChannelVectorParams();
  49             testPipeVectorParams();
  50             testFileVectorParams();
  51         }
  52     }
  53 
  54     static void initBufs(TestClass.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);