Skip to content

Commit 499ea8a

Browse files
committed
Pass Set for processed IFD offsets
This allows combining the add and test operations into a single lookup.
1 parent dec9b6b commit 499ea8a

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Source/com/drew/imaging/tiff/TiffReader.java

+8-9
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ public void processTiff(@NotNull final RandomAccessReader reader,
128128
*
129129
* @param handler the {@link com.drew.imaging.tiff.TiffHandler} that will coordinate processing and accept read values
130130
* @param reader the {@link com.drew.lang.RandomAccessReader} from which the data should be read
131-
* @param processedGlobalIfdOffsets the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop
131+
* @param processedIfdOffsets the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop
132132
* @param ifdOffset the offset within <code>reader</code> at which the IFD data starts
133133
* @param isBigTiff Whether the IFD uses the BigTIFF data format.
134134
* @throws IOException an error occurred while accessing the required data
135135
*/
136136
public static void processIfd(@NotNull final TiffHandler handler,
137137
@NotNull final RandomAccessReader reader,
138-
@NotNull final Set<Integer> processedGlobalIfdOffsets,
138+
@NotNull final Set<Integer> processedIfdOffsets,
139139
final int ifdOffset,
140140
final boolean isBigTiff) throws IOException
141141
{
@@ -166,12 +166,11 @@ public static void processIfd(@NotNull final TiffHandler handler,
166166
// Check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist.
167167
// Note that we track these offsets in the global frame, not the reader's local frame.
168168
int globalIfdOffset = reader.toUnshiftedOffset(ifdOffset);
169-
if (processedGlobalIfdOffsets.contains(Integer.valueOf(globalIfdOffset))) {
170-
return;
171-
}
172169

173170
// remember that we've visited this directory so that we don't visit it again later
174-
processedGlobalIfdOffsets.add(globalIfdOffset);
171+
if (!processedIfdOffsets.add(globalIfdOffset)) {
172+
return;
173+
}
175174

176175
// Validate IFD offset
177176
if (ifdOffset >= reader.getLength() || ifdOffset < 0) {
@@ -276,13 +275,13 @@ public static void processIfd(@NotNull final TiffHandler handler,
276275
if (handler.tryEnterSubIfd(tagId)) {
277276
isIfdPointer = true;
278277
long subDirOffset = reader.getUInt32((int) (tagValueOffset + i*4));
279-
processIfd(handler, reader, processedGlobalIfdOffsets, (int) subDirOffset, isBigTiff);
278+
processIfd(handler, reader, processedIfdOffsets, (int) subDirOffset, isBigTiff);
280279
}
281280
}
282281
}
283282

284283
// If it wasn't an IFD pointer, allow custom tag processing to occur
285-
if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedGlobalIfdOffsets, reader, tagId, (int) byteCount, isBigTiff)) {
284+
if (!isIfdPointer && !handler.customProcessTag((int) tagValueOffset, processedIfdOffsets, reader, tagId, (int) byteCount, isBigTiff)) {
286285
// If no custom processing occurred, process the tag in the standard fashion
287286
processTag(handler, tagId, (int) tagValueOffset, (int) componentCount, formatCode, reader);
288287
}
@@ -308,7 +307,7 @@ public static void processIfd(@NotNull final TiffHandler handler,
308307
}
309308

310309
if (handler.hasFollowerIfd()) {
311-
processIfd(handler, reader, processedGlobalIfdOffsets, nextIfdOffset, isBigTiff);
310+
processIfd(handler, reader, processedIfdOffsets, nextIfdOffset, isBigTiff);
312311
}
313312
}
314313
} finally {

0 commit comments

Comments
 (0)