else if ((buffer.getLength() * 2) < textLength)
{
buffer.ensureSize(textLength * 2);
}
final ByteStream target = new ByteStream(buffer, textLength);
final int[] sourceArray = text.getData();
final int endPos = text.getCursor();
for (int i = text.getOffset(); i < endPos; i++)
{
final int sourceItem = sourceArray[i];
if (sourceItem < 0 || sourceItem > MAX_CHAR)
{
continue;
}
if (sourceItem <= 0xFFFF)
{
if (sourceItem >= 0xD800 && sourceItem <= 0xDFFF)
{
// this is an error condition. We ignore it for now ..
continue;
}
target.put((byte) ((sourceItem & 0xff00) >> 8));
target.put((byte) (sourceItem & 0xff));
}
else
{
// compute the weird replacement mode chars ..
final int derivedSourceItem = sourceItem - 0x10000;
final int highWord = 0xD800 | ((derivedSourceItem & 0xFFC00) >> 10);
target.put((byte) ((highWord & 0xff00) >> 8));
target.put((byte) (highWord & 0xff));
final int lowWord = 0xDC00 | (derivedSourceItem & 0x3FF);
target.put((byte) ((lowWord & 0xff00) >> 8));
target.put((byte) (lowWord & 0xff));
}
}
target.close();
return buffer;
}