Java Basics

hallo Everybody,

public class update
{
int lengthcode = 10;
byte[] lengtharray = {0,(byte)lengthcode};
byte command = 20;
byte[] command = {0,command};

System.arraycopy(command,0,lengtharray,lengtharray.length,command.length);

String client = "hallo";
byte[] stringLength = {0,(byte)client.length};

byte[] stringBytes = client.getBytes("UTF-8");
byte[] temp1 = new byte[ stringLength.length + stringBytes.length ] ;
System.arraycopy( strarrayLength, 0, temp1, 0, strarrayLength.length ) ;
System.arraycopy( stringBytes, 0, temp1, strarrayLength.length, stringBytes.length ) ;

/////////////// now i need to add the length of the stringlength with the lengtharray and update the lengtharay without disturbing the other elements
all the other elements i should have

System.arraycopy(temp1,0,lengtharray,lengtharray.length,temp1.length);

how can i do that

With Regards,
LVL 5
ashok3sepAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSoftware engineerCommented:
Remark:

>>byte command = 20;
>>byte[] command = {0,command};

won't compile (twice the same variable name used)
0
CEHJCommented:
Is your intention to write these to stream only?
0
CEHJCommented:
... if so, you'll find it a lot easier to forget about the arrays - just write with DataOutputStream
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

zzynxSoftware engineerCommented:
Sure this is right?
>> System.arraycopy(command,0,lengtharray,lengtharray.length,command.length);

public static void arraycopy(Object src,  // the source array.
                                         int srcPos,  // starting position in the source array.
                                         Object dest, // the destination array.
                                         int destPos, // starting position in the destination data.            // <<<<< lengtharray.length???
                                         int length)  // the number of array elements to be copied.
0
ashok3sepAuthor Commented:
I had to send a certain set of fields to the server in the properway

so i am using a byte[] array to write to the stream
initially i am sending a length of the data i need to send

but later on when i send strings to the server the length of the string has to be addted to the initial length

so how can i do that.

that was my question.
hope u understand now almost clearly

With regards
Vardhan
0
zzynxSoftware engineerCommented:
What about the remarks I made?
I think the are still valid.
0
ashok3sepAuthor Commented:
about your remarks you are right.

i have mis typed it

it should be
byte[] command_code

and NOT command

With Regards,
Vardhan
0
zzynxSoftware engineerCommented:
And what about the 4th parameter of
>> System.arraycopy(command,0,lengtharray,lengtharray.length,command.length);

Could you please comment what you're trying to do with this line?



0
CEHJCommented:
From what you say, you should probably do something more like

String s = "qwerty";
byte[] bytes = s.getBytes("UTF8");
byte length = (byte)bytes.length;
dis.write(length); // (Using DataOutputStream)
dis.write(bytes);

There's no need to be copying bytes into arrays first, unless you want to hang onto them for later use, in which case, you should probably be using a dedicated class to hold these. You would then give that class something like a writeToStream method that does the above
0
ashok3sepAuthor Commented:
i am appending the commandcode[] values  to the lengtharray[] values

so for example it will look like this if you print it out

{0,10,0,20}
// so now i give a string and append to the same array

String str = "hallo";

the question is i have already created a length of 10 in the array.

now i need to add this str.length to that value 10 so that it automatically becomes 16

i dont want to do it manually.

when i speicify the string i has to be updated.
how can i acheive it

With Regards,

Vardhan
0
CEHJCommented:
>>i am appending the commandcode[] values  to the lengtharray[] values

Why?
0
CEHJCommented:
>>now i need to add this str.length to that value 10 so that it automatically becomes 16

and if you have to compute the total length, why do you do that *before* you have assembled all the data?
0
ashok3sepAuthor Commented:
Hai,
I would like to thank you for your pateince to answer my queries so politely.

This is the way that i should model my program to follow the protocol rule.

only INIT command doesnt have DATA field
all other commands have DATA field

1st field ------------- length , ------- 2 bytes
2nd field -.--------- commandcode, --------2 bytes
3 field ----------- if there is some data to be added (data can be a STRING or WORD ) --------- 2* n bytes

if my data is string then the string length is added to the 1st field length.

4th field ------------ Checksum ------------- 2 bytes

all these fields i write to a byte array and then to the socket.

with regards,

Vardhan
0
CEHJCommented:
a. How is the 1st length field set out? A word with high byte followed by low?
b. How do you compute the checksum?
0
ashok3sepAuthor Commented:
Hallo CEHJ,

It is the same way as you say

a high byte and then a low byte

for example if length is 8
then {0,8} is stored in a bytearray[]
checksum is calculated in the way given below

public byte[] calculateCheckSum(byte[] identify)
{
      int sum = 0;
      message = identify;
      for(int i = 0; i< message.length; i+=2)
      {
            sum += (message[i] * 256);
            sum += (message[i+1]);
      }
byte[] checksumdata = {(byte)(sum / 256) ,(byte)(sum % 256 ) };

byte[] temp3 = new byte[ message.length + checksumdata.length] ;

System.arraycopy(message, 0, temp3, 0, message.length ) ;
System.arraycopy(checksumdata, 0, temp3, message.length, checksumdata.length ) ;
            
message = temp3 ;
            
return message;

}

Hope you can understand

WithRegards,

Ashok.G
0
CEHJCommented:
           byte[] init = new byte[4];

