All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.drew.metadata.apple.AppleRunTimeReader Maven / Gradle / Ivy

Go to download

Java library for extracting EXIF, IPTC, XMP, ICC and other metadata from image and video files.

There is a newer version: 2.19.0
Show newest version
package com.drew.metadata.apple;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.drew.lang.annotations.NotNull;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.makernotes.AppleMakernoteDirectory;
import com.drew.metadata.exif.makernotes.AppleRunTimeMakernoteDirectory;
import com.drew.metadata.plist.BplistReader;

/**
 * Reads the AppleRunTime data and adds {@link AppleRunTimeMakernoteDirectory} to the
 * parent {@link AppleMakernoteDirectory} if it can be parsed with no errors.
 */
public class AppleRunTimeReader
{
    public void extract(@NotNull byte[] bytes, @NotNull final Metadata metadata, @NotNull final Directory parentDirectory)
    {
        parentDirectory.setByteArray(AppleMakernoteDirectory.TAG_RUN_TIME, bytes);

        if (!BplistReader.isValid(bytes)) {
        	parentDirectory.addError("Input array is not a bplist");
        	return;
        }

        AppleRunTimeMakernoteDirectory directory = new AppleRunTimeMakernoteDirectory();
        directory.setParent(parentDirectory);

        try {
            processAppleRunTime(directory, bytes);

            if (directory.getTagCount() > 0) {
                metadata.addDirectory(directory);
            }
        } catch (IOException e) {
            parentDirectory.addError("Error processing TAG_RUN_TIME: " + e.getMessage());
        }
    }

    /**
     * Process the BPLIST containing the RUN_TIME tag. The directory will only be populated with values
     * if the flag indicates that the CMTime structure is "valid".
     *
     * @param directory The AppleRunTimeMakernoteDirectory to set values onto.
     * @param bplist The BPLIST
     * @throws IOException Thrown if an error occurs parsing the BPLIST as a CMTime structure.
     */
    private static void processAppleRunTime(@NotNull final AppleRunTimeMakernoteDirectory directory, @NotNull final byte[] bplist) throws IOException
    {
        final BplistReader.PropertyListResults results = BplistReader.parse(bplist);

        final Set> entrySet = results.getEntrySet();

        if (entrySet != null) {
            HashMap values = new HashMap(entrySet.size());

            for (Map.Entry entry : entrySet) {
                String key = (String)results.getObjects().get(entry.getKey());
                Object value = results.getObjects().get(entry.getValue());

                values.put(key, value);
            }

            // https://developer.apple.com/documentation/coremedia/cmtime-u58

            byte flags = (Byte)values.get("flags");

            if ((flags & 0x1) == 0x1) {
                directory.setInt(AppleRunTimeMakernoteDirectory.CMTimeFlags, flags);
                directory.setInt(AppleRunTimeMakernoteDirectory.CMTimeEpoch, (Byte)values.get("epoch"));
                directory.setLong(AppleRunTimeMakernoteDirectory.CMTimeScale, (Long)values.get("timescale"));
                directory.setLong(AppleRunTimeMakernoteDirectory.CMTimeValue, (Long)values.get("value"));
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy