Index: trunk/src/com/drew/imaging/ImageProcessingException.java
===================================================================
--- trunk/src/com/drew/imaging/ImageProcessingException.java	(revision 6127)
+++ trunk/src/com/drew/imaging/ImageProcessingException.java	(revision 8132)
@@ -1,4 +1,4 @@
 /*
- * Copyright 2002-2012 Drew Noakes
+ * Copyright 2002-2015 Drew Noakes
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,6 @@
  * More information about this project is available at:
  *
- *    http://drewnoakes.com/code/exif/
- *    http://code.google.com/p/metadata-extractor/
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
  */
 package com.drew.imaging;
@@ -26,6 +26,6 @@
 /**
  * An exception class thrown upon an unexpected condition that was fatal for the processing of an image.
- * 
- * @author Drew Noakes http://drewnoakes.com
+ *
+ * @author Drew Noakes https://drewnoakes.com
  */
 public class ImageProcessingException extends CompoundException
Index: trunk/src/com/drew/imaging/PhotographicConversions.java
===================================================================
--- trunk/src/com/drew/imaging/PhotographicConversions.java	(revision 6127)
+++ trunk/src/com/drew/imaging/PhotographicConversions.java	(revision 8132)
@@ -1,4 +1,4 @@
 /*
- * Copyright 2002-2012 Drew Noakes
+ * Copyright 2002-2015 Drew Noakes
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,6 @@
  * More information about this project is available at:
  *
- *    http://drewnoakes.com/code/exif/
- *    http://code.google.com/p/metadata-extractor/
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
  */
 package com.drew.imaging;
@@ -24,5 +24,5 @@
  * Contains helper methods that perform photographic conversions.
  *
- * @author Drew Noakes http://drewnoakes.com
+ * @author Drew Noakes https://drewnoakes.com
  */
 public final class PhotographicConversions
Index: trunk/src/com/drew/imaging/jpeg/JpegMetadataReader.java
===================================================================
--- trunk/src/com/drew/imaging/jpeg/JpegMetadataReader.java	(revision 6127)
+++ trunk/src/com/drew/imaging/jpeg/JpegMetadataReader.java	(revision 8132)
@@ -1,4 +1,4 @@
 /*
- * Copyright 2002-2012 Drew Noakes
+ * Copyright 2002-2015 Drew Noakes
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,46 +16,76 @@
  * More information about this project is available at:
  *
- *    http://drewnoakes.com/code/exif/
- *    http://code.google.com/p/metadata-extractor/
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
  */
 package com.drew.imaging.jpeg;
 
-import com.drew.lang.ByteArrayReader;
-import com.drew.lang.annotations.NotNull;
-import com.drew.metadata.Metadata;
-import com.drew.metadata.exif.ExifReader;
-import com.drew.metadata.iptc.IptcReader;
-import com.drew.metadata.jpeg.JpegCommentReader;
-import com.drew.metadata.jpeg.JpegDirectory;
-import com.drew.metadata.jpeg.JpegReader;
-
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import com.drew.lang.StreamReader;
+import com.drew.lang.annotations.NotNull;
+import com.drew.lang.annotations.Nullable;
+import com.drew.metadata.Metadata;
+//import com.drew.metadata.adobe.AdobeJpegReader;
+import com.drew.metadata.exif.ExifReader;
+//import com.drew.metadata.icc.IccReader;
+import com.drew.metadata.iptc.IptcReader;
+//import com.drew.metadata.jfif.JfifReader;
+import com.drew.metadata.jpeg.JpegCommentReader;
+import com.drew.metadata.jpeg.JpegReader;
+//import com.drew.metadata.photoshop.PhotoshopReader;
+//import com.drew.metadata.xmp.XmpReader;
 
 /**
- * Obtains all available metadata from Jpeg formatted files.
+ * Obtains all available metadata from JPEG formatted files.
  *
- * @author Drew Noakes http://drewnoakes.com
+ * @author Drew Noakes https://drewnoakes.com
  */
 public class JpegMetadataReader
 {
-    // TODO investigate supporting javax.imageio
-//    public static Metadata readMetadata(IIOMetadata metadata) throws JpegProcessingException {}
-//    public static Metadata readMetadata(ImageInputStream in) throws JpegProcessingException{}
-//    public static Metadata readMetadata(IIOImage image) throws JpegProcessingException{}
-//    public static Metadata readMetadata(ImageReader reader) throws JpegProcessingException{}
+    public static final Iterable<JpegSegmentMetadataReader> ALL_READERS = Arrays.asList(
+            new JpegReader(),
+            new JpegCommentReader(),
+            //new JfifReader(),
+            new ExifReader(),
+            //new XmpReader(),
+            //new IccReader(),
+            //new PhotoshopReader(),
+            new IptcReader()//,
+            //new AdobeJpegReader()
+    );
 
     @NotNull
-    public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException
+    public static Metadata readMetadata(@NotNull InputStream inputStream, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
     {
-        return readMetadata(inputStream, true);
+        Metadata metadata = new Metadata();
+        process(metadata, inputStream, readers);
+        return metadata;
     }
 
     @NotNull
-    public static Metadata readMetadata(@NotNull InputStream inputStream, final boolean waitForBytes) throws JpegProcessingException
+    public static Metadata readMetadata(@NotNull InputStream inputStream) throws JpegProcessingException, IOException
     {
-        JpegSegmentReader segmentReader = new JpegSegmentReader(inputStream, waitForBytes);
-        return extractMetadataFromJpegSegmentReader(segmentReader.getSegmentData());
+        return readMetadata(inputStream, null);
+    }
+
+    @NotNull
+    public static Metadata readMetadata(@NotNull File file, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
+    {
+        InputStream inputStream = null;
+        try
+        {
+            inputStream = new FileInputStream(file);
+            return readMetadata(inputStream, readers);
+        } finally {
+            if (inputStream != null)
+                inputStream.close();
+        }
     }
 
@@ -63,54 +93,41 @@
     public static Metadata readMetadata(@NotNull File file) throws JpegProcessingException, IOException
     {
-        JpegSegmentReader segmentReader = new JpegSegmentReader(file);
-        return extractMetadataFromJpegSegmentReader(segmentReader.getSegmentData());
+        return readMetadata(file, null);
     }
 
-    @NotNull
-    public static Metadata extractMetadataFromJpegSegmentReader(@NotNull JpegSegmentData segmentReader)
+    public static void process(@NotNull Metadata metadata, @NotNull InputStream inputStream) throws JpegProcessingException, IOException
     {
-        final Metadata metadata = new Metadata();
+        process(metadata, inputStream, null);
+    }
 
-        // Loop through looking for all SOFn segments.  When we find one, we know what type of compression
-        // was used for the JPEG, and we can process the JPEG metadata in the segment too.
-        for (byte i = 0; i < 16; i++) {
-            // There are no SOF4 or SOF12 segments, so don't bother
-            if (i == 4 || i == 12)
-                continue;
-            // Should never have more than one SOFn for a given 'n'.
-            byte[] jpegSegment = segmentReader.getSegment((byte)(JpegSegmentReader.SEGMENT_SOF0 + i));
-            if (jpegSegment == null)
-                continue;
-            JpegDirectory directory = metadata.getOrCreateDirectory(JpegDirectory.class);
-            directory.setInt(JpegDirectory.TAG_JPEG_COMPRESSION_TYPE, i);
-            new JpegReader().extract(new ByteArrayReader(jpegSegment), metadata);
-            break;
-        }
+    public static void process(@NotNull Metadata metadata, @NotNull InputStream inputStream, @Nullable Iterable<JpegSegmentMetadataReader> readers) throws JpegProcessingException, IOException
+    {
+        if (readers == null)
+            readers = ALL_READERS;
 
-        // There should never be more than one COM segment.
-        byte[] comSegment = segmentReader.getSegment(JpegSegmentReader.SEGMENT_COM);
-        if (comSegment != null)
-            new JpegCommentReader().extract(new ByteArrayReader(comSegment), metadata);
-
-        // Loop through all APP1 segments, checking the leading bytes to identify the format of each.
-        for (byte[] app1Segment : segmentReader.getSegments(JpegSegmentReader.SEGMENT_APP1)) {
-            if (app1Segment.length > 3 && "EXIF".equalsIgnoreCase(new String(app1Segment, 0, 4)))
-                new ExifReader().extract(new ByteArrayReader(app1Segment), metadata);
-
-            //if (app1Segment.length > 27 && "http://ns.adobe.com/xap/1.0/".equalsIgnoreCase(new String(app1Segment, 0, 28)))
-            //    new XmpReader().extract(new ByteArrayReader(app1Segment), metadata);
-        }
-
-        // Loop through all APPD segments, checking the leading bytes to identify the format of each.
-        for (byte[] appdSegment : segmentReader.getSegments(JpegSegmentReader.SEGMENT_APPD)) {
-            if (appdSegment.length > 12 && "Photoshop 3.0".compareTo(new String(appdSegment, 0, 13))==0) {
-                //new PhotoshopReader().extract(new ByteArrayReader(appdSegment), metadata);
-            } else {
-                // TODO might be able to check for a leading 0x1c02 for IPTC data...
-                new IptcReader().extract(new ByteArrayReader(appdSegment), metadata);
+        Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();
+        for (JpegSegmentMetadataReader reader : readers) {
+            for (JpegSegmentType type : reader.getSegmentTypes()) {
+                segmentTypes.add(type);
             }
         }
 
-        return metadata;
+        JpegSegmentData segmentData = JpegSegmentReader.readSegments(new StreamReader(inputStream), segmentTypes);
+
+        processJpegSegmentData(metadata, readers, segmentData);
+    }
+
+    public static void processJpegSegmentData(Metadata metadata, Iterable<JpegSegmentMetadataReader> readers, JpegSegmentData segmentData)
+    {
+        // Pass the appropriate byte arrays to each reader.
+        for (JpegSegmentMetadataReader reader : readers) {
+            for (JpegSegmentType segmentType : reader.getSegmentTypes()) {
+                for (byte[] segmentBytes : segmentData.getSegments(segmentType)) {
+                    if (reader.canProcess(segmentBytes, segmentType)) {
+                        reader.extract(segmentBytes, metadata, segmentType);
+                    }
+                }
+            }
+        }
     }
 
@@ -120,3 +137,2 @@
     }
 }
-
Index: trunk/src/com/drew/imaging/jpeg/JpegProcessingException.java
===================================================================
--- trunk/src/com/drew/imaging/jpeg/JpegProcessingException.java	(revision 6127)
+++ trunk/src/com/drew/imaging/jpeg/JpegProcessingException.java	(revision 8132)
@@ -1,4 +1,4 @@
 /*
- * Copyright 2002-2012 Drew Noakes
+ * Copyright 2002-2015 Drew Noakes
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,6 @@
  * More information about this project is available at:
  *
- *    http://drewnoakes.com/code/exif/
- *    http://code.google.com/p/metadata-extractor/
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
  */
 package com.drew.imaging.jpeg;
@@ -25,7 +25,7 @@
 
 /**
- * An exception class thrown upon unexpected and fatal conditions while processing a Jpeg file.
+ * An exception class thrown upon unexpected and fatal conditions while processing a JPEG file.
  *
- * @author Drew Noakes http://drewnoakes.com
+ * @author Drew Noakes https://drewnoakes.com
  */
 public class JpegProcessingException extends ImageProcessingException
Index: trunk/src/com/drew/imaging/jpeg/JpegSegmentData.java
===================================================================
--- trunk/src/com/drew/imaging/jpeg/JpegSegmentData.java	(revision 6127)
+++ trunk/src/com/drew/imaging/jpeg/JpegSegmentData.java	(revision 8132)
@@ -1,4 +1,4 @@
 /*
- * Copyright 2002-2012 Drew Noakes
+ * Copyright 2002-2015 Drew Noakes
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,6 +16,6 @@
  * More information about this project is available at:
  *
- *    http://drewnoakes.com/code/exif/
- *    http://code.google.com/p/metadata-extractor/
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
  */
 package com.drew.imaging.jpeg;
@@ -24,24 +24,23 @@
 import com.drew.lang.annotations.Nullable;
 
-import java.io.*;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
+import java.util.*;
 
 /**
- * Holds a collection of Jpeg data segments.  This need not necessarily be all segments
- * within the Jpeg.  For example, it may be convenient to store only the non-image
- * segments when analysing (or serializing) metadata.
- * <p/>
- * Segments are keyed via their segment marker (a byte).  Where multiple segments use the
- * same segment marker, they will all be stored and available.
- *
- * @author Drew Noakes http://drewnoakes.com
+ * Holds a collection of JPEG data segments.  This need not necessarily be all segments
+ * within the JPEG. For example, it may be convenient to store only the non-image
+ * segments when analysing metadata.
+ * <p>
+ * Segments are keyed via their {@link JpegSegmentType}. Where multiple segments use the
+ * same segment type, they will all be stored and available.
+ * <p>
+ * Each segment type may contain multiple entries. Conceptually the model is:
+ * <code>Map&lt;JpegSegmentType, Collection&lt;byte[]&gt;&gt;</code>. This class provides
+ * convenience methods around that structure.
+ *
+ * @author Drew Noakes https://drewnoakes.com
  */
-public class JpegSegmentData implements Serializable
+public class JpegSegmentData
 {
-    private static final long serialVersionUID = 7110175216435025451L;
-    
-    /** A map of byte[], keyed by the segment marker */
+    // TODO key this on JpegSegmentType rather than Byte, and hopefully lose much of the use of 'byte' with this class
     @NotNull
     private final HashMap<Byte, List<byte[]>> _segmentDataMap = new HashMap<Byte, List<byte[]>>(10);
@@ -49,71 +48,129 @@
     /**
      * Adds segment bytes to the collection.
-     * @param segmentMarker
-     * @param segmentBytes
-     */
-    @SuppressWarnings({ "MismatchedQueryAndUpdateOfCollection" })
-    public void addSegment(byte segmentMarker, @NotNull byte[] segmentBytes)
-    {
-        final List<byte[]> segmentList = getOrCreateSegmentList(segmentMarker);
-        segmentList.add(segmentBytes);
-    }
-
-    /**
-     * Gets the first Jpeg segment data for the specified marker.
-     * @param segmentMarker the byte identifier for the desired segment
+     *
+     * @param segmentType  the type of the segment being added
+     * @param segmentBytes the byte array holding data for the segment being added
+     */
+    @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
+    public void addSegment(byte segmentType, @NotNull byte[] segmentBytes)
+    {
+        getOrCreateSegmentList(segmentType).add(segmentBytes);
+    }
+
+    /**
+     * Gets the set of JPEG segment type identifiers.
+     */
+    public Iterable<JpegSegmentType> getSegmentTypes()
+    {
+        Set<JpegSegmentType> segmentTypes = new HashSet<JpegSegmentType>();
+
+        for (Byte segmentTypeByte : _segmentDataMap.keySet())
+        {
+            JpegSegmentType segmentType = JpegSegmentType.fromByte(segmentTypeByte);
+            if (segmentType == null) {
+                throw new IllegalStateException("Should not have a segmentTypeByte that is not in the enum: " + Integer.toHexString(segmentTypeByte));
+            }
+            segmentTypes.add(segmentType);
+        }
+
+        return segmentTypes;
+    }
+
+    /**
+     * Gets the first JPEG segment data for the specified type.
+     *
+     * @param segmentType the JpegSegmentType for the desired segment
      * @return a byte[] containing segment data or null if no data exists for that segment
      */
     @Nullable
-    public byte[] getSegment(byte segmentMarker)
-    {
-        return getSegment(segmentMarker, 0);
-    }
-
-    /**
-     * Gets segment data for a specific occurrence and marker.  Use this method when more than one occurrence
-     * of segment data for a given marker exists.
-     * @param segmentMarker identifies the required segment
-     * @param occurrence the zero-based index of the occurrence
-     * @return the segment data as a byte[], or null if no segment exists for the marker & occurrence
-     */
-    @Nullable
-    public byte[] getSegment(byte segmentMarker, int occurrence)
-    {
-        final List<byte[]> segmentList = getSegmentList(segmentMarker);
-
-        if (segmentList==null || segmentList.size()<=occurrence)
-            return null;
-        else
-            return segmentList.get(occurrence);
-    }
-
-    /**
-     * Returns all instances of a given Jpeg segment.  If no instances exist, an empty sequence is returned.
-     *
-     * @param segmentMarker a number which identifies the type of Jpeg segment being queried
-     * @return zero or more byte arrays, each holding the data of a Jpeg segment
-     */
-    @NotNull
-    public Iterable<byte[]> getSegments(byte segmentMarker)
-    {
-        final List<byte[]> segmentList = getSegmentList(segmentMarker);
-        return segmentList==null ? new ArrayList<byte[]>() : segmentList;
-    }
-
-    @Nullable
-    public List<byte[]> getSegmentList(byte segmentMarker)
-    {
-        return _segmentDataMap.get(Byte.valueOf(segmentMarker));
-    }
-
-    @NotNull
-    private List<byte[]> getOrCreateSegmentList(byte segmentMarker)
+    public byte[] getSegment(byte segmentType)
+    {
+        return getSegment(segmentType, 0);
+    }
+
+    /**
+     * Gets the first JPEG segment data for the specified type.
+     *
+     * @param segmentType the JpegSegmentType for the desired segment
+     * @return a byte[] containing segment data or null if no data exists for that segment
+     */
+    @Nullable
+    public byte[] getSegment(@NotNull JpegSegmentType segmentType)
+    {
+        return getSegment(segmentType.byteValue, 0);
+    }
+
+    /**
+     * Gets segment data for a specific occurrence and type.  Use this method when more than one occurrence
+     * of segment data for a given type exists.
+     *
+     * @param segmentType identifies the required segment
+     * @param occurrence  the zero-based index of the occurrence
+     * @return the segment data as a byte[], or null if no segment exists for the type &amp; occurrence
+     */
+    @Nullable
+    public byte[] getSegment(@NotNull JpegSegmentType segmentType, int occurrence)
+    {
+        return getSegment(segmentType.byteValue, occurrence);
+    }
+
+    /**
+     * Gets segment data for a specific occurrence and type.  Use this method when more than one occurrence
+     * of segment data for a given type exists.
+     *
+     * @param segmentType identifies the required segment
+     * @param occurrence  the zero-based index of the occurrence
+     * @return the segment data as a byte[], or null if no segment exists for the type &amp; occurrence
+     */
+    @Nullable
+    public byte[] getSegment(byte segmentType, int occurrence)
+    {
+        final List<byte[]> segmentList = getSegmentList(segmentType);
+
+        return segmentList != null && segmentList.size() > occurrence
+                ? segmentList.get(occurrence)
+                : null;
+    }
+
+    /**
+     * Returns all instances of a given JPEG segment.  If no instances exist, an empty sequence is returned.
+     *
+     * @param segmentType a number which identifies the type of JPEG segment being queried
+     * @return zero or more byte arrays, each holding the data of a JPEG segment
+     */
+    @NotNull
+    public Iterable<byte[]> getSegments(@NotNull JpegSegmentType segmentType)
+    {
+        return getSegments(segmentType.byteValue);
+    }
+
+    /**
+     * Returns all instances of a given JPEG segment.  If no instances exist, an empty sequence is returned.
+     *
+     * @param segmentType a number which identifies the type of JPEG segment being queried
+     * @return zero or more byte arrays, each holding the data of a JPEG segment
+     */
+    @NotNull
+    public Iterable<byte[]> getSegments(byte segmentType)
+    {
+        final List<byte[]> segmentList = getSegmentList(segmentType);
+        return segmentList == null ? new ArrayList<byte[]>() : segmentList;
+    }
+
+    @Nullable
+    private List<byte[]> getSegmentList(byte segmentType)
+    {
+        return _segmentDataMap.get(segmentType);
+    }
+
+    @NotNull
+    private List<byte[]> getOrCreateSegmentList(byte segmentType)
     {
         List<byte[]> segmentList;
-        if (_segmentDataMap.containsKey(segmentMarker)) {
-            segmentList = _segmentDataMap.get(segmentMarker);
+        if (_segmentDataMap.containsKey(segmentType)) {
+            segmentList = _segmentDataMap.get(segmentType);
         } else {
             segmentList = new ArrayList<byte[]>();
-            _segmentDataMap.put(segmentMarker, segmentList);
+            _segmentDataMap.put(segmentType, segmentList);
         }
         return segmentList;
@@ -121,11 +178,23 @@
 
     /**
-     * Returns the count of segment data byte arrays stored for a given segment marker.
-     * @param segmentMarker identifies the required segment
+     * Returns the count of segment data byte arrays stored for a given segment type.
+     *
+     * @param segmentType identifies the required segment
      * @return the segment count (zero if no segments exist).
      */
-    public int getSegmentCount(byte segmentMarker)
-    {
-        final List<byte[]> segmentList = getSegmentList(segmentMarker);
+    public int getSegmentCount(@NotNull JpegSegmentType segmentType)
+    {
+        return getSegmentCount(segmentType.byteValue);
+    }
+
+    /**
+     * Returns the count of segment data byte arrays stored for a given segment type.
+     *
+     * @param segmentType identifies the required segment
+     * @return the segment count (zero if no segments exist).
+     */
+    public int getSegmentCount(byte segmentType)
+    {
+        final List<byte[]> segmentList = getSegmentList(segmentType);
         return segmentList == null ? 0 : segmentList.size();
     }
@@ -133,76 +202,69 @@
     /**
      * Removes a specified instance of a segment's data from the collection.  Use this method when more than one
-     * occurrence of segment data for a given marker exists.
-     * @param segmentMarker identifies the required segment
-     * @param occurrence the zero-based index of the segment occurrence to remove.
-     */
-    @SuppressWarnings({ "MismatchedQueryAndUpdateOfCollection" })
-    public void removeSegmentOccurrence(byte segmentMarker, int occurrence)
-    {
-        final List<byte[]> segmentList = _segmentDataMap.get(Byte.valueOf(segmentMarker));
+     * occurrence of segment data exists for a given type exists.
+     *
+     * @param segmentType identifies the required segment
+     * @param occurrence  the zero-based index of the segment occurrence to remove.
+     */
+    @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
+    public void removeSegmentOccurrence(@NotNull JpegSegmentType segmentType, int occurrence)
+    {
+        removeSegmentOccurrence(segmentType.byteValue, occurrence);
+    }
+
+    /**
+     * Removes a specified instance of a segment's data from the collection.  Use this method when more than one
+     * occurrence of segment data exists for a given type exists.
+     *
+     * @param segmentType identifies the required segment
+     * @param occurrence  the zero-based index of the segment occurrence to remove.
+     */
+    @SuppressWarnings({"MismatchedQueryAndUpdateOfCollection"})
+    public void removeSegmentOccurrence(byte segmentType, int occurrence)
+    {
+        final List<byte[]> segmentList = _segmentDataMap.get(segmentType);
         segmentList.remove(occurrence);
     }
 
     /**
-     * Removes all segments from the collection having the specified marker.
-     * @param segmentMarker identifies the required segment
-     */
-    public void removeSegment(byte segmentMarker)
-    {
-        _segmentDataMap.remove(Byte.valueOf(segmentMarker));
-    }
-
-    /**
-     * Determines whether data is present for a given segment marker.
-     * @param segmentMarker identifies the required segment
+     * Removes all segments from the collection having the specified type.
+     *
+     * @param segmentType identifies the required segment
+     */
+    public void removeSegment(@NotNull JpegSegmentType segmentType)
+    {
+        removeSegment(segmentType.byteValue);
+    }
+
+    /**
+     * Removes all segments from the collection having the specified type.
+     *
+     * @param segmentType identifies the required segment
+     */
+    public void removeSegment(byte segmentType)
+    {
+        _segmentDataMap.remove(segmentType);
+    }
+
+    /**
+     * Determines whether data is present for a given segment type.
+     *
+     * @param segmentType identifies the required segment
      * @return true if data exists, otherwise false
      */
-    public boolean containsSegment(byte segmentMarker)
-    {
-        return _segmentDataMap.containsKey(Byte.valueOf(segmentMarker));
-    }
-
-    /**
-     * Serialises the contents of a JpegSegmentData to a file.
-     * @param file to file to write from
-     * @param segmentData the data to write
-     * @throws IOException if problems occur while writing
-     */
-    public static void toFile(@NotNull File file, @NotNull JpegSegmentData segmentData) throws IOException
-    {
-        FileOutputStream fileOutputStream = null;
-        try
-        {
-            fileOutputStream = new FileOutputStream(file);
-            new ObjectOutputStream(fileOutputStream).writeObject(segmentData);
-        }
-        finally
-        {
-            if (fileOutputStream!=null)
-                fileOutputStream.close();
-        }
-    }
-
-    /**
-     * Deserialises the contents of a JpegSegmentData from a file.
-     * @param file the file to read from
-     * @return the JpegSegmentData as read
-     * @throws IOException if problems occur while reading
-     * @throws ClassNotFoundException if problems occur while deserialising
-     */
-    @NotNull
-    public static JpegSegmentData fromFile(@NotNull File file) throws IOException, ClassNotFoundException
-    {
-        ObjectInputStream inputStream = null;
-        try
-        {
-            inputStream = new ObjectInputStream(new FileInputStream(file));
-            return (JpegSegmentData)inputStream.readObject();
-        }
-        finally
-        {
-            if (inputStream!=null)
-                inputStream.close();
-        }
+    public boolean containsSegment(@NotNull JpegSegmentType segmentType)
+    {
+        return containsSegment(segmentType.byteValue);
+    }
+
+    /**
+     * Determines whether data is present for a given segment type.
+     *
+     * @param segmentType identifies the required segment
+     * @return true if data exists, otherwise false
+     */
+    public boolean containsSegment(byte segmentType)
+    {
+        return _segmentDataMap.containsKey(segmentType);
     }
 }
Index: trunk/src/com/drew/imaging/jpeg/JpegSegmentMetadataReader.java
===================================================================
--- trunk/src/com/drew/imaging/jpeg/JpegSegmentMetadataReader.java	(revision 8132)
+++ trunk/src/com/drew/imaging/jpeg/JpegSegmentMetadataReader.java	(revision 8132)
@@ -0,0 +1,32 @@
+package com.drew.imaging.jpeg;
+
+import com.drew.lang.annotations.NotNull;
+import com.drew.metadata.Metadata;
+
+/**
+ * Defines an object that extracts metadata from in JPEG segments.
+ */
+public interface JpegSegmentMetadataReader
+{
+    /**
+     * Gets the set of JPEG segment types that this reader is interested in.
+     */
+    @NotNull
+    public Iterable<JpegSegmentType> getSegmentTypes();
+
+    /**
+     * Gets a value indicating whether the supplied byte data can be processed by this reader. This is not a guarantee
+     * that no errors will occur, but rather a best-effort indication of whether the parse is likely to succeed.
+     * Implementations are expected to check things such as the opening bytes, data length, etc.
+     */
+    public boolean canProcess(@NotNull final byte[] segmentBytes, @NotNull final JpegSegmentType segmentType);
+
+    /**
+     * Extracts metadata from a JPEG segment's byte array and merges it into the specified {@link Metadata} object.
+     *
+     * @param segmentBytes The byte array from which the metadata should be extracted.
+     * @param metadata The {@link Metadata} object into which extracted values should be merged.
+     * @param segmentType The {@link JpegSegmentType} being read.
+     */
+    public void extract(@NotNull final byte[] segmentBytes, @NotNull final Metadata metadata, @NotNull final JpegSegmentType segmentType);
+}
Index: trunk/src/com/drew/imaging/jpeg/JpegSegmentReader.java
===================================================================
--- trunk/src/com/drew/imaging/jpeg/JpegSegmentReader.java	(revision 6127)
+++ trunk/src/com/drew/imaging/jpeg/JpegSegmentReader.java	(revision 8132)
@@ -1,4 +1,4 @@
 /*
- * Copyright 2002-2012 Drew Noakes
+ * Copyright 2002-2015 Drew Noakes
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,278 +16,153 @@
  * More information about this project is available at:
  *
- *    http://drewnoakes.com/code/exif/
- *    http://code.google.com/p/metadata-extractor/
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
  */
 package com.drew.imaging.jpeg;
 
+import com.drew.lang.SequentialReader;
+import com.drew.lang.StreamReader;
 import com.drew.lang.annotations.NotNull;
 import com.drew.lang.annotations.Nullable;
 
-import java.io.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
 
 /**
- * Performs read functions of Jpeg files, returning specific file segments.
- * @author  Drew Noakes http://drewnoakes.com
+ * Performs read functions of JPEG files, returning specific file segments.
+ * <p>
+ * JPEG files are composed of a sequence of consecutive JPEG 'segments'. Each is identified by one of a set of byte
+ * values, modelled in the {@link JpegSegmentType} enumeration. Use <code>readSegments</code> to read out the some
+ * or all segments into a {@link JpegSegmentData} object, from which the raw JPEG segment byte arrays may be accessed.
+ *
+ * @author Drew Noakes https://drewnoakes.com
  */
 public class JpegSegmentReader
 {
-    // TODO add a findAvailableSegments() method
-    // TODO add more segment identifiers
-    // TODO add a getSegmentDescription() method, returning for example 'App1 application data segment, commonly containing Exif data'
-
-    @NotNull
-    private final JpegSegmentData _segmentData;
-
     /**
      * Private, because this segment crashes my algorithm, and searching for it doesn't work (yet).
      */
-    private static final byte SEGMENT_SOS = (byte)0xDA;
+    private static final byte SEGMENT_SOS = (byte) 0xDA;
 
     /**
      * Private, because one wouldn't search for it.
      */
-    private static final byte MARKER_EOI = (byte)0xD9;
-
-    /** APP0 Jpeg segment identifier -- JFIF data (also JFXX apparently). */
-    public static final byte SEGMENT_APP0 = (byte)0xE0;
-    /** APP1 Jpeg segment identifier -- where Exif data is kept.  XMP data is also kept in here, though usually in a second instance. */
-    public static final byte SEGMENT_APP1 = (byte)0xE1;
-    /** APP2 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP2 = (byte)0xE2;
-    /** APP3 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP3 = (byte)0xE3;
-    /** APP4 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP4 = (byte)0xE4;
-    /** APP5 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP5 = (byte)0xE5;
-    /** APP6 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP6 = (byte)0xE6;
-    /** APP7 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP7 = (byte)0xE7;
-    /** APP8 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP8 = (byte)0xE8;
-    /** APP9 Jpeg segment identifier. */
-    public static final byte SEGMENT_APP9 = (byte)0xE9;
-    /** APPA (App10) Jpeg segment identifier -- can hold Unicode comments. */
-    public static final byte SEGMENT_APPA = (byte)0xEA;
-    /** APPB (App11) Jpeg segment identifier. */
-    public static final byte SEGMENT_APPB = (byte)0xEB;
-    /** APPC (App12) Jpeg segment identifier. */
-    public static final byte SEGMENT_APPC = (byte)0xEC;
-    /** APPD (App13) Jpeg segment identifier -- IPTC data in here. */
-    public static final byte SEGMENT_APPD = (byte)0xED;
-    /** APPE (App14) Jpeg segment identifier. */
-    public static final byte SEGMENT_APPE = (byte)0xEE;
-    /** APPF (App15) Jpeg segment identifier. */
-    public static final byte SEGMENT_APPF = (byte)0xEF;
-    /** Start Of Image segment identifier. */
-    public static final byte SEGMENT_SOI = (byte)0xD8;
-    /** Define Quantization Table segment identifier. */
-    public static final byte SEGMENT_DQT = (byte)0xDB;
-    /** Define Huffman Table segment identifier. */
-    public static final byte SEGMENT_DHT = (byte)0xC4;
-    /** Start-of-Frame Zero segment identifier. */
-    public static final byte SEGMENT_SOF0 = (byte)0xC0;
-    /** Jpeg comment segment identifier. */
-    public static final byte SEGMENT_COM = (byte)0xFE;
+    private static final byte MARKER_EOI = (byte) 0xD9;
 
     /**
-     * Creates a JpegSegmentReader for a specific file.
-     * @param file the Jpeg file to read segments from
+     * Processes the provided JPEG data, and extracts the specified JPEG segments into a {@link JpegSegmentData} object.
+     * <p>
+     * Will not return SOS (start of scan) or EOI (end of image) segments.
+     *
+     * @param file a {@link File} from which the JPEG data will be read.
+     * @param segmentTypes the set of JPEG segments types that are to be returned. If this argument is <code>null</code>
+     *                     then all found segment types are returned.
      */
-    @SuppressWarnings({ "ConstantConditions" })
-    public JpegSegmentReader(@NotNull File file) throws JpegProcessingException, IOException
+    @NotNull
+    public static JpegSegmentData readSegments(@NotNull File file, @Nullable Iterable<JpegSegmentType> segmentTypes) throws JpegProcessingException, IOException
     {
-        if (file==null)
-            throw new NullPointerException();
-
-        InputStream inputStream = null;
+        FileInputStream stream = null;
         try {
-            inputStream = new FileInputStream(file);
-            _segmentData = readSegments(new BufferedInputStream(inputStream), false);
+            stream = new FileInputStream(file);
+            return readSegments(new StreamReader(stream), segmentTypes);
         } finally {
-            if (inputStream != null)
-                inputStream.close();
+            if (stream != null) {
+                stream.close();
+            }
         }
     }
 
     /**
-     * Creates a JpegSegmentReader for a byte array.
-     * @param fileContents the byte array containing Jpeg data
+     * Processes the provided JPEG data, and extracts the specified JPEG segments into a {@link JpegSegmentData} object.
+     * <p>
+     * Will not return SOS (start of scan) or EOI (end of image) segments.
+     *
+     * @param reader a {@link SequentialReader} from which the JPEG data will be read. It must be positioned at the
+     *               beginning of the JPEG data stream.
+     * @param segmentTypes the set of JPEG segments types that are to be returned. If this argument is <code>null</code>
+     *                     then all found segment types are returned.
      */
-    @SuppressWarnings({ "ConstantConditions" })
-    public JpegSegmentReader(@NotNull byte[] fileContents) throws JpegProcessingException
+    @NotNull
+    public static JpegSegmentData readSegments(@NotNull final SequentialReader reader, @Nullable Iterable<JpegSegmentType> segmentTypes) throws JpegProcessingException, IOException
     {
-        if (fileContents==null)
-            throw new NullPointerException();
+        // Must be big-endian
+        assert (reader.isMotorolaByteOrder());
 
-        BufferedInputStream stream = new BufferedInputStream(new ByteArrayInputStream(fileContents));
-        _segmentData = readSegments(stream, false);
+        // first two bytes should be JPEG magic number
+        final int magicNumber = reader.getUInt16();
+        if (magicNumber != 0xFFD8) {
+            throw new JpegProcessingException("JPEG data is expected to begin with 0xFFD8 (ÿØ) not 0x" + Integer.toHexString(magicNumber));
+        }
+
+        Set<Byte> segmentTypeBytes = null;
+        if (segmentTypes != null) {
+            segmentTypeBytes = new HashSet<Byte>();
+            for (JpegSegmentType segmentType : segmentTypes) {
+                segmentTypeBytes.add(segmentType.byteValue);
+            }
+        }
+
+        JpegSegmentData segmentData = new JpegSegmentData();
+
+        do {
+            // Find the segment marker. Markers are zero or more 0xFF bytes, followed
+            // by a 0xFF and then a byte not equal to 0x00 or 0xFF.
+
+            final short segmentIdentifier = reader.getUInt8();
+
+            // We must have at least one 0xFF byte
+            if (segmentIdentifier != 0xFF)
+                throw new JpegProcessingException("Expected JPEG segment start identifier 0xFF, not 0x" + Integer.toHexString(segmentIdentifier).toUpperCase());
+
+            // Read until we have a non-0xFF byte. This identifies the segment type.
+            byte segmentType = reader.getInt8();
+            while (segmentType == (byte)0xFF)
+                segmentType = reader.getInt8();
+
+            if (segmentType == 0)
+                throw new JpegProcessingException("Expected non-zero byte as part of JPEG marker identifier");
+
+            if (segmentType == SEGMENT_SOS) {
+                // The 'Start-Of-Scan' segment's length doesn't include the image data, instead would
+                // have to search for the two bytes: 0xFF 0xD9 (EOI).
+                // It comes last so simply return at this point
+                return segmentData;
+            }
+
+            if (segmentType == MARKER_EOI) {
+                // the 'End-Of-Image' segment -- this should never be found in this fashion
+                return segmentData;
+            }
+
+            // next 2-bytes are <segment-size>: [high-byte] [low-byte]
+            int segmentLength = reader.getUInt16();
+
+            // segment length includes size bytes, so subtract two
+            segmentLength -= 2;
+
+            if (segmentLength < 0)
+                throw new JpegProcessingException("JPEG segment size would be less than zero");
+
+            // Check whether we are interested in this segment
+            if (segmentTypeBytes == null || segmentTypeBytes.contains(segmentType)) {
+                byte[] segmentBytes = reader.getBytes(segmentLength);
+                assert (segmentLength == segmentBytes.length);
+                segmentData.addSegment(segmentType, segmentBytes);
+            } else {
+                // Some if the JPEG is truncated, just return what data we've already gathered
+                if (!reader.trySkip(segmentLength)) {
+                    return segmentData;
+                }
+            }
+
+        } while (true);
     }
 
-    /**
-     * Creates a JpegSegmentReader for an InputStream.
-     * @param inputStream the InputStream containing Jpeg data
-     */
-    @SuppressWarnings({ "ConstantConditions" })
-    public JpegSegmentReader(@NotNull InputStream inputStream, boolean waitForBytes) throws JpegProcessingException
+    private JpegSegmentReader() throws Exception
     {
-        if (inputStream==null)
-            throw new NullPointerException();
-
-        BufferedInputStream bufferedInputStream = inputStream instanceof BufferedInputStream
-                ? (BufferedInputStream)inputStream
-                : new BufferedInputStream(inputStream);
-
-        _segmentData = readSegments(bufferedInputStream, waitForBytes);
-    }
-
-    /**
-     * Reads the first instance of a given Jpeg segment, returning the contents as
-     * a byte array.
-     * @param segmentMarker the byte identifier for the desired segment
-     * @return the byte array if found, else null
-     */
-    @Nullable
-    public byte[] readSegment(byte segmentMarker)
-    {
-        return readSegment(segmentMarker, 0);
-    }
-
-    /**
-     * Reads the Nth instance of a given Jpeg segment, returning the contents as a byte array.
-     * 
-     * @param segmentMarker the byte identifier for the desired segment
-     * @param occurrence the occurrence of the specified segment within the jpeg file
-     * @return the byte array if found, else null
-     */
-    @Nullable
-    public byte[] readSegment(byte segmentMarker, int occurrence)
-    {
-        return _segmentData.getSegment(segmentMarker, occurrence);
-    }
-
-    /**
-     * Returns all instances of a given Jpeg segment.  If no instances exist, an empty sequence is returned.
-     *
-     * @param segmentMarker a number which identifies the type of Jpeg segment being queried
-     * @return zero or more byte arrays, each holding the data of a Jpeg segment
-     */
-    @NotNull
-    public Iterable<byte[]> readSegments(byte segmentMarker)
-    {
-        return _segmentData.getSegments(segmentMarker);
-    }
-
-    /**
-     * Returns the number of segments having the specified JPEG segment marker.
-     * @param segmentMarker the JPEG segment identifying marker.
-     * @return the count of matching segments.
-     */
-    public final int getSegmentCount(byte segmentMarker)
-    {
-        return _segmentData.getSegmentCount(segmentMarker);
-    }
-
-    /**
-     * Returns the JpegSegmentData object used by this reader.
-     * @return the JpegSegmentData object.
-     */
-    @NotNull
-    public final JpegSegmentData getSegmentData()
-    {
-        return _segmentData;
-    }
-
-    @NotNull
-    private JpegSegmentData readSegments(@NotNull final BufferedInputStream jpegInputStream, boolean waitForBytes) throws JpegProcessingException
-    {
-        JpegSegmentData segmentData = new JpegSegmentData();
-
-        try {
-            int offset = 0;
-            // first two bytes should be jpeg magic number
-            byte[] headerBytes = new byte[2];
-            if (jpegInputStream.read(headerBytes, 0, 2)!=2)
-                throw new JpegProcessingException("not a jpeg file");
-            final boolean hasValidHeader = (headerBytes[0] & 0xFF) == 0xFF && (headerBytes[1] & 0xFF) == 0xD8;
-            if (!hasValidHeader)
-                throw new JpegProcessingException("not a jpeg file");
-
-            offset += 2;
-            do {
-                // need four bytes from stream for segment header before continuing
-                if (!checkForBytesOnStream(jpegInputStream, 4, waitForBytes))
-                    throw new JpegProcessingException("stream ended before segment header could be read");
-
-                // next byte is 0xFF
-                byte segmentIdentifier = (byte)(jpegInputStream.read() & 0xFF);
-                if ((segmentIdentifier & 0xFF) != 0xFF) {
-                    throw new JpegProcessingException("expected jpeg segment start identifier 0xFF at offset " + offset + ", not 0x" + Integer.toHexString(segmentIdentifier & 0xFF));
-                }
-                offset++;
-                // next byte is <segment-marker>
-                byte thisSegmentMarker = (byte)(jpegInputStream.read() & 0xFF);
-                offset++;
-                // next 2-bytes are <segment-size>: [high-byte] [low-byte]
-                byte[] segmentLengthBytes = new byte[2];
-                if (jpegInputStream.read(segmentLengthBytes, 0, 2) != 2)
-                    throw new JpegProcessingException("Jpeg data ended unexpectedly.");
-                offset += 2;
-                int segmentLength = ((segmentLengthBytes[0] << 8) & 0xFF00) | (segmentLengthBytes[1] & 0xFF);
-                // segment length includes size bytes, so subtract two
-                segmentLength -= 2;
-                if (!checkForBytesOnStream(jpegInputStream, segmentLength, waitForBytes))
-                    throw new JpegProcessingException("segment size would extend beyond file stream length");
-                if (segmentLength < 0)
-                    throw new JpegProcessingException("segment size would be less than zero");
-                byte[] segmentBytes = new byte[segmentLength];
-                if (jpegInputStream.read(segmentBytes, 0, segmentLength) != segmentLength)
-                    throw new JpegProcessingException("Jpeg data ended unexpectedly.");
-                offset += segmentLength;
-                if ((thisSegmentMarker & 0xFF) == (SEGMENT_SOS & 0xFF)) {
-                    // The 'Start-Of-Scan' segment's length doesn't include the image data, instead would
-                    // have to search for the two bytes: 0xFF 0xD9 (EOI).
-                    // It comes last so simply return at this point
-                    return segmentData;
-                } else if ((thisSegmentMarker & 0xFF) == (MARKER_EOI & 0xFF)) {
-                    // the 'End-Of-Image' segment -- this should never be found in this fashion
-                    return segmentData;
-                } else {
-                    segmentData.addSegment(thisSegmentMarker, segmentBytes);
-                }
-            } while (true);
-        } catch (IOException ioe) {
-            throw new JpegProcessingException("IOException processing Jpeg file: " + ioe.getMessage(), ioe);
-        } finally {
-            try {
-                if (jpegInputStream != null) {
-                    jpegInputStream.close();
-                }
-            } catch (IOException ioe) {
-                throw new JpegProcessingException("IOException processing Jpeg file: " + ioe.getMessage(), ioe);
-            }
-        }
-    }
-
-    private boolean checkForBytesOnStream(@NotNull BufferedInputStream stream, int bytesNeeded, boolean waitForBytes) throws IOException
-    {
-        // NOTE  waiting is essential for network streams where data can be delayed, but it is not necessary for byte[] or filesystems
-
-        if (!waitForBytes)
-            return bytesNeeded <= stream.available();
-
-        int count = 40; // * 100ms = approx 4 seconds
-        while (count > 0) {
-            if (bytesNeeded <= stream.available())
-               return true;
-            try {
-                Thread.sleep(100);
-            } catch (InterruptedException e) {
-                // continue
-            }
-            count--;
-        }
-        return false;
+        throw new Exception("Not intended for instantiation.");
     }
 }
Index: trunk/src/com/drew/imaging/jpeg/JpegSegmentType.java
===================================================================
--- trunk/src/com/drew/imaging/jpeg/JpegSegmentType.java	(revision 8132)
+++ trunk/src/com/drew/imaging/jpeg/JpegSegmentType.java	(revision 8132)
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2002-2015 Drew Noakes
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ *
+ * More information about this project is available at:
+ *
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
+ */
+package com.drew.imaging.jpeg;
+
+import com.drew.lang.annotations.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * An enumeration of the known segment types found in JPEG files.
+ *
+ * @author Drew Noakes https://drewnoakes.com
+ */
+public enum JpegSegmentType
+{
+    /** APP0 JPEG segment identifier -- JFIF data (also JFXX apparently). */
+    APP0((byte)0xE0, true),
+
+    /** APP1 JPEG segment identifier -- where Exif data is kept.  XMP data is also kept in here, though usually in a second instance. */
+    APP1((byte)0xE1, true),
+
+    /** APP2 JPEG segment identifier. */
+    APP2((byte)0xE2, true),
+
+    /** APP3 JPEG segment identifier. */
+    APP3((byte)0xE3, true),
+
+    /** APP4 JPEG segment identifier. */
+    APP4((byte)0xE4, true),
+
+    /** APP5 JPEG segment identifier. */
+    APP5((byte)0xE5, true),
+
+    /** APP6 JPEG segment identifier. */
+    APP6((byte)0xE6, true),
+
+    /** APP7 JPEG segment identifier. */
+    APP7((byte)0xE7, true),
+
+    /** APP8 JPEG segment identifier. */
+    APP8((byte)0xE8, true),
+
+    /** APP9 JPEG segment identifier. */
+    APP9((byte)0xE9, true),
+
+    /** APPA (App10) JPEG segment identifier -- can hold Unicode comments. */
+    APPA((byte)0xEA, true),
+
+    /** APPB (App11) JPEG segment identifier. */
+    APPB((byte)0xEB, true),
+
+    /** APPC (App12) JPEG segment identifier. */
+    APPC((byte)0xEC, true),
+
+    /** APPD (App13) JPEG segment identifier -- IPTC data in here. */
+    APPD((byte)0xED, true),
+
+    /** APPE (App14) JPEG segment identifier. */
+    APPE((byte)0xEE, true),
+
+    /** APPF (App15) JPEG segment identifier. */
+    APPF((byte)0xEF, true),
+
+    /** Start Of Image segment identifier. */
+    SOI((byte)0xD8, false),
+
+    /** Define Quantization Table segment identifier. */
+    DQT((byte)0xDB, false),
+
+    /** Define Huffman Table segment identifier. */
+    DHT((byte)0xC4, false),
+
+    /** Start-of-Frame (0) segment identifier. */
+    SOF0((byte)0xC0, true),
+
+    /** Start-of-Frame (1) segment identifier. */
+    SOF1((byte)0xC1, true),
+
+    /** Start-of-Frame (2) segment identifier. */
+    SOF2((byte)0xC2, true),
+
+    /** Start-of-Frame (3) segment identifier. */
+    SOF3((byte)0xC3, true),
+
+//    /** Start-of-Frame (4) segment identifier. */
+//    SOF4((byte)0xC4, true),
+
+    /** Start-of-Frame (5) segment identifier. */
+    SOF5((byte)0xC5, true),
+
+    /** Start-of-Frame (6) segment identifier. */
+    SOF6((byte)0xC6, true),
+
+    /** Start-of-Frame (7) segment identifier. */
+    SOF7((byte)0xC7, true),
+
+    /** Start-of-Frame (8) segment identifier. */
+    SOF8((byte)0xC8, true),
+
+    /** Start-of-Frame (9) segment identifier. */
+    SOF9((byte)0xC9, true),
+
+    /** Start-of-Frame (10) segment identifier. */
+    SOF10((byte)0xCA, true),
+
+    /** Start-of-Frame (11) segment identifier. */
+    SOF11((byte)0xCB, true),
+
+//    /** Start-of-Frame (12) segment identifier. */
+//    SOF12((byte)0xCC, true),
+
+    /** Start-of-Frame (13) segment identifier. */
+    SOF13((byte)0xCD, true),
+
+    /** Start-of-Frame (14) segment identifier. */
+    SOF14((byte)0xCE, true),
+
+    /** Start-of-Frame (15) segment identifier. */
+    SOF15((byte)0xCF, true),
+
+    /** JPEG comment segment identifier. */
+    COM((byte)0xFE, true);
+
+    public static final Collection<JpegSegmentType> canContainMetadataTypes;
+
+    static {
+        List<JpegSegmentType> segmentTypes = new ArrayList<JpegSegmentType>();
+        for (JpegSegmentType segmentType : JpegSegmentType.class.getEnumConstants()) {
+            if (segmentType.canContainMetadata) {
+                segmentTypes.add(segmentType);
+            }
+        }
+        canContainMetadataTypes = segmentTypes;
+    }
+
+    public final byte byteValue;
+    public final boolean canContainMetadata;
+
+    JpegSegmentType(byte byteValue, boolean canContainMetadata)
+    {
+        this.byteValue = byteValue;
+        this.canContainMetadata = canContainMetadata;
+    }
+
+    @Nullable
+    public static JpegSegmentType fromByte(byte segmentTypeByte)
+    {
+        for (JpegSegmentType segmentType : JpegSegmentType.class.getEnumConstants()) {
+            if (segmentType.byteValue == segmentTypeByte)
+                return segmentType;
+        }
+        return null;
+    }
+}
Index: trunk/src/com/drew/imaging/jpeg/package.html
===================================================================
--- trunk/src/com/drew/imaging/jpeg/package.html	(revision 8132)
+++ trunk/src/com/drew/imaging/jpeg/package.html	(revision 8132)
@@ -0,0 +1,33 @@
+<!--
+  ~ Copyright 2002-2015 Drew Noakes
+  ~
+  ~    Licensed under the Apache License, Version 2.0 (the "License");
+  ~    you may not use this file except in compliance with the License.
+  ~    You may obtain a copy of the License at
+  ~
+  ~        http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~    Unless required by applicable law or agreed to in writing, software
+  ~    distributed under the License is distributed on an "AS IS" BASIS,
+  ~    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~    See the License for the specific language governing permissions and
+  ~    limitations under the License.
+  ~
+  ~ More information about this project is available at:
+  ~
+  ~    https://drewnoakes.com/code/exif/
+  ~    https://github.com/drewnoakes/metadata-extractor
+  -->
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body bgcolor="white">
+
+Contains classes for working with JPEG files.
+
+<!-- Put @see and @since tags down here. -->
+
+</body>
+</html>
Index: trunk/src/com/drew/imaging/package.html
===================================================================
--- trunk/src/com/drew/imaging/package.html	(revision 8132)
+++ trunk/src/com/drew/imaging/package.html	(revision 8132)
@@ -0,0 +1,33 @@
+<!--
+  ~ Copyright 2002-2015 Drew Noakes
+  ~
+  ~    Licensed under the Apache License, Version 2.0 (the "License");
+  ~    you may not use this file except in compliance with the License.
+  ~    You may obtain a copy of the License at
+  ~
+  ~        http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~    Unless required by applicable law or agreed to in writing, software
+  ~    distributed under the License is distributed on an "AS IS" BASIS,
+  ~    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~    See the License for the specific language governing permissions and
+  ~    limitations under the License.
+  ~
+  ~ More information about this project is available at:
+  ~
+  ~    https://drewnoakes.com/code/exif/
+  ~    https://github.com/drewnoakes/metadata-extractor
+  -->
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body bgcolor="white">
+
+Contains classes for working with image file formats and photographic conversions.
+
+<!-- Put @see and @since tags down here. -->
+
+</body>
+</html>
Index: trunk/src/com/drew/imaging/tiff/TiffDataFormat.java
===================================================================
--- trunk/src/com/drew/imaging/tiff/TiffDataFormat.java	(revision 8132)
+++ trunk/src/com/drew/imaging/tiff/TiffDataFormat.java	(revision 8132)
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2002-2015 Drew Noakes
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ *
+ * More information about this project is available at:
+ *
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
+ */
+package com.drew.imaging.tiff;
+
+import com.drew.lang.annotations.NotNull;
+import com.drew.lang.annotations.Nullable;
+
+/**
+ * An enumeration of data formats used by the TIFF specification.
+ *
+ * @author Drew Noakes https://drewnoakes.com
+ */
+public class TiffDataFormat
+{
+    public static final int CODE_INT8_U = 1;
+    public static final int CODE_STRING = 2;
+    public static final int CODE_INT16_U = 3;
+    public static final int CODE_INT32_U = 4;
+    public static final int CODE_RATIONAL_U = 5;
+    public static final int CODE_INT8_S = 6;
+    public static final int CODE_UNDEFINED = 7;
+    public static final int CODE_INT16_S = 8;
+    public static final int CODE_INT32_S = 9;
+    public static final int CODE_RATIONAL_S = 10;
+    public static final int CODE_SINGLE = 11;
+    public static final int CODE_DOUBLE = 12;
+
+    @NotNull public static final TiffDataFormat INT8_U = new TiffDataFormat("BYTE", CODE_INT8_U, 1);
+    @NotNull public static final TiffDataFormat STRING = new TiffDataFormat("STRING", CODE_STRING, 1);
+    @NotNull public static final TiffDataFormat INT16_U = new TiffDataFormat("USHORT", CODE_INT16_U, 2);
+    @NotNull public static final TiffDataFormat INT32_U = new TiffDataFormat("ULONG", CODE_INT32_U, 4);
+    @NotNull public static final TiffDataFormat RATIONAL_U = new TiffDataFormat("URATIONAL", CODE_RATIONAL_U, 8);
+    @NotNull public static final TiffDataFormat INT8_S = new TiffDataFormat("SBYTE", CODE_INT8_S, 1);
+    @NotNull public static final TiffDataFormat UNDEFINED = new TiffDataFormat("UNDEFINED", CODE_UNDEFINED, 1);
+    @NotNull public static final TiffDataFormat INT16_S = new TiffDataFormat("SSHORT", CODE_INT16_S, 2);
+    @NotNull public static final TiffDataFormat INT32_S = new TiffDataFormat("SLONG", CODE_INT32_S, 4);
+    @NotNull public static final TiffDataFormat RATIONAL_S = new TiffDataFormat("SRATIONAL", CODE_RATIONAL_S, 8);
+    @NotNull public static final TiffDataFormat SINGLE = new TiffDataFormat("SINGLE", CODE_SINGLE, 4);
+    @NotNull public static final TiffDataFormat DOUBLE = new TiffDataFormat("DOUBLE", CODE_DOUBLE, 8);
+
+    @NotNull
+    private final String _name;
+    private final int _tiffFormatCode;
+    private final int _componentSizeBytes;
+
+    @Nullable
+    public static TiffDataFormat fromTiffFormatCode(int tiffFormatCode)
+    {
+        switch (tiffFormatCode) {
+            case 1: return INT8_U;
+            case 2: return STRING;
+            case 3: return INT16_U;
+            case 4: return INT32_U;
+            case 5: return RATIONAL_U;
+            case 6: return INT8_S;
+            case 7: return UNDEFINED;
+            case 8: return INT16_S;
+            case 9: return INT32_S;
+            case 10: return RATIONAL_S;
+            case 11: return SINGLE;
+            case 12: return DOUBLE;
+        }
+        return null;
+    }
+
+    private TiffDataFormat(@NotNull String name, int tiffFormatCode, int componentSizeBytes)
+    {
+        _name = name;
+        _tiffFormatCode = tiffFormatCode;
+        _componentSizeBytes = componentSizeBytes;
+    }
+
+    public int getComponentSizeBytes()
+    {
+        return _componentSizeBytes;
+    }
+
+    public int getTiffFormatCode()
+    {
+        return _tiffFormatCode;
+    }
+
+    @Override
+    @NotNull
+    public String toString()
+    {
+        return _name;
+    }
+}
Index: trunk/src/com/drew/imaging/tiff/TiffHandler.java
===================================================================
--- trunk/src/com/drew/imaging/tiff/TiffHandler.java	(revision 8132)
+++ trunk/src/com/drew/imaging/tiff/TiffHandler.java	(revision 8132)
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2002-2015 Drew Noakes
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ *
+ * More information about this project is available at:
+ *
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
+ */
+package com.drew.imaging.tiff;
+
+import com.drew.lang.RandomAccessReader;
+import com.drew.lang.Rational;
+import com.drew.lang.annotations.NotNull;
+
+import java.io.IOException;
+import java.util.Set;
+
+/**
+ * @author Drew Noakes https://drewnoakes.com
+ */
+public interface TiffHandler
+{
+    /**
+     * Receives the 2-byte marker found in the TIFF header.
+     * <p>
+     * Implementations are not obligated to use this information for any purpose, though it may be useful for
+     * validation or perhaps differentiating the type of mapping to use for observed tags and IFDs.
+     *
+     * @param marker the 2-byte value found at position 2 of the TIFF header
+     */
+    void setTiffMarker(int marker) throws TiffProcessingException;
+
+    boolean isTagIfdPointer(int tagType);
+    boolean hasFollowerIfd();
+
+    void endingIFD();
+
+    void completed(@NotNull final RandomAccessReader reader, final int tiffHeaderOffset);
+
+    boolean customProcessTag(int tagOffset,
+                             @NotNull Set<Integer> processedIfdOffsets,
+                             int tiffHeaderOffset,
+                             @NotNull RandomAccessReader reader,
+                             int tagId,
+                             int byteCount) throws IOException;
+
+    void warn(@NotNull String message);
+    void error(@NotNull String message);
+
+    void setByteArray(int tagId, @NotNull byte[] bytes);
+    void setString(int tagId, @NotNull String string);
+    void setRational(int tagId, @NotNull Rational rational);
+    void setRationalArray(int tagId, @NotNull Rational[] array);
+    void setFloat(int tagId, float float32);
+    void setFloatArray(int tagId, @NotNull float[] array);
+    void setDouble(int tagId, double double64);
+    void setDoubleArray(int tagId, @NotNull double[] array);
+    void setInt8s(int tagId, byte int8s);
+    void setInt8sArray(int tagId, @NotNull byte[] array);
+    void setInt8u(int tagId, short int8u);
+    void setInt8uArray(int tagId, @NotNull short[] array);
+    void setInt16s(int tagId, int int16s);
+    void setInt16sArray(int tagId, @NotNull short[] array);
+    void setInt16u(int tagId, int int16u);
+    void setInt16uArray(int tagId, @NotNull int[] array);
+    void setInt32s(int tagId, int int32s);
+    void setInt32sArray(int tagId, @NotNull int[] array);
+    void setInt32u(int tagId, long int32u);
+    void setInt32uArray(int tagId, @NotNull long[] array);
+}
Index: trunk/src/com/drew/imaging/tiff/TiffProcessingException.java
===================================================================
--- trunk/src/com/drew/imaging/tiff/TiffProcessingException.java	(revision 8132)
+++ trunk/src/com/drew/imaging/tiff/TiffProcessingException.java	(revision 8132)
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2002-2015 Drew Noakes
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ *
+ * More information about this project is available at:
+ *
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
+ */
+
+package com.drew.imaging.tiff;
+
+import com.drew.imaging.ImageProcessingException;
+import com.drew.lang.annotations.Nullable;
+
+/**
+ * An exception class thrown upon unexpected and fatal conditions while processing a TIFF file.
+ *
+ * @author Drew Noakes https://drewnoakes.com
+ * @author Darren Salomons
+ */
+public class TiffProcessingException extends ImageProcessingException
+{
+    private static final long serialVersionUID = -1658134119488001891L;
+
+    public TiffProcessingException(@Nullable String message)
+    {
+        super(message);
+    }
+
+    public TiffProcessingException(@Nullable String message, @Nullable Throwable cause)
+    {
+        super(message, cause);
+    }
+
+    public TiffProcessingException(@Nullable Throwable cause)
+    {
+        super(cause);
+    }
+}
Index: trunk/src/com/drew/imaging/tiff/TiffReader.java
===================================================================
--- trunk/src/com/drew/imaging/tiff/TiffReader.java	(revision 8132)
+++ trunk/src/com/drew/imaging/tiff/TiffReader.java	(revision 8132)
@@ -0,0 +1,368 @@
+/*
+ * Copyright 2002-2015 Drew Noakes
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ *
+ * More information about this project is available at:
+ *
+ *    https://drewnoakes.com/code/exif/
+ *    https://github.com/drewnoakes/metadata-extractor
+ */
+package com.drew.imaging.tiff;
+
+import com.drew.lang.RandomAccessReader;
+import com.drew.lang.Rational;
+import com.drew.lang.annotations.NotNull;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Processes TIFF-formatted data, calling into client code via that {@link TiffHandler} interface.
+ *
+ * @author Drew Noakes https://drewnoakes.com
+ */
+public class TiffReader
+{
+    /**
+     * Processes a TIFF data sequence.
+     *
+     * @param reader the {@link RandomAccessReader} from which the data should be read
+     * @param handler the {@link TiffHandler} that will coordinate processing and accept read values
+     * @param tiffHeaderOffset the offset within <code>reader</code> at which the TIFF header starts
+     * @throws TiffProcessingException if an error occurred during the processing of TIFF data that could not be
+     *                                 ignored or recovered from
+     * @throws IOException an error occurred while accessing the required data
+     */
+    public void processTiff(@NotNull final RandomAccessReader reader,
+                            @NotNull final TiffHandler handler,
+                            final int tiffHeaderOffset) throws TiffProcessingException, IOException
+    {
+        // This must be either "MM" or "II".
+        short byteOrderIdentifier = reader.getInt16(tiffHeaderOffset);
+
+        if (byteOrderIdentifier == 0x4d4d) { // "MM"
+            reader.setMotorolaByteOrder(true);
+        } else if (byteOrderIdentifier == 0x4949) { // "II"
+            reader.setMotorolaByteOrder(false);
+        } else {
+            throw new TiffProcessingException("Unclear distinction between Motorola/Intel byte ordering: " + byteOrderIdentifier);
+        }
+
+        // Check the next two values for correctness.
+        final int tiffMarker = reader.getUInt16(2 + tiffHeaderOffset);
+        handler.setTiffMarker(tiffMarker);
+
+        int firstIfdOffset = reader.getInt32(4 + tiffHeaderOffset) + tiffHeaderOffset;
+
+        // David Ekholm sent a digital camera image that has this problem
+        // TODO getLength should be avoided as it causes RandomAccessStreamReader to read to the end of the stream
+        if (firstIfdOffset >= reader.getLength() - 1) {
+            handler.warn("First IFD offset is beyond the end of the TIFF data segment -- trying default offset");
+            // First directory normally starts immediately after the offset bytes, so try that
+            firstIfdOffset = tiffHeaderOffset + 2 + 2 + 4;
+        }
+
+        Set<Integer> processedIfdOffsets = new HashSet<Integer>();
+        processIfd(handler, reader, processedIfdOffsets, firstIfdOffset, tiffHeaderOffset);
+
+        handler.completed(reader, tiffHeaderOffset);
+    }
+
+    /**
+     * Processes a TIFF IFD.
+     *
+     * IFD Header:
+     * <ul>
+     *     <li><b>2 bytes</b> number of tags</li>
+     * </ul>
+     * Tag structure:
+     * <ul>
+     *     <li><b>2 bytes</b> tag type</li>
+     *     <li><b>2 bytes</b> format code (values 1 to 12, inclusive)</li>
+     *     <li><b>4 bytes</b> component count</li>
+     *     <li><b>4 bytes</b> inline value, or offset pointer if too large to fit in four bytes</li>
+     * </ul>
+     *
+     *
+     * @param handler the {@link com.drew.imaging.tiff.TiffHandler} that will coordinate processing and accept read values
+     * @param reader the {@link com.drew.lang.RandomAccessReader} from which the data should be read
+     * @param processedIfdOffsets the set of visited IFD offsets, to avoid revisiting the same IFD in an endless loop
+     * @param ifdOffset the offset within <code>reader</code> at which the IFD data starts
+     * @param tiffHeaderOffset the offset within <code>reader</code> at which the TIFF header starts
+     * @throws IOException an error occurred while accessing the required data
+     */
+    public static void processIfd(@NotNull final TiffHandler handler,
+                                  @NotNull final RandomAccessReader reader,
+                                  @NotNull final Set<Integer> processedIfdOffsets,
+                                  final int ifdOffset,
+                                  final int tiffHeaderOffset) throws IOException
+    {
+        try {
+            // check for directories we've already visited to avoid stack overflows when recursive/cyclic directory structures exist
+            if (processedIfdOffsets.contains(Integer.valueOf(ifdOffset))) {
+                return;
+            }
+
+            // remember that we've visited this directory so that we don't visit it again later
+            processedIfdOffsets.add(ifdOffset);
+
+            if (ifdOffset >= reader.getLength() || ifdOffset < 0) {
+                handler.error("Ignored IFD marked to start outside data segment");
+                return;
+            }
+
+            // First two bytes in the IFD are the number of tags in this directory
+            int dirTagCount = reader.getUInt16(ifdOffset);
+
+            int dirLength = (2 + (12 * dirTagCount) + 4);
+            if (dirLength + ifdOffset > reader.getLength()) {
+                handler.error("Illegally sized IFD");
+                return;
+            }
+
+            //
+            // Handle each tag in this directory
+            //
+            int invalidTiffFormatCodeCount = 0;
+            for (int tagNumber = 0; tagNumber < dirTagCount; tagNumber++) {
+                final int tagOffset = calculateTagOffset(ifdOffset, tagNumber);
+
+                // 2 bytes for the tag id
+                final int tagId = reader.getUInt16(tagOffset);
+
+                // 2 bytes for the format code
+                final int formatCode = reader.getUInt16(tagOffset + 2);
+                final TiffDataFormat format = TiffDataFormat.fromTiffFormatCode(formatCode);
+
+                if (format == null) {
+                    // This error suggests that we are processing at an incorrect index and will generate
+                    // rubbish until we go out of bounds (which may be a while).  Exit now.
+                    handler.error("Invalid TIFF tag format code: " + formatCode);
+                    // TODO specify threshold as a parameter, or provide some other external control over this behaviour
+                    if (++invalidTiffFormatCodeCount > 5) {
+                        handler.error("Stopping processing as too many errors seen in TIFF IFD");
+                        return;
+                    }
+                    continue;
+                }
+
+                // 4 bytes dictate the number of components in this tag's data
+                final int componentCount = reader.getInt32(tagOffset + 4);
+                if (componentCount < 0) {
+                    handler.error("Negative TIFF tag component count");
+                    continue;
+                }
+
+                final int byteCount = componentCount * format.getComponentSizeBytes();
+
+                final int tagValueOffset;
+                if (byteCount > 4) {
+                    // If it's bigger than 4 bytes, the dir entry contains an offset.
+                    final int offsetVal = reader.getInt32(tagOffset + 8);
+                    if (offsetVal + byteCount > reader.getLength()) {
+                        // Bogus pointer offset and / or byteCount value
+                        handler.error("Illegal TIFF tag pointer offset");
+                        continue;
+                    }
+                    tagValueOffset = tiffHeaderOffset + offsetVal;
+                } else {
+                    // 4 bytes or less and value is in the dir entry itself.
+                    tagValueOffset = tagOffset + 8;
+                }
+
+                if (tagValueOffset < 0 || tagValueOffset > reader.getLength()) {
+                    handler.error("Illegal TIFF tag pointer offset");
+                    continue;
+                }
+
+                // Check that this tag isn't going to allocate outside the bounds of the data array.
+                // This addresses an uncommon OutOfMemoryError.
+                if (byteCount < 0 || tagValueOffset + byteCount > reader.getLength()) {
+                    handler.error("Illegal number of bytes for TIFF tag data: " + byteCount);
+                    continue;
+                }
+
+                //
+                // Special handling for tags that point to other IFDs
+                //
+                if (byteCount == 4 && handler.isTagIfdPointer(tagId)) {
+                    final int subDirOffset = tiffHeaderOffset + reader.getInt32(tagValueOffset);
+                    processIfd(handler, reader, processedIfdOffsets, subDirOffset, tiffHeaderOffset);
+                } else {
+                    if (!handler.customProcessTag(tagValueOffset, processedIfdOffsets, tiffHeaderOffset, reader, tagId, byteCount)) {
+                        processTag(handler, tagId, tagValueOffset, componentCount, formatCode, reader);
+                    }
+                }
+            }
+
+            // at the end of each IFD is an optional link to the next IFD
+            final int finalTagOffset = calculateTagOffset(ifdOffset, dirTagCount);
+            int nextIfdOffset = reader.getInt32(finalTagOffset);
+            if (nextIfdOffset != 0) {
+                nextIfdOffset += tiffHeaderOffset;
+                if (nextIfdOffset >= reader.getLength()) {
+                    // Last 4 bytes of IFD reference another IFD with an address that is out of bounds
+                    // Note this could have been caused by jhead 1.3 cropping too much
+                    return;
+                } else if (nextIfdOffset < ifdOffset) {
+                    // TODO is this a valid restriction?
+                    // Last 4 bytes of IFD reference another IFD with an address that is before the start of this directory
+                    return;
+                }
+
+                if (handler.hasFollowerIfd()) {
+                    processIfd(handler, reader, processedIfdOffsets, nextIfdOffset, tiffHeaderOffset);
+                }
+            }
+        } finally {
+            handler.endingIFD();
+        }
+    }
+
+    private static void processTag(@NotNull final TiffHandler handler,
+                                   final int tagId,
+                                   final int tagValueOffset,
+                                   final int componentCount,
+                                   final int formatCode,
+                                   @NotNull final RandomAccessReader reader) throws IOException
+    {
+        switch (formatCode) {
+            case TiffDataFormat.CODE_UNDEFINED:
+                // this includes exif user comments
+                handler.setByteArray(tagId, reader.getBytes(tagValueOffset, componentCount));
+                break;
+            case TiffDataFormat.CODE_STRING:
+                handler.setString(tagId, reader.getNullTerminatedString(tagValueOffset, componentCount));
+                break;
+            case TiffDataFormat.CODE_RATIONAL_S:
+                if (componentCount == 1) {
+                    handler.setRational(tagId, new Rational(reader.getInt32(tagValueOffset), reader.getInt32(tagValueOffset + 4)));
+                } else if (componentCount > 1) {
+                    Rational[] array = new Rational[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = new Rational(reader.getInt32(tagValueOffset + (8 * i)), reader.getInt32(tagValueOffset + 4 + (8 * i)));
+                    handler.setRationalArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_RATIONAL_U:
+                if (componentCount == 1) {
+                    handler.setRational(tagId, new Rational(reader.getUInt32(tagValueOffset), reader.getUInt32(tagValueOffset + 4)));
+                } else if (componentCount > 1) {
+                    Rational[] array = new Rational[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = new Rational(reader.getUInt32(tagValueOffset + (8 * i)), reader.getUInt32(tagValueOffset + 4 + (8 * i)));
+                    handler.setRationalArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_SINGLE:
+                if (componentCount == 1) {
+                    handler.setFloat(tagId, reader.getFloat32(tagValueOffset));
+                } else {
+                    float[] array = new float[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getFloat32(tagValueOffset + (i * 4));
+                    handler.setFloatArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_DOUBLE:
+                if (componentCount == 1) {
+                    handler.setDouble(tagId, reader.getDouble64(tagValueOffset));
+                } else {
+                    double[] array = new double[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getDouble64(tagValueOffset + (i * 4));
+                    handler.setDoubleArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_INT8_S:
+                if (componentCount == 1) {
+                    handler.setInt8s(tagId, reader.getInt8(tagValueOffset));
+                } else {
+                    byte[] array = new byte[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getInt8(tagValueOffset + i);
+                    handler.setInt8sArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_INT8_U:
+                if (componentCount == 1) {
+                    handler.setInt8u(tagId, reader.getUInt8(tagValueOffset));
+                } else {
+                    short[] array = new short[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getUInt8(tagValueOffset + i);
+                    handler.setInt8uArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_INT16_S:
+                if (componentCount == 1) {
+                    handler.setInt16s(tagId, (int)reader.getInt16(tagValueOffset));
+                } else {
+                    short[] array = new short[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getInt16(tagValueOffset + (i * 2));
+                    handler.setInt16sArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_INT16_U:
+                if (componentCount == 1) {
+                    handler.setInt16u(tagId, reader.getUInt16(tagValueOffset));
+                } else {
+                    int[] array = new int[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getUInt16(tagValueOffset + (i * 2));
+                    handler.setInt16uArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_INT32_S:
+                // NOTE 'long' in this case means 32 bit, not 64
+                if (componentCount == 1) {
+                    handler.setInt32s(tagId, reader.getInt32(tagValueOffset));
+                } else {
+                    int[] array = new int[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getInt32(tagValueOffset + (i * 4));
+                    handler.setInt32sArray(tagId, array);
+                }
+                break;
+            case TiffDataFormat.CODE_INT32_U:
+                // NOTE 'long' in this case means 32 bit, not 64
+                if (componentCount == 1) {
+                    handler.setInt32u(tagId, reader.getUInt32(tagValueOffset));
+                } else {
+                    long[] array = new long[componentCount];
+                    for (int i = 0; i < componentCount; i++)
+                        array[i] = reader.getUInt32(tagValueOffset + (i * 4));
+                    handler.setInt32uArray(tagId, array);
+                }
+                break;
+            default:
+                handler.error(String.format("Unknown format code %d for tag %d", formatCode, tagId));
+        }
+    }
+
+    /**
+     * Determine the offset of a given tag within the specified IFD.
+     *
+     * @param ifdStartOffset the offset at which the IFD starts
+     * @param entryNumber    the zero-based entry number
+     */
+    private static int calculateTagOffset(int ifdStartOffset, int entryNumber)
+    {
+        // Add 2 bytes for the tag count.
+        // Each entry is 12 bytes.
+        return ifdStartOffset + 2 + (12 * entryNumber);
+    }
+}
Index: trunk/src/com/drew/imaging/tiff/package.html
===================================================================
--- trunk/src/com/drew/imaging/tiff/package.html	(revision 8132)
+++ trunk/src/com/drew/imaging/tiff/package.html	(revision 8132)
@@ -0,0 +1,33 @@
+<!--
+  ~ Copyright 2002-2015 Drew Noakes
+  ~
+  ~    Licensed under the Apache License, Version 2.0 (the "License");
+  ~    you may not use this file except in compliance with the License.
+  ~    You may obtain a copy of the License at
+  ~
+  ~        http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~    Unless required by applicable law or agreed to in writing, software
+  ~    distributed under the License is distributed on an "AS IS" BASIS,
+  ~    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~    See the License for the specific language governing permissions and
+  ~    limitations under the License.
+  ~
+  ~ More information about this project is available at:
+  ~
+  ~    https://drewnoakes.com/code/exif/
+  ~    https://github.com/drewnoakes/metadata-extractor
+  -->
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
+<html>
+<head>
+</head>
+<body bgcolor="white">
+
+Contains classes for working with TIFF format files.
+
+<!-- Put @see and @since tags down here. -->
+
+</body>
+</html>
