Passing Data Using Data/Bye Streams
Hello Everyone!
First Let me say thanks for viewing this thread. Second I have been spending a week trying to reverse engineer this code to work and it has been driving me crazy. Basically, I set some variables of ints, shorts, and one ByteArray to place into an object, then into a ByteArrayOutputStream, then into a DataOutputStream. Then this data gets placed inside a datagram packet and I reverse engineer the code to get the original data out again. I hope that makes basic sense. My problem is the original variables that I am trying to get back I can get back my ints and shorts but I cannot get back my original ByteArray which contains a string. This program is a testdriver I need to work. I have included most of the code from the 2 primary classes. I thank you very much again!
//Test Driver Code
public class DriverClass {
static SignalPdu s = new SignalPdu();
static SignalPdu s2 = new SignalPdu();
static EntityID eid = new EntityID();
static byte pDatabyte[];
static String aString = "a";
static int id = 1;
static int encode = 2;
static int tdl = 6;
static int rate = 1;
static int radioId = 100;
static short dataLength = 100;
static int pDataLength = 100;
static ByteArrayOutputStream baos = new ByteArrayOutputStream();
static int encoder = 0;
public static void main(String[] args) {
TestDriver test = new TestDriver();
s.setEntityId(eid);
s.setEncodingScheme(encode);
s.setTdlType(tdl);
s.setSampleRate(rate);
s.setRadioId(radioId);
s.setDataLength(dataLength);
// Create PayLoad of A's in pDatabyte ByteArray
//for (int index = 0; index < 3; index++) {
//for (int index = 0; index < pDataLength; index++) {
aString = aString.concat(aString);
System.out.println("This is the new value of aString: " + aString);
aString = aString.concat("This is a test");
pDatabyte = aString.getBytes();
// aString.
System.out.println("This is the value of the bytearray pDatabyte: "
+ pDatabyte);
s.setpDatabyte(pDatabyte);
// }
// Prepare OutStream and Marshalling
DataOutputStream dos = new DataOutputStream(baos);
s.marshal(dos);
byte[] pduData = baos.toByteArray();
// Create Datagram Packet and Put byte data inside pDatabyte2
DatagramPacket packet = new DatagramPacket(pduData, pduData.length);
//Testing
int pduDataLength = pduData.length;
System.out.println("The length of pduData is currently: " + pduDataLength);
for(int c = 0; c<pduDataLength; c++)
System.out.println("This is the value of pduData at : " + c + " " + pduData[c]);
//End of testing
byte[] pDatabyte2;
pDatabyte2 = packet.getData();
//Testing
int pDatabyte2Length = pDatabyte2.length;
System.out.println("The length of pDatabyte2 is currently: " + pDatabyte2Length);
for(int j = 0; j<pDatabyte2Length; j++)
System.out.println("This is the value of pDatabyte2 at : " + j + " " + pDatabyte2[j]);
//End of Testing
// Create InputStream and prepare to unmarshal data
ByteArrayInputStream baos2 = new ByteArrayInputStream(pDatabyte2);
DataInputStream dis = new DataInputStream(baos2);
s2.unmarshal(dis);
//If statements to test
// Test the Databyte--which is the onlyone that fails
if (s.getDatabyte() == s2.getDatabyte()) {
System.out.println("DataByte is the same");
} else {
System.out.println("DataByte is the (NOT)same");
System.out.println("This is the DataByte of s + s2: "
+ s.getDatabyte() + " " + s2.getDatabyte());
//Next Class
public class SignalPdu extends RadioCommunicationsFamilyPdu implements
Serializable {
/** encoding scheme used, and enumeration */
protected int encodingScheme;
/** tdl type */
protected int tdlType;
/** sample rate */
protected int sampleRate;
/** length od data */
protected short dataLength;
/** number of samples */
protected short samples;
//@SuppressWarnings("unchecked")
public List data = new ArrayList();
protected byte pData[];
/** Constructor */
public SignalPdu() {
}
public int getMarshalledSize() {
int marshalSize = 0;
marshalSize = super.getMarshalledSize();
marshalSize = marshalSize + 2; // encodingScheme
marshalSize = marshalSize + 2; // tdlType
marshalSize = marshalSize + 4; // sampleRate
marshalSize = marshalSize + 2; // dataLength
marshalSize = marshalSize + 2; // samples
marshalSize = marshalSize + this.getDataLength(); // samples
for (int idx = 0; idx < pData.length; idx++) {
System.out
.println("****** <><><> This is before the getMarshallSize loop");
marshalSize = marshalSize + 1*8 ;
// OneByteChunk listElement = data.get(idx);
// marshalSize = marshalSize + listElement.getMarshalledSize();
System.out
.println("****** <><><> This is after the getMarshallSize loop");
}
return marshalSize;
}
public void setEncodingScheme(int pEncodingScheme) {
encodingScheme = pEncodingScheme;
}
@XmlAttribute
public int getEncodingScheme() {
return encodingScheme;
}
public void setTdlType(int pTdlType) {
tdlType = pTdlType;
}
@XmlAttribute
public int getTdlType() {
return tdlType;
}
public void setSampleRate(int pSampleRate) {
sampleRate = pSampleRate;
}
@XmlAttribute
public int getSampleRate() {
return sampleRate;
}
public void setDataLength(short pDataLength) {
dataLength = pDataLength;
}
@XmlAttribute
public short getDataLength() {
return dataLength;
}
// new code
public void setpDatabyte(byte pDatabyte[]) {
pData = pDatabyte;
}
public byte[] getDatabyte() {
return pData;
}
// end new code
public void setSamples(short pSamples) {
samples = pSamples;
}
@XmlAttribute
public short getSamples() {
return samples;
}
public void setData(List pData1) {
System.out
.println("************* The setData method in SignalPdu is being called!!!");
data = pData1;
}
@XmlElementWrapper(name = "dataList")
public List getData() {
return data;
}
public void marshal(DataOutputStream dos) {
System.out
.println("**********%%%%%%%%%%% 1-2. This is the SignalPdu Class super.marshal(dos) method this is pData" + " " + pData);
String test;
byte dataByte[];
int marshalSize = 0;
try {
dos.writeShort((short) encodingScheme);
dos.writeShort((short) tdlType);
dos.writeInt((int) sampleRate);
dos.writeShort((short) dataLength);
dos.writeShort((short) samples);
System.out
.println("**********%%%%%%%%%%% 2-2. This is the SignalPdu Class super.marshal(dos) method ");
for (int idx1 = 0; idx1 < pData.length; idx1++) {
dos.write(pData[idx1]);
System.out
.println("**********%%%%%%%%%%% 3-2. This is the SignalPdu Class super.marshal(dos) method - This is value of dos "
+ dos);
}
}
catch (Exception e) {
System.out.println(e);
}
//System.out
//.println("**********%%%%%%%%%%% 4-2. This is the SignalPdu Class super.marshal(dos) method ");
} // end of marshal method
public void unmarshal(DataInputStream dis) {
//super.unmarshal(dis);
System.out
.println("**********()()()()()() SignalPDU class, unmarshall method being called");
try {
encodingScheme = (int) dis.readUnsignedShort();
tdlType = (int) dis.readUnsignedShort();
System.out.println("c in the SignalPdu Class tdlType is equal to = " + tdlType);
sampleRate = dis.readInt();
dataLength = dis.readShort();
System.out.println("This is the unmarshall method of the signalpdu..... this is that datalength values: " + dataLength);
samples = dis.readShort();
String test2;
System.out.println("This is the unmarshalled method of the SignalPdu right before the loop");
for (int idx = 11; idx < pData.length; idx++) {
// dis.read
// test2 = dis.readUTF();
pData[idx] = dis.readByte();
System.out.println("&&&*** This is pData at unmarshal method of SignalPdu **&&&" + pData);
System.out
.println("This result is from the unmarshal method which accepts the DataInputStream dis looking for string: "
);
System.out
.println("This result is from the unmarshal method which accepts the DataInputStream dis:= "
+ pData[idx]);
}
} // end try
catch (Exception e) {
System.out.println("This is the unmarshalled method of the SignalPdu");
System.out.println(e);
System.out.println("This is the unmarshalled method of the SignalPdu");
}
} // end of unmarshal method
|