samer@0: // samer@0: // VecToStream.java samer@0: // samer@0: // samer@0: // Created by Samer Abdallah on Mon Jun 10 2002. samer@0: // Copyright (c) 2002 __MyCompanyName__. All rights reserved. samer@0: // samer@0: samer@0: package samer.units; samer@0: samer@0: import samer.core.*; samer@0: import samer.tools.*; samer@0: import samer.maths.*; samer@0: import java.io.*; samer@0: samer@0: /** Write sequence of vectors to stream in binary format. */ samer@0: samer@0: public class VecToStream extends AnonymousTask { samer@0: Vec x; // the vector itself samer@0: OutputStream out; // stream to write to samer@0: DataOutputStream objout; samer@0: Task task; samer@0: samer@0: public VecToStream(Vec x, OutputStream out) throws Exception samer@0: { samer@0: this.x=x; samer@0: this.out=out; samer@0: objout=new DataOutputStream(out); samer@0: samer@0: double [] a=x.array(); samer@0: //if (a!=null) task=new ArrayObjectWriter(a); samer@0: //else samer@0: task=new IteratorFloatWriter(); samer@0: } samer@0: samer@0: public void dispose() { samer@0: try { samer@0: objout.flush(); samer@0: objout.close(); samer@0: } catch (Exception ex) {} samer@0: samer@0: // close out? samer@0: } samer@0: samer@0: public Task getTask() { return task; } samer@0: samer@0: public void run() throws Exception { task.run(); /* autoflush? */ } samer@0: public void stopping() { samer@0: try { objout.flush(); } samer@0: catch (Exception ex) {} samer@0: } samer@0: samer@0: class ArrayObjectWriter extends AnonymousTask { samer@0: double [] array; samer@0: public ArrayObjectWriter(double [] a) { array=a; } samer@0: public void run() throws Exception { samer@0: // objout.writeObject(array); samer@0: } samer@0: } samer@0: samer@0: class IteratorFloatWriter extends AnonymousTask { samer@0: public void run() throws Exception { samer@0: Vec.Iterator i=x.iterator(); samer@0: while (i.more()) { samer@0: objout.writeFloat((float)i.next()); samer@0: } samer@0: } samer@0: } samer@0: }