public byte[] encodeValue(FieldType fieldType, Object value,
int byteOrder) throws ImageWriteException
{
if (!(value instanceof String))
throw new ImageWriteException("Text value not String: " + value
+ " (" + Debug.getType(value) + ")");
String s = (String) value;
try
{
// try ASCII, with NO prefix.
byte asciiBytes[] = s
.getBytes(TEXT_ENCODING_ASCII.encodingName);
String decodedAscii = new String(asciiBytes,
TEXT_ENCODING_ASCII.encodingName);
if (decodedAscii.equals(s))
{
// no unicode/non-ascii values.
byte result[] = new byte[asciiBytes.length
+ TEXT_ENCODING_ASCII.prefix.length];
System.arraycopy(TEXT_ENCODING_ASCII.prefix, 0, result, 0,
TEXT_ENCODING_ASCII.prefix.length);
System.arraycopy(asciiBytes, 0, result,
TEXT_ENCODING_ASCII.prefix.length,
asciiBytes.length);
return result;
}
else
{
// use unicode
byte unicodeBytes[] = s
.getBytes(TEXT_ENCODING_UNICODE.encodingName);
byte result[] = new byte[unicodeBytes.length
+ TEXT_ENCODING_UNICODE.prefix.length];
System.arraycopy(TEXT_ENCODING_UNICODE.prefix, 0, result,
0, TEXT_ENCODING_UNICODE.prefix.length);
System.arraycopy(unicodeBytes, 0, result,
TEXT_ENCODING_UNICODE.prefix.length,
unicodeBytes.length);
return result;
}
}
catch (UnsupportedEncodingException e)
{
throw new ImageWriteException(e.getMessage(), e);
}
}