/* * DataSetStreamProducer.java * * Created on October 12, 2007, 10:11 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package org.das2.dataset; import org.das2.datum.Datum; import org.das2.datum.DatumVector; import org.das2.datum.UnitsUtil; import org.das2.stream.DataTransferType; import org.das2.stream.PacketDescriptor; import org.das2.stream.StreamDescriptor; import org.das2.stream.StreamException; import org.das2.stream.StreamScalarDescriptor; import org.das2.stream.StreamProducer; import org.das2.stream.StreamXDescriptor; import org.das2.stream.StreamYScanDescriptor; import java.io.IOException; import java.io.OutputStream; import java.nio.channels.Channels; import java.nio.channels.WritableByteChannel; import java.util.Map; import java.util.Map.Entry; /** * Configurable class for serializing a DataSet into a das2Stream. This class * handles both VectorDataSets and TableDataSets, and uses java beans properties * to control how the stream is produced. This code subsumes the functionality * of TableUtil.dumpToDas2Stream and VectorUtil.dumpToDas2Stream. * * @author jbf */ public class DataSetStreamProducer { /** Creates a new instance of DataSetStreamProducer */ public DataSetStreamProducer() { } /** * convenient method for writing to an OutputStream. Simply * uses Channels.newChannel to create a WritableByteChannel. */ public void writeStream( OutputStream out ) { this.writeStream( Channels.newChannel(out) ); } /** * writes the stream to the Channel. */ public void writeStream( WritableByteChannel out ) { if ( dataSet instanceof VectorDataSet ) { writeVectorDataSetStream( out ); } else { writeTableDataSetStream( out ); } } private void writeTableDataSetStream( WritableByteChannel out ) { TableDataSet tds= (TableDataSet)dataSet; if (tds.getXLength() == 0) { try { out.close(); } catch (IOException ioe) { //Do nothing. } return; } try { StreamProducer producer = new StreamProducer(out); StreamDescriptor sd = new StreamDescriptor(); Map properties= tds.getProperties(); if ( properties!=null) { for ( Entry e: properties.entrySet() ) { String key= e.getKey(); sd.setProperty(key, e.getValue() ); } } if ( compressed ) { sd.setCompression( "deflate" ); } producer.streamDescriptor(sd); DataTransferType xTransferType; DataTransferType yTransferType; if ( asciiTransferTypes ) { if ( UnitsUtil.isTimeLocation(tds.getXUnits()) ) { xTransferType= DataTransferType.getByName("time24"); } else { xTransferType= DataTransferType.getByName("ascii10"); } yTransferType= DataTransferType.getByName("ascii10"); } else { xTransferType= DataTransferType.getByName("sun_real8"); yTransferType= DataTransferType.getByName("sun_real4"); } PacketDescriptor pd = new PacketDescriptor(); StreamXDescriptor xDescriptor = new StreamXDescriptor(); xDescriptor.setUnits(tds.getXUnits()); xDescriptor.setDataTransferType(xTransferType); pd.setXDescriptor(xDescriptor); String[] planeIds= DataSetUtil.getAllPlaneIds(tds); DatumVector[] yValues = new DatumVector[planeIds.length]; for ( int j=0; j properties= vds.getProperties(); if ( properties!=null) { for ( Entry e: properties.entrySet() ) { String key= e.getKey(); sd.setProperty(key, e.getValue() ); } } if ( compressed ) { sd.setCompression( "deflate" ); } producer.streamDescriptor(sd); DataTransferType xTransferType; DataTransferType yTransferType; if ( asciiTransferTypes ) { if ( UnitsUtil.isTimeLocation(vds.getXUnits()) ) { xTransferType= DataTransferType.getByName("time24"); } else { xTransferType= DataTransferType.getByName("ascii10"); } yTransferType= DataTransferType.getByName("ascii10"); } else { xTransferType= DataTransferType.getByName("sun_real8"); yTransferType= DataTransferType.getByName("sun_real4"); } StreamXDescriptor xDescriptor = new StreamXDescriptor(); xDescriptor.setUnits(vds.getXUnits()); xDescriptor.setDataTransferType(xTransferType); PacketDescriptor pd = new PacketDescriptor(); pd.setXDescriptor(xDescriptor); String[] planeIds= DataSetUtil.getAllPlaneIds(vds); DatumVector[] yValues = new DatumVector[planeIds.length]; for ( int i=0; i