/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2006, by :
* Corporate:
* EADS Astrium SAS
* EADS CRC
* Individual:
* Claude Cazenave
*
* $Id: TestStreamMemoryBuffer.java,v 1.4 2006/09/01 11:05:29 cazenave Exp $
*
* Changes
* -------
* 16 janv. 2006 : Initial public release (CC);
*
*/
package simtools.util.test;
import java.io.IOException;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
import simtools.util.StreamMemoryBuffer;
/**
* A test class for the StreamMemoryBuffer
*
*/
public class TestStreamMemoryBuffer {
/**
* A byte channel to validate memory buffer saving
*/
public static class DumpChannel implements ByteChannel{
public int read(ByteBuffer dst) throws IOException {
// not used
return 0;
}
public void close() throws IOException {
// not used
}
public boolean isOpen() {
// not used
return true;
}
public int write(ByteBuffer src) throws IOException {
int c=0;
while(src.hasRemaining()){
if(c==0){
System.out.print("=>"+src.get());
}
else{
System.out.print(","+src.get());
}
c++;
}
System.out.println();
return c;
}
}
/**
* Display a packet content
* @param b the array from which bytes are to be read
* @param offset the offset within the array of the first byte to be read
* @param length the number of bytes to be read from the given array
*/
static void dump(byte[] b, int offset, int length){
for(int i=0;i<length;i++){
System.out.print(b[i+offset]);
if(i==length-1){
System.out.println();
}
else{
System.out.print(",");
}
}
}
/**
* 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);
}
}