| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.photoshop; |
| 22 |
|
| 23 |
import com.drew.imaging.jpeg.JpegSegmentMetadataReader; |
| 24 |
import com.drew.imaging.jpeg.JpegSegmentType; |
| 25 |
import com.drew.lang.Charsets; |
| 26 |
import com.drew.lang.SequentialByteArrayReader; |
| 27 |
import com.drew.lang.SequentialReader; |
| 28 |
import com.drew.lang.annotations.NotNull; |
| 29 |
import com.drew.metadata.Metadata; |
| 30 |
|
| 31 |
import java.io.IOException; |
| 32 |
import java.util.Collections; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
@SuppressWarnings("WeakerAccess") |
| 40 |
public class DuckyReader implements JpegSegmentMetadataReader |
| 41 |
{ |
| 42 |
@NotNull |
| 43 |
private static final String JPEG_SEGMENT_PREAMBLE = "Ducky"; |
| 44 |
|
| 45 |
@NotNull |
| 46 |
public Iterable<JpegSegmentType> getSegmentTypes() |
| 47 |
{ |
| 48 |
return Collections.singletonList(JpegSegmentType.APPC); |
| 49 |
} |
| 50 |
|
| 51 |
public void readJpegSegments(@NotNull Iterable<byte[]> segments, @NotNull Metadata metadata, @NotNull JpegSegmentType segmentType) |
| 52 |
{ |
| 53 |
final int preambleLength = JPEG_SEGMENT_PREAMBLE.length(); |
| 54 |
|
| 55 |
for (byte[] segmentBytes : segments) { |
| 56 |
|
| 57 |
if (segmentBytes.length < preambleLength || !JPEG_SEGMENT_PREAMBLE.equals(new String(segmentBytes, 0, preambleLength))) |
| 58 |
continue; |
| 59 |
|
| 60 |
extract( |
| 61 |
new SequentialByteArrayReader(segmentBytes, preambleLength), |
| 62 |
metadata); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
public void extract(@NotNull final SequentialReader reader, @NotNull final Metadata metadata) |
| 67 |
{ |
| 68 |
DuckyDirectory directory = new DuckyDirectory(); |
| 69 |
metadata.addDirectory(directory); |
| 70 |
|
| 71 |
try |
| 72 |
{ |
| 73 |
while (true) |
| 74 |
{ |
| 75 |
int tag = reader.getUInt16(); |
| 76 |
|
| 77 |
|
| 78 |
if (tag == 0) |
| 79 |
break; |
| 80 |
|
| 81 |
int length = reader.getUInt16(); |
| 82 |
|
| 83 |
switch (tag) |
| 84 |
{ |
| 85 |
case DuckyDirectory.TAG_QUALITY: |
| 86 |
{ |
| 87 |
if (length != 4) |
| 88 |
{ |
| 89 |
directory.addError("Unexpected length for the quality tag"); |
| 90 |
return; |
| 91 |
} |
| 92 |
directory.setInt(tag, reader.getInt32()); |
| 93 |
break; |
| 94 |
} |
| 95 |
case DuckyDirectory.TAG_COMMENT: |
| 96 |
case DuckyDirectory.TAG_COPYRIGHT: |
| 97 |
{ |
| 98 |
reader.skip(4); |
| 99 |
directory.setStringValue(tag, reader.getStringValue(length - 4, Charsets.UTF_16BE)); |
| 100 |
break; |
| 101 |
} |
| 102 |
default: |
| 103 |
{ |
| 104 |
|
| 105 |
directory.setByteArray(tag, reader.getBytes(length)); |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
catch (IOException e) |
| 112 |
{ |
| 113 |
directory.addError(e.getMessage()); |
| 114 |
} |
| 115 |
} |
| 116 |
} |