Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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,
0
ashok3sep
Asked:
ashok3sep
  • 8
  • 8
  • 4
  • +1
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 8
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now