docs: @NotNull
      public static JpegSegmentData readSegments(@NotNull final SequentialReader reader, @Nullable Iterable<JpegSegmentType> segmentTypes) throws JpegProcessingException, IOException
      {
          // Must be big-endian
          assert (reader.isMotorolaByteOrder());
  
          // 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.
  
              byte segmentIdentifier = reader.getInt8();
              byte segmentType = reader.getInt8();
  
              // Read until we have a 0xFF byte followed by a byte that is not 0xFF or 0x00
              while (segmentIdentifier != SEGMENT_IDENTIFIER || segmentType == SEGMENT_IDENTIFIER || segmentType == 0) {
              	segmentIdentifier = segmentType;
              	segmentType = reader.getInt8();
              }
  
              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 {
                  // Skip this segment
                  if (!reader.trySkip(segmentLength)) {
                      // If skipping failed, just return the segments we found so far
                      return segmentData;
                  }
              }
  
          } while (true);
      }