Name: mc57594 Date: 07/15/99
We have a piece of code that performs a translation from a
number (type long) into a String representing an IP address.
This piece of code is included in an auxiliary class in its
original version, as a static method.
When this code is executed under any version of the JRE (JDK),
it works fine, no matter how many times it is run. When the
runtime environment uses HotSpot, the code produces bad results
after a number of executions. The instruction that seems to
raise the problem performs the following operation to remove
the sign from a byte:
n.byteValue() & 0xff
where n is an instance of class Long.
--------------------------------------
This code fails with HotSpot but is OK
when you run java with the -classic
option:
public class Test1
{
public static void main(String argv[])
{
for (long a = 1049856; a <= 1051846; a++)
{
System.out.println(a + "\t" + numberToString(a));
}
}
protected static String numberToString(long theNumber)
{
String returnString = new String("");
Long n = new Long(theNumber);
for (int i = 0; i < 4; i++)
{
returnString = (i < 3 ? ".": "") +
(n.byteValue() & 0xff) +
returnString;
n = new Long(n.longValue() >> 8);
}
return returnString;
}
}
--------------------------------------
This code runs correctly with HotSpot:
public class Test2
{
public static void main(String argv[])
{
for (long a = 1049856; a <= 1051846; a++)
{
String returnString = new String("");
Long n = new Long(a);
for (int i = 0; i < 4; i++)
{
returnString = (i < 3 ? ".": "") +
(n.byteValue() & 0xff) +
returnString;
n = new Long(n.longValue() >> 8);
}
System.out.println(a + "\t" + returnString);
}
}
}
The ouput is correct IP numbers but after a while Test1 gives
output like:
171.43792.11210754.1049160
(Review ID: 84971)
======================================================================