jdk/test/java/nio/channels/SocketChannel/VectorIO.java
changeset 6845 0138b84aafdd
parent 5798 9cc262cd2a7a
child 7668 d4a77089c587
equal deleted inserted replaced
6697:39929804f9d4 6845:0138b84aafdd
    58             throw new Exception("Failed: Length = " + testSize);
    58             throw new Exception("Failed: Length = " + testSize);
    59     }
    59     }
    60 
    60 
    61     static void bufferTest(int port) throws Exception {
    61     static void bufferTest(int port) throws Exception {
    62         ByteBuffer[] bufs = new ByteBuffer[testSize];
    62         ByteBuffer[] bufs = new ByteBuffer[testSize];
       
    63         long total = 0L;
    63         for(int i=0; i<testSize; i++) {
    64         for(int i=0; i<testSize; i++) {
    64             String source = "buffer" + i;
    65             String source = "buffer" + i;
    65             if (generator.nextBoolean())
    66             if (generator.nextBoolean())
    66                 bufs[i] = ByteBuffer.allocateDirect(source.length());
    67                 bufs[i] = ByteBuffer.allocateDirect(source.length());
    67             else
    68             else
    68                 bufs[i] = ByteBuffer.allocate(source.length());
    69                 bufs[i] = ByteBuffer.allocate(source.length());
    69 
    70 
    70             bufs[i].put(source.getBytes("8859_1"));
    71             bufs[i].put(source.getBytes("8859_1"));
    71             bufs[i].flip();
    72             bufs[i].flip();
       
    73             total += bufs[i].remaining();
    72         }
    74         }
    73 
    75 
    74         // Get a connection to the server
    76         // Get a connection to the server
    75         InetAddress lh = InetAddress.getLocalHost();
    77         InetAddress lh = InetAddress.getLocalHost();
    76         InetSocketAddress isa = new InetSocketAddress(lh, port);
    78         InetSocketAddress isa = new InetSocketAddress(lh, port);
    77         SocketChannel sc = SocketChannel.open();
    79         SocketChannel sc = SocketChannel.open();
    78         sc.connect(isa);
    80         sc.connect(isa);
    79         sc.configureBlocking(false);
    81         sc.configureBlocking(generator.nextBoolean());
    80 
    82 
    81         // Write the data out
    83         // Write the data out
    82         long bytesWritten = 0;
    84         long rem = total;
    83         do {
    85         while (rem > 0L) {
    84             bytesWritten = sc.write(bufs);
    86             long bytesWritten = sc.write(bufs);
    85         } while (bytesWritten > 0);
    87             if (bytesWritten == 0) {
    86 
    88                 if (sc.isBlocking())
    87         try {
    89                     throw new RuntimeException("write did not block");
    88             Thread.currentThread().sleep(500);
    90                 Thread.sleep(50);
    89         } catch (InterruptedException ie) { }
    91             } else {
       
    92                 rem -= bytesWritten;
       
    93             }
       
    94         }
    90 
    95 
    91         // Clean up
    96         // Clean up
    92         sc.close();
    97         sc.close();
    93     }
    98     }
    94 
    99 
   113         void go() throws Exception {
   118         void go() throws Exception {
   114             bufferTest();
   119             bufferTest();
   115         }
   120         }
   116 
   121 
   117         void bufferTest() throws Exception {
   122         void bufferTest() throws Exception {
       
   123             long total = 0L;
   118             ByteBuffer[] bufs = new ByteBuffer[testSize];
   124             ByteBuffer[] bufs = new ByteBuffer[testSize];
   119             for(int i=0; i<testSize; i++) {
   125             for(int i=0; i<testSize; i++) {
   120                 String source = "buffer" + i;
   126                 String source = "buffer" + i;
   121                 if (generator.nextBoolean())
   127                 if (generator.nextBoolean())
   122                     bufs[i] = ByteBuffer.allocateDirect(source.length());
   128                     bufs[i] = ByteBuffer.allocateDirect(source.length());
   123                 else
   129                 else
   124                     bufs[i] = ByteBuffer.allocate(source.length());
   130                     bufs[i] = ByteBuffer.allocate(source.length());
       
   131                 total += bufs[i].capacity();
   125             }
   132             }
   126 
   133 
   127             // Get a connection from client
   134             // Get a connection from client
   128             SocketChannel sc = null;
   135             SocketChannel sc = null;
   129 
   136 
   136                     if (sc != null)
   143                     if (sc != null)
   137                         break;
   144                         break;
   138                     Thread.sleep(50);
   145                     Thread.sleep(50);
   139                 }
   146                 }
   140 
   147 
       
   148                 sc.configureBlocking(generator.nextBoolean());
       
   149 
   141                 // Read data into multiple buffers
   150                 // Read data into multiple buffers
   142                 long bytesRead = 0;
   151                 long avail = total;
   143                 do {
   152                 while (avail > 0) {
   144                     bytesRead = sc.read(bufs);
   153                     long bytesRead = sc.read(bufs);
   145                 } while (bytesRead > 0);
   154                     if (bytesRead < 0)
       
   155                         break;
       
   156                     if (bytesRead == 0) {
       
   157                         if (sc.isBlocking())
       
   158                             throw new RuntimeException("read did not block");
       
   159                         Thread.sleep(50);
       
   160                     }
       
   161                     avail -= bytesRead;
       
   162                 }
   146 
   163 
   147                 // Check results
   164                 // Check results
   148                 for(int i=0; i<testSize; i++) {
   165                 for(int i=0; i<testSize; i++) {
   149                     String expected = "buffer" + i;
   166                     String expected = "buffer" + i;
   150                     bufs[i].flip();
   167                     bufs[i].flip();