// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.nio.ByteBuffer;

public class ByteBufferTest {
   private final static int N = 10000000;
   private final static String s = "Dette er en test";
   private static void test1() {
      ByteBuffer bb = null;
      long t1 = System.currentTimeMillis();
      for(int i = 0; i < N; i++) {
         bb = ByteBuffer.wrap(s.getBytes());
      }
      long t2 = System.currentTimeMillis();
      bb.rewind();
      if(bb.limit() != s.length()) System.out.println("Oops");
      for(int i = 0; i < bb.limit(); i++) if(bb.get() != s.charAt(i)) System.out.println("oops");
      System.out.println("wrap: " + (t2-t1));
   }
   private static void test2() {
      ByteBuffer bb = ByteBuffer.allocate(s.length());
      long t1 = System.currentTimeMillis();
      for(int i = 0; i < N; i++) {
         bb.clear();
         bb.put(s.getBytes());
      }
      long t2 = System.currentTimeMillis();
      bb.rewind();
      if(bb.limit() != s.length()) System.out.println("Oops");
      for(int i = 0; i < bb.limit(); i++) if(bb.get() != s.charAt(i)) System.out.println("oops");
      System.out.println("clear and put: " + (t2-t1));
   }
   public static void main(String[] args) {
      test1();
      test2();
   }
}
