@Override
public byte[] encodeValue(final FieldType fieldType, final Object value, final ByteOrder byteOrder)
throws ImageWriteException {
if (!(value instanceof String)) {
throw new ImageWriteException("GPS text value not String", value);
}
final String s = (String) value;
try {
// try ASCII, with NO prefix.
final byte[] asciiBytes = s.getBytes(TEXT_ENCODING_ASCII.encodingName);
final String decodedAscii = new String(asciiBytes, TEXT_ENCODING_ASCII.encodingName);
if (decodedAscii.equals(s)) {
// no unicode/non-ascii values.
final 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;
}
// use Unicode
final TextEncoding encoding;
if (byteOrder == ByteOrder.BIG_ENDIAN) {
encoding = TEXT_ENCODING_UNICODE_BE;
} else {
encoding = TEXT_ENCODING_UNICODE_LE;
}
final byte[] unicodeBytes = s.getBytes(encoding.encodingName);
final byte[] result = new byte[unicodeBytes.length + encoding.prefix.length];
System.arraycopy(encoding.prefix, 0, result, 0, encoding.prefix.length);
System.arraycopy(unicodeBytes, 0, result, encoding.prefix.length, unicodeBytes.length);
return result;
} catch (final UnsupportedEncodingException e) {
throw new ImageWriteException(e.getMessage(), e);
}
}