public void traverseJfif(final ByteSource byteSource, final Visitor visitor) throws ImagingException, IOException {
        try (InputStream is = byteSource.getInputStream()) {
            BinaryFunctions.readAndVerifyBytes(is, JpegConstants.SOI, "Not a Valid JPEG File: doesn't begin with 0xffd8");

            int markerCount;
            for (markerCount = 0; true; markerCount++) {
                final byte[] markerBytes = new byte[2];
                do {
                    markerBytes[0] = markerBytes[1];
                    markerBytes[1] = BinaryFunctions.readByte("marker", is, "Could not read marker");
                } while ((0xff & markerBytes[0]) != 0xff || (0xff & markerBytes[1]) == 0xff);
                final int marker = (0xff & markerBytes[0]) << 8 | 0xff & markerBytes[1];

                if (marker == JpegConstants.EOI_MARKER || marker == JpegConstants.SOS_MARKER) {
                    if (!visitor.beginSos()) {
                        return;
                    }

                    final byte[] imageData = IOUtils.toByteArray(is);
                    visitor.visitSos(marker, markerBytes, imageData);
                    break;
                }

                final byte[] segmentLengthBytes = BinaryFunctions.readBytes("segmentLengthBytes", is, 2, "segmentLengthBytes");
                final int segmentLength = ByteConversions.toUInt16(segmentLengthBytes, getByteOrder());
                if (segmentLength < 2) {
                    throw new ImagingException("Invalid segment size");
                }

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

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

            Debug.debug(markerCount + " markers");
        }
    }
Constructs a new instance with the default, big-endian, byte order.
src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java