> 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
>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
> 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