Changeset 13061 in josm for trunk/src/com/drew/metadata/iptc


Ignore:
Timestamp:
2017-10-30T22:46:09+01:00 (8 years ago)
Author:
Don-vip
Message:

fix #15505 - update to metadata-extractor 2.10.1

Location:
trunk/src/com/drew/metadata/iptc
Files:
1 added
1 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/com/drew/metadata/iptc/IptcDescriptor.java

    r10862 r13061  
    11/*
    2  * Copyright 2002-2016 Drew Noakes
     2 * Copyright 2002-2017 Drew Noakes
    33 *
    44 *    Licensed under the Apache License, Version 2.0 (the "License");
     
    3535 * @author Drew Noakes https://drewnoakes.com
    3636 */
     37@SuppressWarnings("WeakerAccess")
    3738public class IptcDescriptor extends TagDescriptor<IptcDirectory>
    3839{
  • trunk/src/com/drew/metadata/iptc/IptcDirectory.java

    r10862 r13061  
    11/*
    2  * Copyright 2002-2016 Drew Noakes
     2 * Copyright 2002-2017 Drew Noakes
    33 *
    44 *    Licensed under the Apache License, Version 2.0 (the "License");
     
    3838 * @author Drew Noakes https://drewnoakes.com
    3939 */
     40@SuppressWarnings("WeakerAccess")
    4041public class IptcDirectory extends Directory
    4142{
  • trunk/src/com/drew/metadata/iptc/IptcReader.java

    r10862 r13061  
    11/*
    2  * Copyright 2002-2016 Drew Noakes
     2 * Copyright 2002-2017 Drew Noakes
    33 *
    44 *    Licensed under the Apache License, Version 2.0 (the "License");
     
    2929import com.drew.metadata.Directory;
    3030import com.drew.metadata.Metadata;
     31import com.drew.metadata.StringValue;
    3132
    3233import java.io.IOException;
     34import java.nio.charset.Charset;
    3335import java.util.Collections;
    3436
     
    5658    public static final int POST_DATA_RECORD = 9;
    5759*/
     60    private static final byte IptcMarkerByte = 0x1c;
    5861
    5962    @NotNull
     
    6770        for (byte[] segmentBytes : segments) {
    6871            // Ensure data starts with the IPTC marker byte
    69             if (segmentBytes.length != 0 && segmentBytes[0] == 0x1c) {
     72            if (segmentBytes.length != 0 && segmentBytes[0] == IptcMarkerByte) {
    7073                extract(new SequentialByteArrayReader(segmentBytes), metadata, segmentBytes.length);
    7174            }
     
    107110            }
    108111
    109             if (startByte != 0x1c) {
     112            if (startByte != IptcMarkerByte) {
    110113                // NOTE have seen images where there was one extra byte at the end, giving
    111114                // offset==length at this point, which is not worth logging as an error.
    112115                if (offset != length)
    113                     directory.addError("Invalid IPTC tag marker at offset " + (offset - 1) + ". Expected '0x1c' 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) + "'.");
    114117                return;
    115118            }
     
    164167        }
    165168
    166         String string = null;
    167 
    168169        switch (tagIdentifier) {
    169170            case IptcDirectory.TAG_CODED_CHARACTER_SET:
    170171                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) {
    173174                    // 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);
    176176                }
    177                 directory.setString(tagIdentifier, charset);
     177                directory.setString(tagIdentifier, charsetName);
    178178                return;
    179179            case IptcDirectory.TAG_ENVELOPE_RECORD_VERSION:
     
    201201        // If we haven't returned yet, treat it as a string
    202202        // 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);
    212218        }
    213219
    214220        if (directory.containsTag(tagIdentifier)) {
    215             // this fancy string[] business avoids using an ArrayList for performance reasons
    216             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;
    218224            if (oldStrings == null) {
    219225                // TODO hitting this block means any prior value(s) are discarded
    220                 newStrings = new String[1];
     226                newStrings = new StringValue[1];
    221227            } else {
    222                 newStrings = new String[oldStrings.length + 1];
     228                newStrings = new StringValue[oldStrings.length + 1];
    223229                System.arraycopy(oldStrings, 0, newStrings, 0, oldStrings.length);
    224230            }
    225231            newStrings[newStrings.length - 1] = string;
    226             directory.setStringArray(tagIdentifier, newStrings);
     232            directory.setStringValueArray(tagIdentifier, newStrings);
    227233        } else {
    228             directory.setString(tagIdentifier, string);
     234            directory.setStringValue(tagIdentifier, string);
    229235        }
    230236    }
  • trunk/src/com/drew/metadata/iptc/Iso2022Converter.java

    r10862 r13061  
    11/*
    2  * Copyright 2002-2016 Drew Noakes
     2 * Copyright 2002-2017 Drew Noakes
    33 *
    44 *    Licensed under the Apache License, Version 2.0 (the "License");
     
    5959
    6060    /**
    61      * Attempts to guess the encoding of a string provided as a byte array.
     61     * Attempts to guess the {@link Charset} of a string provided as a byte array.
    6262     * <p>
    63      * Encodings trialled are, in order:
     63     * Charsets trialled are, in order:
    6464     * <ul>
    6565     *     <li>UTF-8</li>
     
    6868     * </ul>
    6969     * <p>
    70      * Its only purpose is to guess the encoding if and only if iptc tag coded character set is not set. If the
     70     * Its only purpose is to guess the Charset if and only if IPTC tag coded character set is not set. If the
    7171     * encoding is not UTF-8, the tag should be set. Otherwise it is bad practice. This method tries to
    7272     * workaround this issue since some metadata manipulating tools do not prevent such bad practice.
     
    7979     */
    8080    @Nullable
    81     static String guessEncoding(@NotNull final byte[] bytes)
     81    static Charset guessCharSet(@NotNull final byte[] bytes)
    8282    {
    8383        String[] encodings = { UTF_8, System.getProperty("file.encoding"), ISO_8859_1 };
     
    8585        for (String encoding : encodings)
    8686        {
    87             CharsetDecoder cs = Charset.forName(encoding).newDecoder();
     87            Charset charset = Charset.forName(encoding);
     88            CharsetDecoder cs = charset.newDecoder();
    8889
    8990            try {
    9091                cs.decode(ByteBuffer.wrap(bytes));
    91                 return encoding;
     92                return charset;
    9293            } catch (CharacterCodingException e) {
    9394                // fall through...
Note: See TracChangeset for help on using the changeset viewer.