Changeset 13061 in josm for trunk/src/com/drew/metadata/iptc/IptcReader.java
- Timestamp:
- 2017-10-30T22:46:09+01:00 (8 years ago)
- File:
-
- 1 edited
-
trunk/src/com/drew/metadata/iptc/IptcReader.java (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/iptc/IptcReader.java
r10862 r13061 1 1 /* 2 * Copyright 2002-201 6Drew Noakes2 * Copyright 2002-2017 Drew Noakes 3 3 * 4 4 * Licensed under the Apache License, Version 2.0 (the "License"); … … 29 29 import com.drew.metadata.Directory; 30 30 import com.drew.metadata.Metadata; 31 import com.drew.metadata.StringValue; 31 32 32 33 import java.io.IOException; 34 import java.nio.charset.Charset; 33 35 import java.util.Collections; 34 36 … … 56 58 public static final int POST_DATA_RECORD = 9; 57 59 */ 60 private static final byte IptcMarkerByte = 0x1c; 58 61 59 62 @NotNull … … 67 70 for (byte[] segmentBytes : segments) { 68 71 // Ensure data starts with the IPTC marker byte 69 if (segmentBytes.length != 0 && segmentBytes[0] == 0x1c) {72 if (segmentBytes.length != 0 && segmentBytes[0] == IptcMarkerByte) { 70 73 extract(new SequentialByteArrayReader(segmentBytes), metadata, segmentBytes.length); 71 74 } … … 107 110 } 108 111 109 if (startByte != 0x1c) {112 if (startByte != IptcMarkerByte) { 110 113 // NOTE have seen images where there was one extra byte at the end, giving 111 114 // offset==length at this point, which is not worth logging as an error. 112 115 if (offset != length) 113 directory.addError("Invalid IPTC tag marker at offset " + (offset - 1) + ". Expected '0x 1c' but got '0x" + Integer.toHexString(startByte) + "'.");116 directory.addError("Invalid IPTC tag marker at offset " + (offset - 1) + ". Expected '0x" + Integer.toHexString(IptcMarkerByte) + "' but got '0x" + Integer.toHexString(startByte) + "'."); 114 117 return; 115 118 } … … 164 167 } 165 168 166 String string = null;167 168 169 switch (tagIdentifier) { 169 170 case IptcDirectory.TAG_CODED_CHARACTER_SET: 170 171 byte[] bytes = reader.getBytes(tagByteCount); 171 String charset = Iso2022Converter.convertISO2022CharsetToJavaCharset(bytes); 172 if (charset == null) { 172 String charsetName = Iso2022Converter.convertISO2022CharsetToJavaCharset(bytes); 173 if (charsetName == null) { 173 174 // Unable to determine the charset, so fall through and treat tag as a regular string 174 string = new String(bytes); 175 break; 175 charsetName = new String(bytes); 176 176 } 177 directory.setString(tagIdentifier, charset); 177 directory.setString(tagIdentifier, charsetName); 178 178 return; 179 179 case IptcDirectory.TAG_ENVELOPE_RECORD_VERSION: … … 201 201 // If we haven't returned yet, treat it as a string 202 202 // NOTE that there's a chance we've already loaded the value as a string above, but failed to parse the value 203 if (string == null) { 204 String encoding = directory.getString(IptcDirectory.TAG_CODED_CHARACTER_SET); 205 if (encoding != null) { 206 string = reader.getString(tagByteCount, encoding); 207 } else { 208 byte[] bytes = reader.getBytes(tagByteCount); 209 encoding = Iso2022Converter.guessEncoding(bytes); 210 string = encoding != null ? new String(bytes, encoding) : new String(bytes); 211 } 203 String charSetName = directory.getString(IptcDirectory.TAG_CODED_CHARACTER_SET); 204 Charset charset = null; 205 try { 206 if (charSetName != null) 207 charset = Charset.forName(charSetName); 208 } catch (Throwable ignored) { 209 } 210 211 StringValue string; 212 if (charSetName != null) { 213 string = reader.getStringValue(tagByteCount, charset); 214 } else { 215 byte[] bytes = reader.getBytes(tagByteCount); 216 Charset charSet = Iso2022Converter.guessCharSet(bytes); 217 string = charSet != null ? new StringValue(bytes, charSet) : new StringValue(bytes, null); 212 218 } 213 219 214 220 if (directory.containsTag(tagIdentifier)) { 215 // this fancy string[] business avoids using an ArrayList for performance reasons216 String[] oldStrings = directory.getStringArray(tagIdentifier); 217 String[] newStrings; 221 // this fancy StringValue[] business avoids using an ArrayList for performance reasons 222 StringValue[] oldStrings = directory.getStringValueArray(tagIdentifier); 223 StringValue[] newStrings; 218 224 if (oldStrings == null) { 219 225 // TODO hitting this block means any prior value(s) are discarded 220 newStrings = new String[1]; 226 newStrings = new StringValue[1]; 221 227 } else { 222 newStrings = new String[oldStrings.length + 1]; 228 newStrings = new StringValue[oldStrings.length + 1]; 223 229 System.arraycopy(oldStrings, 0, newStrings, 0, oldStrings.length); 224 230 } 225 231 newStrings[newStrings.length - 1] = string; 226 directory.setStringArray(tagIdentifier, newStrings); 232 directory.setStringValueArray(tagIdentifier, newStrings); 227 233 } else { 228 directory.setString(tagIdentifier, string); 234 directory.setStringValue(tagIdentifier, string); 229 235 } 230 236 }
Note:
See TracChangeset
for help on using the changeset viewer.
