FileDocCategorySizeDatePackage
Transfer3.javaAPI DocExample5571Thu Jun 27 19:51:36 BST 2002tuning.nio

Transfer3

public class Transfer3 extends Object implements Runnable

Fields Summary
static int
NUM_FLOATS
static float[]
FloatArray
static int
BYTE_SIZE
static byte[]
ByteArray
static byte[]
ByteArray2
static ByteBuffer
inByteBuffer
static ByteBuffer
outByteBuffer
static FloatBuffer
inFloatBuffer
static FloatBuffer
outFloatBuffer
static int
PORT
Constructors Summary
Methods Summary
public static voidclientTest()

    niotestClient();
    pause();
    oldtestClient2();
    pause();
    oldtestClient();
    pause();
    niotestClient();
    pause();
    oldtestClient2();
    pause();
    oldtestClient();
  
public static voidcreateFloats()

    for (int i =0; i < FloatArray.length; i++)
      FloatArray[i] = ((float) i)/3.2567F;
  
public static voidmain(java.lang.String[] args)


      
     
  
    createFloats();
    test(args);
  
public static voidniotestClient()

    outFloatBuffer.clear();
    Socket client = new Socket("localhost", PORT);
    OutputStream out = client.getOutputStream();

    long time = System.currentTimeMillis();
    DataOutputStream wrtr = new DataOutputStream(out);
    outFloatBuffer.put(FloatArray, 0, FloatArray.length);
    wrtr.writeInt(FloatArray.length);
    wrtr.write(ByteArray, 0, FloatArray.length*4);
    wrtr.flush();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    client.close();
    System.out.println("Writing of objects to buffer: " + time);
  
public static voidniotestServer()

    inFloatBuffer.clear();
    ServerSocket server = new ServerSocket(PORT);
    Socket client = server.accept();
    InputStream istream = client.getInputStream();
 
    long time = System.currentTimeMillis();
    DataInputStream rdr = new DataInputStream(istream);
    int len = rdr.readInt();
    float[] allFloats = new float[len];
    rdr.readFully(ByteArray, 0, len*4);
    inFloatBuffer.get(allFloats, 0, len);
    time = System.currentTimeMillis() - time;
    rdr.close();
    client.close();
    server.close();
    System.out.println("Reading of floats into buffer: " + time);

    allFloats = null;
    System.gc();
  
public static voidoldtestClient()

    Socket client = new Socket("localhost", PORT);
    OutputStream out = client.getOutputStream();
 
    long time = System.currentTimeMillis();
    DataOutputStream wrtr = new DataOutputStream(out);
    wrtr.writeInt(FloatArray.length);
    for (int i = 0; i < FloatArray.length ; i++)
    {
      wrtr.writeFloat(FloatArray[i]);
    }
    wrtr.flush();
    wrtr.close();
    client.close();
    time = System.currentTimeMillis() - time;
    System.out.println("Looped writing of floats to socket: " + time);
  
public static voidoldtestClient2()

    Socket client = new Socket("localhost", PORT);
    OutputStream out = client.getOutputStream();
 
    long time = System.currentTimeMillis();
    ObjectOutputStream wrtr = new ObjectOutputStream(out);
    wrtr.writeObject(FloatArray);
    wrtr.flush();
    wrtr.close();
    client.close();
    time = System.currentTimeMillis() - time;
    System.out.println("writing of float array to socket: " + time);
  
public static voidoldtestServer()

    ServerSocket server = new ServerSocket(PORT);
    Socket client = server.accept();

    InputStream istream = client.getInputStream();

    long time = System.currentTimeMillis();
    DataInputStream rdr = new DataInputStream(istream);
    int len = rdr.readInt();
    float[] allFloats = new float[len];
    for (int i = 0; i < len ; i++)
    {
      allFloats[i] = rdr.readFloat();
    }
    rdr.close();
    time = System.currentTimeMillis() - time;
    client.close();
    server.close();
    System.out.println("Looped reading of floats from socket: " + time);

    allFloats = null;
    System.gc();
  
public static voidoldtestServer2()

    ServerSocket server = new ServerSocket(PORT);
    Socket client = server.accept();

    InputStream istream = client.getInputStream();

    long time = System.currentTimeMillis();
    ObjectInputStream rdr = new ObjectInputStream(istream);
    float[] allFloats = (float[]) rdr.readObject();
    rdr.close();
    time = System.currentTimeMillis() - time;
    client.close();
    server.close();
    System.out.println("reading of float array from socket: " + time);

    allFloats = null;
    System.gc();
  
public static voidpause()

    Thread.sleep(5000);
  
public voidrun()

    try{clientTest();}
    catch(Exception e){e.printStackTrace();}
  
public static voidserverTest()

    niotestServer();
    pause();
    oldtestServer2();
    pause();
    oldtestServer();
    pause();
    niotestServer();
    pause();
    oldtestServer2();
    pause();
    oldtestServer();
  
public static voidtest(java.lang.String[] args)

    (new Thread(new Transfer3())).start();
    serverTest();