source: josm/trunk/src/com/drew/imaging/jpeg/JpegSegmentData.java@ 10862

Last change on this file since 10862 was 10862, checked in by Don-vip, 8 years ago

update to metadata-extractor 2.9.1

File size: 9.3 KB
Line 
1/*
2 * Copyright 2002-2016 Drew Noakes
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 */
21package com.drew.imaging.jpeg;
22
23import com.drew.lang.annotations.NotNull;
24import com.drew.lang.annotations.Nullable;
25
26import java.util.*;
27
28/**
29 * Holds a collection of JPEG data segments. This need not necessarily be all segments
30 * within the JPEG. For example, it may be convenient to store only the non-image
31 * segments when analysing metadata.
32 * <p>
33 * Segments are keyed via their {@link JpegSegmentType}. Where multiple segments use the
34 * same segment type, they will all be stored and available.
35 * <p>
36 * Each segment type may contain multiple entries. Conceptually the model is:
37 * <code>Map&lt;JpegSegmentType, Collection&lt;byte[]&gt;&gt;</code>. This class provides
38 * convenience methods around that structure.
39 *
40 * @author Drew Noakes https://drewnoakes.com
41 */
42public class JpegSegmentData
43{
44 // TODO key this on JpegSegmentType rather than Byte, and hopefully lose much of the use of 'byte' with this class
45 @NotNull
46 private final HashMap<Byte, List<byte[]>> _segmentDataMap = new HashMap<Byte, List<byte[]>>(10);
47
48 /**
49 * Adds segment bytes to the collection.
50 *
51 * @param segmentType the type of the segment being added
52 * @param segmentBytes the byte array holding data for the segment being added
53 */
54 public void addSegment(byte segmentType, @NotNull byte[] segmentBytes)
55 {
56 getOrCreateSegmentList(segmentType).add(segmentBytes);
57 }
58
59 /**
60 * Gets the set of JPEG segment type identifiers.
61 */
62 public Iterable<JpegSegmentType> getSegmentTypes()
63 {
64 Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();
65
66 for (Byte segmentTypeByte : _segmentDataMap.keySet())
67 {
68 JpegSegmentType segmentType = JpegSegmentType.fromByte(segmentTypeByte);
69 if (segmentType == null) {
70 throw new IllegalStateException("Should not have a segmentTypeByte that is not in the enum: " + Integer.toHexString(segmentTypeByte));
71 }
72 segmentTypes.add(segmentType);
73 }
74
75 return segmentTypes;
76 }
77
78 /**
79 * Gets the first JPEG segment data for the specified type.
80 *
81 * @param segmentType the JpegSegmentType for the desired segment
82 * @return a byte[] containing segment data or null if no data exists for that segment
83 */
84 @Nullable
85 public byte[] getSegment(byte segmentType)
86 {
87 return getSegment(segmentType, 0);
88 }
89
90 /**
91 * Gets the first JPEG segment data for the specified type.
92 *
93 * @param segmentType the JpegSegmentType for the desired segment
94 * @return a byte[] containing segment data or null if no data exists for that segment
95 */
96 @Nullable
97 public byte[] getSegment(@NotNull JpegSegmentType segmentType)
98 {
99 return getSegment(segmentType.byteValue, 0);
100 }
101
102 /**
103 * Gets segment data for a specific occurrence and type. Use this method when more than one occurrence
104 * of segment data for a given type exists.
105 *
106 * @param segmentType identifies the required segment
107 * @param occurrence the zero-based index of the occurrence
108 * @return the segment data as a byte[], or null if no segment exists for the type &amp; occurrence
109 */
110 @Nullable
111 public byte[] getSegment(@NotNull JpegSegmentType segmentType, int occurrence)
112 {
113 return getSegment(segmentType.byteValue, occurrence);
114 }
115
116 /**
117 * Gets segment data for a specific occurrence and type. Use this method when more than one occurrence
118 * of segment data for a given type exists.
119 *
120 * @param segmentType identifies the required segment
121 * @param occurrence the zero-based index of the occurrence
122 * @return the segment data as a byte[], or null if no segment exists for the type &amp; occurrence
123 */
124 @Nullable
125 public byte[] getSegment(byte segmentType, int occurrence)
126 {
127 final List<byte[]> segmentList = getSegmentList(segmentType);
128
129 return segmentList != null && segmentList.size() > occurrence
130 ? segmentList.get(occurrence)
131 : null;
132 }
133
134 /**
135 * Returns all instances of a given JPEG segment. If no instances exist, an empty sequence is returned.
136 *
137 * @param segmentType a number which identifies the type of JPEG segment being queried
138 * @return zero or more byte arrays, each holding the data of a JPEG segment
139 */
140 @NotNull
141 public Iterable<byte[]> getSegments(@NotNull JpegSegmentType segmentType)
142 {
143 return getSegments(segmentType.byteValue);
144 }
145
146 /**
147 * Returns all instances of a given JPEG segment. If no instances exist, an empty sequence is returned.
148 *
149 * @param segmentType a number which identifies the type of JPEG segment being queried
150 * @return zero or more byte arrays, each holding the data of a JPEG segment
151 */
152 @NotNull
153 public Iterable<byte[]> getSegments(byte segmentType)
154 {
155 final List<byte[]> segmentList = getSegmentList(segmentType);
156 return segmentList == null ? new ArrayList<byte[]>() : segmentList;
157 }
158
159 @Nullable
160 private List<byte[]> getSegmentList(byte segmentType)
161 {
162 return _segmentDataMap.get(segmentType);
163 }
164
165 @NotNull
166 private List<byte[]> getOrCreateSegmentList(byte segmentType)
167 {
168 List<byte[]> segmentList;
169 if (_segmentDataMap.containsKey(segmentType)) {
170 segmentList = _segmentDataMap.get(segmentType);
171 } else {
172 segmentList = new ArrayList<byte[]>();
173 _segmentDataMap.put(segmentType, segmentList);
174 }
175 return segmentList;
176 }
177
178 /**
179 * Returns the count of segment data byte arrays stored for a given segment type.
180 *
181 * @param segmentType identifies the required segment
182 * @return the segment count (zero if no segments exist).
183 */
184 public int getSegmentCount(@NotNull JpegSegmentType segmentType)
185 {
186 return getSegmentCount(segmentType.byteValue);
187 }
188
189 /**
190 * Returns the count of segment data byte arrays stored for a given segment type.
191 *
192 * @param segmentType identifies the required segment
193 * @return the segment count (zero if no segments exist).
194 */
195 public int getSegmentCount(byte segmentType)
196 {
197 final List<byte[]> segmentList = getSegmentList(segmentType);
198 return segmentList == null ? 0 : segmentList.size();
199 }
200
201 /**
202 * Removes a specified instance of a segment's data from the collection. Use this method when more than one
203 * occurrence of segment data exists for a given type exists.
204 *
205 * @param segmentType identifies the required segment
206 * @param occurrence the zero-based index of the segment occurrence to remove.
207 */
208 public void removeSegmentOccurrence(@NotNull JpegSegmentType segmentType, int occurrence)
209 {
210 removeSegmentOccurrence(segmentType.byteValue, occurrence);
211 }
212
213 /**
214 * Removes a specified instance of a segment's data from the collection. Use this method when more than one
215 * occurrence of segment data exists for a given type exists.
216 *
217 * @param segmentType identifies the required segment
218 * @param occurrence the zero-based index of the segment occurrence to remove.
219 */
220 public void removeSegmentOccurrence(byte segmentType, int occurrence)
221 {
222 final List<byte[]> segmentList = _segmentDataMap.get(segmentType);
223 segmentList.remove(occurrence);
224 }
225
226 /**
227 * Removes all segments from the collection having the specified type.
228 *
229 * @param segmentType identifies the required segment
230 */
231 public void removeSegment(@NotNull JpegSegmentType segmentType)
232 {
233 removeSegment(segmentType.byteValue);
234 }
235
236 /**
237 * Removes all segments from the collection having the specified type.
238 *
239 * @param segmentType identifies the required segment
240 */
241 public void removeSegment(byte segmentType)
242 {
243 _segmentDataMap.remove(segmentType);
244 }
245
246 /**
247 * Determines whether data is present for a given segment type.
248 *
249 * @param segmentType identifies the required segment
250 * @return true if data exists, otherwise false
251 */
252 public boolean containsSegment(@NotNull JpegSegmentType segmentType)
253 {
254 return containsSegment(segmentType.byteValue);
255 }
256
257 /**
258 * Determines whether data is present for a given segment type.
259 *
260 * @param segmentType identifies the required segment
261 * @return true if data exists, otherwise false
262 */
263 public boolean containsSegment(byte segmentType)
264 {
265 return _segmentDataMap.containsKey(segmentType);
266 }
267}
Note: See TracBrowser for help on using the repository browser.