public void traverseJFIF(ByteSource byteSource, Visitor visitor)
            throws ImageReadException,
            // ImageWriteException,
            IOException
    {
        InputStream is = null;

        try
        {
            is = byteSource.getInputStream();

            readAndVerifyBytes(is, SOI,
                    "Not a Valid JPEG File: doesn't begin with 0xffd8");

            int byteOrder = getByteOrder();

            for (int markerCount = 0; true; markerCount++)
            {
                byte markerBytes[] = readByteArray("markerBytes", 2, is,
                        "markerBytes");
                int marker = convertByteArrayToShort("marker", markerBytes,
                        byteOrder);

//                Debug.debug("marker", marker + " (0x" + Integer.toHexString(marker) + ")");
//                Debug.debug("markerBytes", markerBytes);

                if (marker == 0xffd9 || marker == SOS_Marker)
                {
                    if (!visitor.beginSOS())
                        return;

                    byte imageData[] = getStreamBytes(is);
                    visitor.visitSOS(marker, markerBytes, imageData);
                    break;
                }

                byte segmentLengthBytes[] = readByteArray("segmentLengthBytes",
                        2, is, "segmentLengthBytes");
                int segmentLength = convertByteArrayToShort("segmentLength",
                        segmentLengthBytes, byteOrder);

//                Debug.debug("segmentLength", segmentLength + " (0x" + Integer.toHexString(segmentLength) + ")");
//                Debug.debug("segmentLengthBytes", segmentLengthBytes);

                byte segmentData[] = readByteArray("Segment Data",
                        segmentLength - 2, is,
                        "Invalid Segment: insufficient data");

                // Debug.debug("segmentLength", segmentLength);

                if (!visitor.visitSegment(marker, markerBytes, segmentLength,
                        segmentLengthBytes, segmentData))
                    return;
            }

        } finally
        {
            try
            {
                if (is != null) {
                    is.close();
                }
            } catch (Exception e)
            {
                Debug.debug(e);
            }
        }
    }