jdk/test/java/nio/channels/SocketChannel/VectorIO.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @summary Test socketchannel vector IO
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @library ..
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.misc.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class VectorIO {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static int port = 40170;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static int testSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        testSize = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        for(int i=15; i<18; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            testSize = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static void runTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        System.err.println("Length " + testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Server sv = new Server(testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        sv.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                Thread.currentThread().sleep(200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            } catch (InterruptedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                if (sv.finish(8000) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                    throw new Exception("Failed: Error in server thread");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } while (!sv.ready);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        bufferTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (sv.finish(8000) == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            throw new Exception("Failed: Length = " + testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    static void bufferTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        ByteBuffer[] bufs = new ByteBuffer[testSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        for(int i=0; i<testSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            String source = "buffer" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (generator.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                bufs[i] = ByteBuffer.allocateDirect(source.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                bufs[i] = ByteBuffer.allocate(source.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            bufs[i].put(source.getBytes("8859_1"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            bufs[i].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // Get a connection to the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        InetAddress lh = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        InetSocketAddress isa = new InetSocketAddress(lh, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        SocketChannel sc = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        sc.connect(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        sc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        // Write the data out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        long bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            bytesWritten = sc.write(bufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        } while (bytesWritten > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            Thread.currentThread().sleep(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (InterruptedException ie) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static class Server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        extends TestThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        static int port = 40170;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        static Random generator = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        int testSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        volatile boolean ready = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        Server(int testSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            super("Server " + testSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            this.testSize = testSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        void go() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            bufferTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        void bufferTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ByteBuffer[] bufs = new ByteBuffer[testSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            for(int i=0; i<testSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                String source = "buffer" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                if (generator.nextBoolean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                    bufs[i] = ByteBuffer.allocateDirect(source.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    bufs[i] = ByteBuffer.allocate(source.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            // Get a connection from client
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            ServerSocketChannel ssc = ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            SocketChannel sc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                ssc.configureBlocking(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                InetAddress lh = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                InetSocketAddress isa = new InetSocketAddress(lh, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                ssc.socket().bind(isa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                ready = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    sc = ssc.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    if (sc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    Thread.sleep(50);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                // Read data into multiple buffers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                long bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    bytesRead = sc.read(bufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                } while (bytesRead > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                // Check results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                for(int i=0; i<testSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    String expected = "buffer" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    bufs[i].flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    int size = bufs[i].capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    byte[] data = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    for(int j=0; j<size; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        data[j] = bufs[i].get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    String message = new String(data, "8859_1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    if (!message.equals(expected))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        throw new Exception("Wrong data: Got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                                            + message + ", expected "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                                            + expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                // Clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                ssc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                if (sc != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    sc.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
}