FileDocCategorySizeDatePackage
Transfer.javaAPI DocExample4606Thu Jun 27 17:30:26 BST 2002tuning.nio

Transfer

public class Transfer extends Object implements Runnable

Fields Summary
static int
BYTE_SIZE
static float[]
FloatArray
static byte[]
ByteArray
static FloatBuffer
inFloatBuffer
static MyByteArrayOutputStream
Buff
Constructors Summary
Methods Summary
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();
  
public static voidniotest()

    System.out.println("Testing nio write");
    MyByteArrayOutputStream buf = Buff;
    buf.reset();
    inFloatBuffer.clear();

    long time = System.currentTimeMillis();
    DataOutputStream wrtr = new DataOutputStream(buf);
    inFloatBuffer.put(FloatArray, 0, FloatArray.length);
    wrtr.writeInt(FloatArray.length);
    wrtr.write(ByteArray);
    wrtr.flush();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println("Writing of objects to buffer: " + time);

    InputStream istream = new ByteArrayInputStream(buf.getBuffer());
    System.out.println(" buffer size: " + buf.getBuffer().length);

    time = System.currentTimeMillis();
    DataInputStream rdr = new DataInputStream(istream);
    inFloatBuffer.clear();
    int len = rdr.readInt();
    float[] allFloats = new float[len];
    rdr.readFully(ByteArray, 0, len*4);
    inFloatBuffer.get(allFloats, 0, len);
    rdr.close();
    time = System.currentTimeMillis() - time;
    System.out.println("Reading of objects from buffer: " + time);
    System.out.println("Reading idx 17: " + allFloats [17]);

    buf = null;
    wrtr = null;
    istream = null;
    rdr = null;
    allFloats = null;
    System.gc();
  
public static voidoldtest()

     
     
  
    System.out.println("Testing iterative write");
    MyByteArrayOutputStream buf = Buff;
    buf.reset();

    long time = System.currentTimeMillis();
    DataOutputStream wrtr = new DataOutputStream(buf);
    wrtr.writeInt(FloatArray.length);
    for (int i = 0; i < FloatArray.length ; i++)
      wrtr.writeFloat(FloatArray[i]);
    wrtr.flush();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println("Writing of objects to buffer: " + time);

    InputStream istream = new ByteArrayInputStream(buf.getBuffer());
    System.out.println(" buffer size: " + buf.getBuffer().length);

    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;
    System.out.println("Reading of objects from buffer: " + time);
    System.out.println("Reading idx 17: " + allFloats [17]);

    buf = null;
    wrtr = null;
    istream = null;
    rdr = null;
    allFloats = null;
    System.gc();
  
public static voidoldtest2()

    System.out.println("Testing object write");
    MyByteArrayOutputStream buf = Buff;
    buf.reset();

    long time = System.currentTimeMillis();
    ObjectOutputStream wrtr = new ObjectOutputStream(buf);
    wrtr.writeObject(FloatArray);
    wrtr.flush();
    wrtr.close();
    time = System.currentTimeMillis() - time;
    System.out.println("Writing of objects to buffer: " + time);

    InputStream istream = new ByteArrayInputStream(buf.getBuffer());
    System.out.println(" buffer size: " + buf.getBuffer().length);

    time = System.currentTimeMillis();
    ObjectInputStream rdr = new ObjectInputStream(istream);
    float[] allFloats = (float[]) rdr.readObject();
    rdr.close();
    time = System.currentTimeMillis() - time;
    System.out.println("Reading of objects from buffer: " + time);
    System.out.println("Reading idx 17: " + allFloats [17]);

    buf = null;
    wrtr = null;
    istream = null;
    rdr = null;
    allFloats = null;
    System.gc();
  
public static voidtest()

    niotest();
    oldtest();
    oldtest2();