use of com.ibm.j9ddr.vm29.j9.AlgorithmVersion in project openj9 by eclipse.
the class J9ObjectHelper method stringValue.
public static String stringValue(J9ObjectPointer objPointer) throws CorruptDataException {
if (!J9ObjectHelper.getClassName(objPointer).equals("java/lang/String")) {
throw new IllegalArgumentException();
}
// No synchronization needed here because the type of java/lang/String.value is immutable
if (isStringBackedByByteArray == null) {
try {
getObjectField(objPointer, getFieldOffset(objPointer, "value", "[B"));
isStringBackedByByteArray = new Boolean(true);
} catch (NoSuchElementException e) {
getObjectField(objPointer, getFieldOffset(objPointer, "value", "[C"));
isStringBackedByByteArray = new Boolean(false);
}
}
J9ObjectPointer valueObject = isStringBackedByByteArray.booleanValue() ? getObjectField(objPointer, getFieldOffset(objPointer, "value", "[B")) : getObjectField(objPointer, getFieldOffset(objPointer, "value", "[C"));
if (valueObject.isNull()) {
return "<Uninitialized String>";
}
int offset = 0;
int count = getIntField(objPointer, getFieldOffset(objPointer, "count", "I"));
boolean isStringCompressed = false;
AlgorithmVersion version = AlgorithmVersion.getVersionOf(AlgorithmVersion.STRING_COMPRESSION_VERSION);
switch(version.getAlgorithmVersion()) {
case 1:
boolean enableCompression = getBooleanField(objPointer, getFieldOffset(objPointer, "enableCompression", "Z"));
if (enableCompression) {
if (count >= 0) {
isStringCompressed = true;
} else {
count = count & 0x7FFFFFFF;
}
}
break;
default:
offset = getIntField(objPointer, getFieldOffset(objPointer, "offset", "I"));
break;
}
char[] charValue = new char[count];
J9IndexableObjectPointer valueArray = J9IndexableObjectPointer.cast(valueObject);
if (isStringBackedByByteArray.booleanValue()) {
byte[] value = (byte[]) J9IndexableObjectHelper.getData(valueArray);
if (isStringCompressed) {
for (int i = 0; i < count; ++i) {
charValue[i] = byteToCharUnsigned(getByteFromArrayByIndex(value, i));
}
} else {
for (int i = 0; i < count; ++i) {
charValue[i] = getCharFromArrayByIndex(value, i);
}
}
} else {
char[] value = (char[]) J9IndexableObjectHelper.getData(valueArray);
if (isStringCompressed) {
for (int i = 0; i < count; ++i) {
charValue[i] = byteToCharUnsigned(getByteFromArrayByIndex(value, i));
}
} else {
for (int i = 0; i < count; ++i) {
charValue[i] = getCharFromArrayByIndex(value, i);
}
}
}
return new String(charValue, offset, count);
}
Aggregations