Changeset 13061 in josm for trunk/src/com/drew/metadata/iptc
- Timestamp:
- 2017-10-30T22:46:09+01:00 (8 years ago)
- Location:
- trunk/src/com/drew/metadata/iptc
- Files:
-
- 1 added
- 1 deleted
- 4 edited
-
IptcDescriptor.java (modified) (2 diffs)
-
IptcDirectory.java (modified) (2 diffs)
-
IptcReader.java (modified) (7 diffs)
-
Iso2022Converter.java (modified) (5 diffs)
-
package-info.java (added)
-
package.html (deleted)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/drew/metadata/iptc/IptcDescriptor.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"); … … 35 35 * @author Drew Noakes https://drewnoakes.com 36 36 */ 37 @SuppressWarnings("WeakerAccess") 37 38 public class IptcDescriptor extends TagDescriptor<IptcDirectory> 38 39 { -
trunk/src/com/drew/metadata/iptc/IptcDirectory.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"); … … 38 38 * @author Drew Noakes https://drewnoakes.com 39 39 */ 40 @SuppressWarnings("WeakerAccess") 40 41 public class IptcDirectory extends Directory 41 42 { -
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 } -
trunk/src/com/drew/metadata/iptc/Iso2022Converter.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"); … … 59 59 60 60 /** 61 * Attempts to guess the encodingof a string provided as a byte array.61 * Attempts to guess the {@link Charset} of a string provided as a byte array. 62 62 * <p> 63 * Encodings trialled are, in order:63 * Charsets trialled are, in order: 64 64 * <ul> 65 65 * <li>UTF-8</li> … … 68 68 * </ul> 69 69 * <p> 70 * Its only purpose is to guess the encodingif and only ifiptctag coded character set is not set. If the70 * Its only purpose is to guess the Charset if and only if IPTC tag coded character set is not set. If the 71 71 * encoding is not UTF-8, the tag should be set. Otherwise it is bad practice. This method tries to 72 72 * workaround this issue since some metadata manipulating tools do not prevent such bad practice. … … 79 79 */ 80 80 @Nullable 81 static String guessEncoding(@NotNull final byte[] bytes)81 static Charset guessCharSet(@NotNull final byte[] bytes) 82 82 { 83 83 String[] encodings = { UTF_8, System.getProperty("file.encoding"), ISO_8859_1 }; … … 85 85 for (String encoding : encodings) 86 86 { 87 CharsetDecoder cs = Charset.forName(encoding).newDecoder(); 87 Charset charset = Charset.forName(encoding); 88 CharsetDecoder cs = charset.newDecoder(); 88 89 89 90 try { 90 91 cs.decode(ByteBuffer.wrap(bytes)); 91 return encoding;92 return charset; 92 93 } catch (CharacterCodingException e) { 93 94 // fall through...
Note:
See TracChangeset
for help on using the changeset viewer.
