| 1 |
package com.drew.metadata.apple; |
| 2 |
|
| 3 |
import java.io.IOException; |
| 4 |
import java.util.HashMap; |
| 5 |
import java.util.Map; |
| 6 |
import java.util.Set; |
| 7 |
|
| 8 |
import com.drew.lang.annotations.NotNull; |
| 9 |
import com.drew.metadata.Directory; |
| 10 |
import com.drew.metadata.Metadata; |
| 11 |
import com.drew.metadata.exif.makernotes.AppleMakernoteDirectory; |
| 12 |
import com.drew.metadata.exif.makernotes.AppleRunTimeMakernoteDirectory; |
| 13 |
import com.drew.metadata.plist.BplistReader; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class AppleRunTimeReader |
| 20 |
{ |
| 21 |
public void extract(@NotNull byte[] bytes, @NotNull final Metadata metadata, @NotNull final Directory parentDirectory) |
| 22 |
{ |
| 23 |
if (!BplistReader.isValid(bytes)) { |
| 24 |
parentDirectory.addError("Input array is not a bplist"); |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
AppleRunTimeMakernoteDirectory directory = new AppleRunTimeMakernoteDirectory(); |
| 29 |
directory.setParent(parentDirectory); |
| 30 |
|
| 31 |
try { |
| 32 |
processAppleRunTime(directory, bytes); |
| 33 |
|
| 34 |
if (directory.getTagCount() > 0) { |
| 35 |
metadata.addDirectory(directory); |
| 36 |
} |
| 37 |
} catch (IOException e) { |
| 38 |
parentDirectory.addError("Error processing TAG_RUN_TIME: " + e.getMessage()); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private static void processAppleRunTime(@NotNull final AppleRunTimeMakernoteDirectory directory, @NotNull final byte[] bplist) throws IOException |
| 51 |
{ |
| 52 |
final BplistReader.PropertyListResults results = BplistReader.parse(bplist); |
| 53 |
|
| 54 |
final Set<Map.Entry<Byte, Byte>> entrySet = results.getEntrySet(); |
| 55 |
|
| 56 |
if (entrySet != null) { |
| 57 |
HashMap<String, Object> values = new HashMap<String, Object>(entrySet.size()); |
| 58 |
|
| 59 |
for (Map.Entry<Byte, Byte> entry : entrySet) { |
| 60 |
String key = (String)results.getObjects().get(entry.getKey()); |
| 61 |
Object value = results.getObjects().get(entry.getValue()); |
| 62 |
|
| 63 |
values.put(key, value); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
Object flagsObject = values.get("flags"); |
| 69 |
if (flagsObject instanceof Byte) { |
| 70 |
byte flags = (Byte) flagsObject; |
| 71 |
if ((flags & 0x1) == 0x1) { |
| 72 |
directory.setInt(AppleRunTimeMakernoteDirectory.CMTimeFlags, flags); |
| 73 |
directory.setInt(AppleRunTimeMakernoteDirectory.CMTimeEpoch, (Byte) values.get("epoch")); |
| 74 |
directory.setLong(AppleRunTimeMakernoteDirectory.CMTimeScale, (Long) values.get("timescale")); |
| 75 |
directory.setLong(AppleRunTimeMakernoteDirectory.CMTimeValue, (Long) values.get("value")); |
| 76 |
} |
| 77 |
} else if (flagsObject instanceof String) { |
| 78 |
byte flags = Byte.parseByte((String) flagsObject); |
| 79 |
if ((flags & 0x1) == 0x1) { |
| 80 |
directory.setInt(AppleRunTimeMakernoteDirectory.CMTimeFlags, flags); |
| 81 |
directory.setInt(AppleRunTimeMakernoteDirectory.CMTimeEpoch, Byte.parseByte((String) values.get("epoch"))); |
| 82 |
directory.setLong(AppleRunTimeMakernoteDirectory.CMTimeScale, Long.parseLong((String) values.get("timescale"))); |
| 83 |
directory.setLong(AppleRunTimeMakernoteDirectory.CMTimeValue, Long.parseLong((String) values.get("value"))); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
} |