1 /*
   2  * Copyright (c) 2000, 2010, 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  * @summary Test socketchannel vector IO
  26  * @library ..
  27  * @key randomness
  28  */
  29 
  30 import java.io.*;
  31 import java.net.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;
  34 import java.util.*;
  35 import sun.misc.*;
  36 
  37 
  38 public class VectorIO {
  39 
  40     static Random generator = new Random();
  41 
  42     static int testSize;
  43 
  44     public static void main(String[] args) throws Exception {
  45         testSize = 1;
  46         runTest();
  47         for(int i=15; i<18; i++) {
  48             testSize = i;
  49             runTest();
  50         }
  51     }
  52 
  53     static void runTest() throws Exception {
  54         System.err.println("Length " + testSize);
  55         Server sv = new Server(testSize);
  56         sv.start();
  57         bufferTest(sv.port());
  58         if (sv.finish(8000) == 0)
  59             throw new Exception("Failed: Length = " + testSize);
  60     }
  61 
  62     static void bufferTest(int port) throws Exception {
  63         ByteBuffer[] bufs = new ByteBuffer[testSize];
  64         long total = 0L;
  65         for(int i=0; i<testSize; i++) {
  66             String source = "buffer" + i;
  67             if (generator.nextBoolean())
  68                 bufs[i] = ByteBuffer.allocateDirect(source.length());
  69             else
  70                 bufs[i] = ByteBuffer.allocate(source.length());
  71 
  72             bufs[i].put(source.getBytes("8859_1"));
  73             bufs[i].flip();
  74             total += bufs[i].remaining();
  75         }
  76 
  77         // Get a connection to the server
  78         InetAddress lh = InetAddress.getLocalHost();
  79         InetSocketAddress isa = new InetSocketAddress(lh, port);
  80         SocketChannel sc = SocketChannel.open();
  81         sc.connect(isa);
  82         sc.configureBlocking(generator.nextBoolean());
  83 
  84         // Write the data out
  85         long rem = total;
  86         while (rem > 0L) {
  87             long bytesWritten = sc.write(bufs);
  88             if (bytesWritten == 0) {
  89                 if (sc.isBlocking())
  90                     throw new RuntimeException("write did not block");
  91                 Thread.sleep(50);
  92             } else {
  93                 rem -= bytesWritten;
  94             }
  95         }
  96 
  97         // Clean up
  98         sc.close();
  99     }
 100 
 101     static class Server
 102         extends TestThread
 103     {
 104         static Random generator = new Random();
 105 
 106         final int testSize;
 107         final ServerSocketChannel ssc;
 108 
 109         Server(int testSize) throws IOException {
 110             super("Server " + testSize);
 111             this.testSize = testSize;
 112             this.ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
 113         }
 114 
 115         int port() {
 116             return ssc.socket().getLocalPort();
 117         }
 118 
 119         void go() throws Exception {
 120             bufferTest();
 121         }
 122 
 123         void bufferTest() throws Exception {
 124             long total = 0L;
 125             ByteBuffer[] bufs = new ByteBuffer[testSize];
 126             for(int i=0; i<testSize; i++) {
 127                 String source = "buffer" + i;
 128                 if (generator.nextBoolean())
 129                     bufs[i] = ByteBuffer.allocateDirect(source.length());
 130                 else
 131                     bufs[i] = ByteBuffer.allocate(source.length());
 132                 total += bufs[i].capacity();
 133             }
 134 
 135             // Get a connection from client
 136             SocketChannel sc = null;
 137 
 138             try {
 139 
 140                 ssc.configureBlocking(false);
 141 
 142                 for (;;) {
 143                     sc = ssc.accept();
 144                     if (sc != null)
 145                         break;
 146                     Thread.sleep(50);
 147                 }
 148 
 149                 sc.configureBlocking(generator.nextBoolean());
 150 
 151                 // Read data into multiple buffers
 152                 long avail = total;
 153                 while (avail > 0) {
 154                     long bytesRead = sc.read(bufs);
 155                     if (bytesRead < 0)
 156                         break;
 157                     if (bytesRead == 0) {
 158                         if (sc.isBlocking())
 159                             throw new RuntimeException("read did not block");
 160                         Thread.sleep(50);
 161                     }
 162                     avail -= bytesRead;
 163                 }
 164 
 165                 // Check results
 166                 for(int i=0; i<testSize; i++) {
 167                     String expected = "buffer" + i;
 168                     bufs[i].flip();
 169                     int size = bufs[i].capacity();
 170                     byte[] data = new byte[size];
 171                     for(int j=0; j<size; j++)
 172                         data[j] = bufs[i].get();
 173                     String message = new String(data, "8859_1");
 174                     if (!message.equals(expected))
 175                         throw new Exception("Wrong data: Got "
 176                                             + message + ", expected "
 177                                             + expected);
 178                 }
 179 
 180             } finally {
 181                 // Clean up
 182                 ssc.close();
 183                 if (sc != null)
 184                     sc.close();
 185             }
 186 
 187         }
 188 
 189     }
 190 
 191 }