Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

Trying to understand some C/C++ code

Thread view: 
bufferOverflow - 25 Jul 2007 05:57 GMT
Greetings all,

I am trying to understand a piece of C++ code that I am trying to port
to Java.

void Buffer::packInteger(int value){

 int netval = htonl(value); // convert to network order (big-endian)
 data = (char *) realloc(data, datalen + 4); //allocate datalen + 4
bytes to data

 char* temp = data + datalen; //temp is allocated a pointer to (data
+datalen) ?

 memcpy(temp, &netval, 4); // copy the first 4 bytes from netval to
temp;
 datalen += 4; //increment datalen by 4

}
Now if I understood the comments in the code correctly, I can't seem
to understand how the integer called "value" is being packed even at
all in the code above!

My stab of the code above in Java is something like this -

public void packInteger(int value) {
                //b is defined as byte b[ ] = new byte[4];
               b[0] = (byte) ((val >> 24) & 255);
               b[1] = (byte) ((val >> 16) & 255);
               b[2] = (byte) ((val >> 8)  & 255);
               b[3] = (byte) (val &  255);
}

AFAICT, the code above shifts 24,26,8 and 0 places to the right and
then ANDs it with 255 to get the values at those position
respectively. Is this how it's done ? Or am I missing some critical
pieces. I think I am. :-(

Thanks!
bufferOverflow - 25 Jul 2007 06:04 GMT
> Greetings all,
>
[quoted text clipped - 27 lines]
>                 b[2] = (byte) ((val >> 8)  & 255);
>                 b[3] = (byte) (val &  255);

Should be:
               b[0] = (byte) ((value >> 24) & 255);
               b[1] = (byte) ((value >> 16) & 255);
               b[2] = (byte) ((value >> 8)  & 255);
               b[3] = (byte) (value &  255);
Patricia Shanahan - 25 Jul 2007 06:17 GMT
>> Greetings all,
>>
[quoted text clipped - 33 lines]
>                 b[2] = (byte) ((value >> 8)  & 255);
>                 b[3] = (byte) (value &  255);

The overall function of the C code seems to be to append value,
expressed in network order, to a byte buffer, presumably for purposes of
output to a stream?

Is it possible to look at this another layer up in the code? Maybe all
you need is to attach a DataOutputStream to the output and use its
writeInt method?

Patricia
bufferOverflow - 25 Jul 2007 08:41 GMT
Hi Patricia and Roedy,

> >> Greetings all,
>
[quoted text clipped - 37 lines]
> expressed in network order, to a byte buffer, presumably for purposes of
> output to a stream?

Yes, that's the end output.
The data buffer in Java code is b[].
In C++ code, it's the data.

> Is it possible to look at this another layer up in the code? Maybe all
> you need is to attach a DataOutputStream to the output and use its
> writeInt method?

The writeInt() method writes the passed in "value" parameter to the
byte ? Maybe you were mean writeByte() ? If you meant writeInt(), this
isn't possible because down the wire, it has to be sent as individual
bytes.

The whole library's central piece is this buffer. I can't write to a
OutputStream just yet in here (as in a Socket), this whole writing-to-
Socket business is taken care of by a encoder/decoder working one
layer above this buffer class which takes care of things like encoding/
decoding the whole buffer (bytes [numbers,strings etc..]) to a stream
(socket).

Thanks for your input Patricia and Roedy but I think my concept is
going a bit crusty here. I'll get back when I understand what the heck
I am doing!
Thanks again.
Lew - 25 Jul 2007 15:12 GMT
> The writeInt() method writes the passed in "value" parameter to the
> byte ? Maybe you were mean writeByte() ? If you meant writeInt(), this
> isn't possible because down the wire, it has to be sent as individual
> bytes.

Isn't that exactly what writeInt() does?

Signature

Lew

Patricia Shanahan - 25 Jul 2007 17:23 GMT
> Hi Patricia and Roedy,
...
>> Is it possible to look at this another layer up in the code? Maybe all
>> you need is to attach a DataOutputStream to the output and use its
[quoted text clipped - 4 lines]
> isn't possible because down the wire, it has to be sent as individual
> bytes.

I find this remark rather confusing, because you seem to be telling me
that you cannot use writeInt because you need to do exactly what
DataOutputStream's writeInt does.

> The whole library's central piece is this buffer. I can't write to a
> OutputStream just yet in here (as in a Socket), this whole writing-to-
> Socket business is taken care of by a encoder/decoder working one
> layer above this buffer class which takes care of things like encoding/
> decoding the whole buffer (bytes [numbers,strings etc..]) to a stream
> (socket).

The data output stream could be based on a ByteArrayOutputStream.

Patricia
Roedy Green - 25 Jul 2007 07:10 GMT
>public void packInteger(int value) {
>                 //b is defined as byte b[ ] = new byte[4];
>                b[0] = (byte) ((val >> 24) & 255);
>                b[1] = (byte) ((val >> 16) & 255);
>                b[2] = (byte) ((val >> 8)  & 255);
>                b[3] = (byte) (val &  255);
see http://mindprod.com/jgloss/endian.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Nigel Wade - 25 Jul 2007 10:22 GMT
> Greetings all,
>
[quoted text clipped - 18 lines]
> to understand how the integer called "value" is being packed even at
> all in the code above!

It is converted to a network byte order integer netval. netval is "packed" using
memcpy to the end of the newly allocated storage area.

> My stab of the code above in Java is something like this -
>
[quoted text clipped - 10 lines]
> respectively. Is this how it's done ? Or am I missing some critical
> pieces. I think I am. :-(

There should be no need to do this operation in Java. All integers are written
in network byte order, regardless of the hardware platform. It can be made into
a NOOP if you use an internal buffer and write the integer to it.

>   int netval = htonl(value); // convert to network order (big-endian)

not required, use ByteBuffer later to do it for us.

>   data = (char *) realloc(data, datalen + 4); //allocate datalen + 4

byte[] newData = new byte[data.length+4];
System.arraycopy(data,0,newData,0,data.length);
data = newData;

> bytes to data
>
[quoted text clipped - 3 lines]
>   memcpy(temp, &netval, 4); // copy the first 4 bytes from netval to
> temp;

Use a ByteBuffer to wrap the byte array, then we can use the byte ordered
methods to store the int in network byte order (the default ByteBuffer is
BIG_ENDIAN which is the same as network byte order).

ByteBuffer bb = ByteBuffer.wrap(data, datalen, 4);
bb.putInt(int);

>   datalen += 4; //increment datalen by 4

datalen = data.length;

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.