Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old April 22nd, 2009, 10:18 PM
Authorized User
 
Join Date: Feb 2009
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
Reply With Quote
  #2 (permalink)  
Old April 23rd, 2009, 12:20 PM
Authorized User
 
Join Date: Feb 2009
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convert byte to bytearra[]

Hello Everyone!
I think I might have a solution to my own problem. The Output streams I do not think are letting me pass data that is not of primitive type. I was trying to pass a byte[] and it was coming up null everytime on the other end. My idea is to pass in a loop a bunch of bytes and then put them into a bytearray on the other side. To me it makes sense. I have had some issues trying to perform this conversion.

It is possible to convert several bytes into a byte[] array, correct?

Thanks a lot,
d-mart
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing multiple data farra ASP.NET 2.0 Professional 0 April 25th, 2006 07:50 PM
Passing data between forms pactite Access 5 June 4th, 2005 03:02 PM
passing data texasraven HTML Code Clinic 0 March 3rd, 2004 04:03 PM
Passing URL data Brian_S Dreamweaver (all versions) 2 December 1st, 2003 07:22 AM



All times are GMT -4. The time now is 02:41 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.