/**
* The test entry point
* @param args
*/
public static void main(String[] args) {
StreamMemoryBuffer m=new StreamMemoryBuffer(4,10);
byte[] in={1,2,3,4,5,6};
ByteBuffer bin=ByteBuffer.allocateDirect(6);
byte[] out=new byte[6];
final StreamMemoryBuffer.StreamReader r=m.createReader();
for(int i=0;i<10;i++){
if(i%2==0){
System.out.println("write "+m.write(in,0,in.length-(i%3)));
}
else{
bin.put(in,0,in.length-(i%3));
bin.flip();
System.out.println("write buffer"+m.write(bin));
bin.clear();
}
for(int j=0;j<in.length;j++){
in[j]++;
}
}
for(int i=0;i<12;i++){
try{
int l=r.read(i,0,-1,out,0);
dump(out,0,l);
}
catch(BufferUnderflowException e){
if(i<6){
System.out.println("expected error");
}
else{
throw e;
}
}
catch(BufferOverflowException e){
if(i>=10){
System.out.println("expected error");
}
else{
throw e;
}
}
}
// concurent access...
Thread t=new Thread(){
public void run(){
DumpChannel spy=new DumpChannel();
byte[] out=new byte[6];
long index=-1;
for(int i=0;i<5;i++){
System.out.print("wait...");
index=r.getNextReadIndex(0,index);
System.out.println("get "+index);
int l=r.read(index,0,-1,out,0);
dump(out,0,l);
}
r.resetMark(2);
try {
System.out.println("save "+r.save(spy ,0));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
System.out.println("save "+r.save(spy ,0));
} catch (IOException e) {
e.printStackTrace();
}
}
};
t.start();
for(int i=0;i<10;i++){
for(int j=0;j<in.length;j++){
in[j]=(byte)(i+j);
}
try {
Thread.sleep(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.print("write "+m.write(in,0,in.length-(i%3))+"...");
}
System.exit(0);
}