You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-dev@db.apache.org by Bryan Pendleton <bp...@gmail.com> on 2018/02/24 23:32:22 UTC
Confused about some code in NetworkServerControlImpl.java
Hi Derby developers,
I'm staring at this code in NetworkServerControlImpl.java;
private void writeShort(int value) throws Exception
{
try {
commandOs.writeByte((byte)((value & 0xf0) >> 8 ));
commandOs.writeByte((byte)(value & 0x0f));
}
catch (IOException e)
{
clientSocketError(e);
}
}
I'm not quite sure what this code is doing.
It seems to be mixing some combination of 8-bit shifting
and 4-bit value masking.
I think it might actually be losing/destroying 4 bits of
the 16-bit value (the high 4 bytes in the low byte of
the short).
For example if you call writeShort(23), it emits 0x00, 0x07,
when I think it should emit 0x00,0x17.
I suspect the code should read:
private void writeShort(int value) throws Exception
{
try {
commandOs.writeByte((byte)((value & 0xff00) >> 8 ));
commandOs.writeByte((byte)(value & 0x00ff));
}
catch (IOException e)
{
clientSocketError(e);
}
}
or perhaps
private void writeShort(int value) throws Exception
{
try {
commandOs.writeByte((byte)((value >> 8 ));
commandOs.writeByte((byte)value);
}
catch (IOException e)
{
clientSocketError(e);
}
}
or some such, but really I don't understand it at all.
And, very close to this code is a very similar method:
private void writeByte(int value) throws Exception
{
try {
commandOs.writeByte((byte)(value & 0x0f));
}
catch (IOException e)
{
clientSocketError(e);
}
}
which I think might be damaged for any value in the
range 17-255? It seems like it should probably be:
private void writeByte(int value) throws Exception
{
try {
commandOs.writeByte((byte)(value & 0xff));
}
catch (IOException e)
{
clientSocketError(e);
}
}
or maybe just:
private void writeByte(int value) throws Exception
{
try {
commandOs.writeByte((byte)value);
}
catch (IOException e)
{
clientSocketError(e);
}
}
The thing is, this is some VERY old code. It seems to go
back to the original donation from IBM, more than a
dozen years ago.
So I'm sure I'm just missing something basic.
Has anyone looked at this code before?
Am I just confused?
thanks,
bryan
Re: Confused about some code in NetworkServerControlImpl.java
Posted by Rick Hillegas <ri...@gmail.com>.
Hey Bryan,
My guess would be that this is an attempt to dictate endian-ness. I
don't know why though.
Thanks,
-Rick
On 2/24/18 6:32 PM, Bryan Pendleton wrote:
> Hi Derby developers,
>
> I'm staring at this code in NetworkServerControlImpl.java;
>
> private void writeShort(int value) throws Exception
> {
> try {
> commandOs.writeByte((byte)((value & 0xf0) >> 8 ));
> commandOs.writeByte((byte)(value & 0x0f));
> }
> catch (IOException e)
> {
> clientSocketError(e);
> }
> }
>
> I'm not quite sure what this code is doing.
>
> It seems to be mixing some combination of 8-bit shifting
> and 4-bit value masking.
>
> I think it might actually be losing/destroying 4 bits of
> the 16-bit value (the high 4 bytes in the low byte of
> the short).
>
> For example if you call writeShort(23), it emits 0x00, 0x07,
> when I think it should emit 0x00,0x17.
>
> I suspect the code should read:
>
> private void writeShort(int value) throws Exception
> {
> try {
> commandOs.writeByte((byte)((value & 0xff00) >> 8 ));
> commandOs.writeByte((byte)(value & 0x00ff));
> }
> catch (IOException e)
> {
> clientSocketError(e);
> }
> }
>
> or perhaps
>
> private void writeShort(int value) throws Exception
> {
> try {
> commandOs.writeByte((byte)((value >> 8 ));
> commandOs.writeByte((byte)value);
> }
> catch (IOException e)
> {
> clientSocketError(e);
> }
> }
>
> or some such, but really I don't understand it at all.
>
> And, very close to this code is a very similar method:
>
> private void writeByte(int value) throws Exception
> {
> try {
> commandOs.writeByte((byte)(value & 0x0f));
> }
> catch (IOException e)
> {
> clientSocketError(e);
> }
> }
>
> which I think might be damaged for any value in the
> range 17-255? It seems like it should probably be:
>
> private void writeByte(int value) throws Exception
> {
> try {
> commandOs.writeByte((byte)(value & 0xff));
> }
> catch (IOException e)
> {
> clientSocketError(e);
> }
> }
>
> or maybe just:
>
> private void writeByte(int value) throws Exception
> {
> try {
> commandOs.writeByte((byte)value);
> }
> catch (IOException e)
> {
> clientSocketError(e);
> }
> }
>
> The thing is, this is some VERY old code. It seems to go
> back to the original donation from IBM, more than a
> dozen years ago.
>
> So I'm sure I'm just missing something basic.
>
> Has anyone looked at this code before?
>
> Am I just confused?
>
> thanks,
>
> bryan
>