Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XPTitle info tag can be an ASCII string #1

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
* Windows XP onwards store some tags using UTF-16LE, but the field type is byte
* - here we deal with this.
*/
public class TagInfoXpString extends TagInfo {
public class TagInfoXpString extends TagInfoAsciiOrByte {

public TagInfoXpString(final String name, final int tag, final int length,
final TiffDirectoryType directoryType) {
super(name, tag, FieldType.BYTE, length, directoryType);
super(name, tag, length, directoryType);
}

@Override
Expand All @@ -41,23 +42,34 @@ public byte[] encodeValue(final FieldType fieldType, final Object value, final B
if (!(value instanceof String)) {
throw new ImageWriteException("Text value not String", value);
}
final String s = (String) value;
try {
return s.getBytes("UTF-16LE");
} catch (final UnsupportedEncodingException cannotHappen) {
return null;

if (fieldType == FieldType.ASCII) {
return super.encodeValue(fieldType, value, byteOrder);
} else if (fieldType == FieldType.BYTE) {
final String s = (String) value;
try {
return s.getBytes("UTF-16LE");
} catch (final UnsupportedEncodingException cannotHappen) {
return null;
}
} else {
throw new ImageWriteException("Invalid data", value);
}
}

@Override
public String getValue(final TiffField entry) throws ImageReadException {
if (entry.getFieldType() != FieldType.BYTE) {
throw new ImageReadException("Text field not encoded as bytes.");
if (entry.getFieldType() == FieldType.ASCII) {
return (String)super.getValue(entry);
}
try {
return new String(entry.getByteArrayValue(), "UTF-16LE");
} catch (final UnsupportedEncodingException cannotHappen) {
return null;
else if (entry.getFieldType() == FieldType.BYTE) {
try {
return new String(entry.getByteArrayValue(), "UTF-16LE");
} catch (final UnsupportedEncodingException cannotHappen) {
return null;
}
} else {
throw new ImageReadException("Text field not encoded as ascii or bytes.");
}
}
}