private static List<Section> parse(InputStream is,boolean readMetaOnly){
  try {
    if (is.read() != 0xff || is.read() != M_SOI) {
      return null;
    }
    List<Section> sections=new ArrayList<Section>();
    int c;
    while ((c=is.read()) != -1) {
      if (c != 0xff) {
        return null;
      }
      while ((c=is.read()) == 0xff) {
      }
      if (c == -1) {
        return null;
      }
      int marker=c;
      if (marker == M_SOS) {
        if (!readMetaOnly) {
          Section section=new Section();
          section.marker=marker;
          section.length=-1;
          section.data=new byte[is.available()];
          is.read(section.data,0,section.data.length);
          sections.add(section);
        }
        return sections;
      }
      int lh=is.read();
      int ll=is.read();
      if (lh == -1 || ll == -1) {
        return null;
      }
      int length=lh << 8 | ll;
      if (!readMetaOnly || c == M_APP1) {
        Section section=new Section();
        section.marker=marker;
        section.length=length;
        section.data=new byte[length - 2];
        is.read(section.data,0,length - 2);
        sections.add(section);
      }
 else {
        is.skip(length - 2);
      }
    }
    return sections;
  }
 catch (  IOException e) {
    Log.d(TAG,"Could not parse file.",e);
    return null;
  }
 finally {
    if (is != null) {
      try {
        is.close();
      }
 catch (      IOException e) {
      }
    }
  }
}

private static List < Section > parse ( InputStream is , boolean readMetaOnly ) { try { if ( is . read ( ) != 0xff || is . read ( ) != M_SOI ) { return null ; } List < Section > sections = new ArrayList < Section > ( ) ; int c ; while ( ( c = is . read ( ) ) != - 1 ) { if ( c != 0xff ) { return null ; } while ( ( c = is . read ( ) ) == 0xff ) { } if ( c == - 1 ) { return null ; } int marker = c ; if ( marker == M_SOS ) { if ( ! readMetaOnly ) { Section section = new Section ( ) ; section . marker = marker ; section . length = - 1 ; section . data = new byte [ is . available ( ) ] ; is . read ( section . data , 0 , section . data . length ) ; sections . add ( section ) ; } return sections ; } int lh = is . read ( ) ; int ll = is . read ( ) ; if ( lh == - 1 || ll == - 1 ) { return null ; } int length = lh << 8 | ll ; if ( ! readMetaOnly || c == M_APP1 ) { Section section = new Section ( ) ; section . marker = marker ; section . length = length ; section . data = new byte [ length - 2 ] ; is . read ( section . data , 0 , length - 2 ) ; sections . add ( section ) ; } else { is . skip ( length - 2 ) ; } } return sections ; } catch ( IOException e ) { Log . d ( TAG , "Could not parse file." , e ) ; return null ; } finally { if ( is != null ) { try { is . close ( ) ; } catch ( IOException e ) { } } } }
parses the jpeg image file . if readmetaonly is true , only keeps the exif and xmp sections ( with marker m_app1 ) and ignore others ; otherwise , keep all sections . the last section with image data will have - 1 length .
