Debian Codegraph @

src/com/drew/metadata/photoshop/DuckyReader.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
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 * Reads Photoshop "ducky" segments, created during Save-for-Web.
36 *
37 * @author Drew Noakes https://drewnoakes.com
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 // Ensure data starts with the necessary preamble
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 // End of Segment is marked with zero
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 // Unexpected tag
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 }