package org.autoplot.hapiserver; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.Charset; import java.util.Collections; import java.util.logging.Level; import java.util.logging.Logger; import org.das2.datum.Units; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.das2.qds.QDataSet; import org.das2.qds.SemanticOps; import org.das2.qds.ops.Ops; import org.das2.qstream.AsciiTimeTransferType; import org.das2.qstream.TransferType; /** * Format to binary types. Note that TransferTypes use doubles to communicate, * so floating point numbers may not format precisely. * @author jbf */ public class BinaryDataFormatter implements DataFormatter { private static final Logger logger= Logger.getLogger("hapi"); TransferType[] transferTypes; double[] fill; ByteBuffer b; int bufferSize; public BinaryDataFormatter( ) { } @Override public void initialize( JSONObject info, OutputStream out, QDataSet record) { try { transferTypes= new TransferType[record.length()]; fill= new double[record.length()]; bufferSize= 0; int totalFields= 0; JSONArray parameters= info.getJSONArray("parameters"); for ( int i=0; i0 && bytes[ipos]<0 ) ipos--; // unicode characters > 127 are multi-byte. buffer.put( bytes, 0, ipos ); //TODO: we could be in the middle of a UTF-8 letter. } } @Override public double read(ByteBuffer buffer) { throw new UnsupportedOperationException("Not supported."); } @Override public int sizeBytes() { return len; } @Override public boolean isAscii() { return false; } @Override public String name() { return "string"+len; } }; } else if ( stype.equals("double") || stype.equals("integer")) { tt= TransferType.getForName(stype, Collections.singletonMap(QDataSet.UNITS,(Object)u) ); fl= Double.parseDouble( parameter.getString("fill") ); } else { throw new IllegalArgumentException("server is misconfigured, using unsupported type: "+stype ); } int nfields; if ( parameter.has("size") ) { JSONArray ja= (JSONArray)parameter.get("size"); int prod= 1; for ( int j=0; j0 && Ops.valid(d).value()==0 ) { transferTypes[i].write( fill[i], b ); } else { transferTypes[i].write( d.value(), b ); } } byte[] bytes= b.array(); out.write( bytes ); b.flip(); } /** * perform any final operations to the stream. This * DOES NOT close the stream! * @param out */ @Override public void finalize( OutputStream out ) { } }