 1Content-type: application/x-Unknown; charset=UTF8package nmc.common;

import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class PolledDataReply extends Reply {
    int      channelNumber;
    boolean  endOfData;
    int      subseqNo;
    long     timestamp;
    byte[]   data;

    public PolledDataReply(DataInputStream dis) throws IOException {
        this.decerealize(dis);
    }
    
    public PolledDataReply(Message m, TimestampedData dataSample) {
	PollCmd pc = (PollCmd)m;
	this.channelNumber = pc.channelNumber;
    	this.len = 4+1+4+8+dataSample.data.length;

    	this.seqNo = pc.seqNo;
	this.endOfData = true;
	this.subseqNo = 0;
        this.timestamp = dataSample.timestamp;
	this.data = dataSample.data; // no reason to copy it
    }
    
    public void decerealize(DataInputStream dis) throws IOException {
	super.decerealize(dis);
	this.channelNumber = dis.readInt();
	this.endOfData     = dis.readBoolean();
	this.subseqNo      = dis.readInt();
        this.timestamp     = dis.readLong();
	this.data     = new byte[this.len-4-1-4-8];
	dis.read(data);
    }

    public int getMessageType(){
	return POLLED_DATA_REPLY;
    }
    
    public byte[] getData() {
    	return data;
    }
    public int getChannelNumber() {
	return this.channelNumber;
    }

    public long getTimestamp() {
	return this.timestamp;
    }
    
    public void cerealize(DataOutputStream s) throws IOException {
    	super.cerealize(s);

	s.writeInt      (channelNumber);
	s.writeBoolean  (endOfData);
	s.writeInt      (subseqNo);
	s.writeLong     (timestamp);

	if (data != null) {
	    System.out.println("Cerealizing PolledDataReply, data.length="
			       + data.length);
	} else {
	    System.out.println("Cerealizing PolledDataReply, data is null");
	}
	s.write(data, 0, data.length);
    }


    public String toString(){
	
	char[] better = new char[data.length];
	for (int i=0; i<data.length; i++) {
	    better[i] = (char)data[i];
	}
	String theData = new String(better);

	return "%<%msgType=PolledDataReply%seqNo="
	       + this.seqNo
	       + "%len="
	       + this.len
	       + "%channelNumber="
	       + this.channelNumber
	       + "%endOfData="
	       + this.endOfData
	       + "%subseqNo="
	       + this.subseqNo
	       + "%timestamp="
	       + this.timestamp
	       + "%data="
	       + theData
	       + "%>%";
    }


}