            // Assume command '20' (these should be named constants, not literals)
            init[2] = 0;
            init[3] = 20;


            // Get some test data
            final int TEST_LENGTH = 300;
            StringBuffer sb = new StringBuffer(TEST_LENGTH);
            String letters = "abcdefghijklmnopqrstuvwxyz";
            for(int i = 0;i < TEST_LENGTH;i++) {
                  sb.append(letters.charAt((int)(Math.random() * letters.length())));
            }
            String data = sb.toString();


            // Get data and length
            byte[] dataBytes = data.getBytes("UTF8");
            int length = dataBytes.length;
            if (length > 0xFFFF) {
                  throw new RuntimeException("True data length cannot be represented properly");
            }

            // Set length
            init[0] = (byte)(length >> 8);
            init[1] = (byte)(length & 0xFF);

            // Calc checksum (you probably need to check and test the checksum routine)
            byte[] checksumData = calculateCheckSum(init);

            // Write to stream
            out.write(init);
            out.write(dataBytes);
            out.write(checksumData);

0
ashok3sepAuthor Commented:
Hallo CEHJ,

what you say is correct,
but i want in the way that everything has to be appended to INIT array and then finally writing this array to the stream.
thats what my protocol rule says,

if i send in otherways it says your command cannot be identified .
please check your structure.

With regasrds,

Ashok
0
CEHJCommented:
Can you show the bit where you write to the stream?
0
ashok3sepAuthor Commented:
//// INIT Command------------

//// Here is how i write to a stream.
first i gather all the datas and put it in a byte array and then finally i write to the stream.

////Here begins the code

int lengthByte = 6;
byte[] init = createMessage.createLengthField(lengthByte);
byte command = 2;
init = createMessage.createCommandField(init,command);
init = createMessage.createCheckSumField(init);
bos.write(init);
bos.flush();

With Regards,
vardhan
0
ashok3sepAuthor Commented:
I would like to have my points refunded to me again as i did the piece of code myself.

bos.write(identify);
bos.flush();
while ( bis.available() < 0)
{Thread.sleep(1000);}
      
byte[] identifyAcknowledgement = new byte[8];
for(int i = 0; i < identifyAcknowledgement.length ; i++)
{
      identifyAcknowledgement[i]=(byte)bis.read();
}
Thread.sleep(1000);

WithRegards,

Ashok.G
0
moduloCommented:
PAQed with points refunded (25)

modulo
Community Support Moderator
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.