Program: 33339
Project: 33339
Buggy source:
/** 
 * Insert a collection of Metadata into an image
 * @param metadata a collection of Metadata to be inserted
 * @param is InputStream for the image
 * @param os OutputStream for the image with Metadata inserted
 * @throws IOException
 */
public static void insertMetadata(Collection<Metadata> metadata,InputStream is,OutputStream os) throws IOException {
  boolean finished=false;
  int length=0;
  short marker;
  Marker emarker;
  int app0Index=-1;
  int exifIndex=-1;
  ByteArrayOutputStream eightBIMStream=null;
  Map<MetadataType,Metadata> metadataMap=new HashMap<MetadataType,Metadata>();
  for (  Metadata meta : metadata)   metadataMap.put(meta.getType(),meta);
  if (metadataMap.get(MetadataType.IPTC) != null) {
    IRB irb=(IRB)metadataMap.get(MetadataType.PHOTOSHOP_IRB);
    if (irb != null) {
      Map<Short,_8BIM> bimMap=new HashMap<Short,_8BIM>(irb.get8BIM());
      bimMap.remove(ImageResourceID.IPTC_NAA.getValue());
      ByteArrayOutputStream bout=new ByteArrayOutputStream();
      for (      _8BIM bim : bimMap.values())       bim.write(bout);
      metadataMap.put(MetadataType.PHOTOSHOP_IRB,new IRB(bout.toByteArray()));
    }
  }
  List<Segment> segments=new ArrayList<Segment>();
  Exif exif=(Exif)metadataMap.remove(MetadataType.EXIF);
  if (exif != null && exif.isThumbnailRequired() && !exif.containsThumbnail()) {
    is=new FileCacheRandomAccessInputStream(is);
    exif.setThumbnailImage(IMGUtils.createThumbnail(is));
  }
  if (Marker.fromShort(IOUtils.readShortMM(is)) != Marker.SOI)   throw new IllegalArgumentException("String_Node_Str");
  IOUtils.writeShortMM(os,Marker.SOI.getValue());
  marker=IOUtils.readShortMM(is);
  while (!finished) {
    if (Marker.fromShort(marker) == Marker.SOS) {
      JFIF jfif=(JFIF)metadataMap.remove(MetadataType.JPG_JFIF);
      for (int i=0; i < app0Index; i++)       segments.get(i).write(os);
      if (jfif != null) {
      }
      int index=Math.max(app0Index,exifIndex);
      for (int i=(app0Index < 0 ? 0 : app0Index); i < index; i++)       segments.get(i).write(os);
      if (exif != null) {
        exif.write(os);
      }
      XMP xmp=(XMP)metadataMap.remove(MetadataType.XMP);
      if (xmp instanceof JpegXMP) {
        xmp.write(os);
      }
      ICCProfile profile=(ICCProfile)metadataMap.remove(MetadataType.ICC_PROFILE);
      if (profile != null) {
        profile.write(os);
      }
      IPTC iptc=(IPTC)metadataMap.remove(MetadataType.IPTC);
      if (iptc != null) {
        Map<Short,_8BIM> bimMap=null;
        if (eightBIMStream != null) {
          IRB irb=new IRB(eightBIMStream.toByteArray());
          bimMap=new HashMap<Short,_8BIM>(irb.get8BIM());
          bimMap.remove(ImageResourceID.IPTC_NAA.getValue());
        }
        eightBIMStream.reset();
        iptc.write(eightBIMStream);
        _8BIM newBIM=new _8BIM(ImageResourceID.IPTC_NAA.getValue(),"String_Node_Str",eightBIMStream.toByteArray());
        if (bimMap != null) {
          bimMap.put(newBIM.getID(),newBIM);
          writeIRB(os,bimMap.values());
        }
 else {
          writeIRB(os,newBIM);
        }
      }
      IRB irb=(IRB)metadataMap.remove(MetadataType.PHOTOSHOP_IRB);
      if (irb != null) {
        writeIRB(os,irb.get8BIM().values());
      }
      Comments comments=(Comments)metadataMap.remove(MetadataType.COMMENT);
      if (comments != null) {
        for (        String comment : comments.getComments())         writeComment(comment,os);
      }
      for (int i=index; i < segments.size(); i++)       segments.get(i).write(os);
      IOUtils.writeShortMM(os,marker);
      copyToEnd(is,os);
      finished=true;
    }
 else {
      emarker=Marker.fromShort(marker);
switch (emarker) {
case JPG:
case JPG0:
case JPG13:
case TEM:
        segments.add(new Segment(emarker,0,null));
      marker=IOUtils.readShortMM(is);
    break;
case APP0:
  length=IOUtils.readUnsignedShortMM(is);
byte[] app0Bytes=new byte[length - 2];
IOUtils.readFully(is,app0Bytes);
if (metadataMap.get(MetadataType.JPG_JFIF) != null) {
segments.add(new Segment(emarker,length,app0Bytes));
}
 else segments.add(new Segment(emarker,length,app0Bytes));
app0Index=segments.size();
marker=IOUtils.readShortMM(is);
break;
case APP1:
length=IOUtils.readUnsignedShortMM(is);
byte[] temp=new byte[length - 2];
IOUtils.readFully(is,temp);
if (metadataMap.get(MetadataType.XMP) != null && temp.length >= XMP_EXT_ID.length() && new String(temp,0,XMP_EXT_ID.length()).equals(XMP_EXT_ID)) {
;
}
 else if (metadataMap.get(MetadataType.XMP) != null && temp.length >= XMP_ID.length() && new String(temp,0,XMP_ID.length()).equals(XMP_ID)) {
;
}
 else if (exif != null && temp.length >= EXIF_ID.length() && new String(temp,0,EXIF_ID.length()).equals(EXIF_ID)) {
exifIndex=segments.size();
}
 else {
segments.add(new Segment(emarker,length,temp));
}
marker=IOUtils.readShortMM(is);
break;
case APP2:
length=IOUtils.readUnsignedShortMM(is);
byte[] icc_profile_buf=new byte[length - 2];
IOUtils.readFully(is,icc_profile_buf);
if (metadataMap.get(MetadataType.ICC_PROFILE) != null && icc_profile_buf.length >= ICC_PROFILE_ID.length() && new String(icc_profile_buf,0,ICC_PROFILE_ID.length()).equals(ICC_PROFILE_ID)) {
;
}
 else {
segments.add(new Segment(emarker,length,icc_profile_buf));
}
marker=IOUtils.readShortMM(is);
break;
case APP13:
if (eightBIMStream == null) eightBIMStream=new ByteArrayOutputStream();
readAPP13(is,eightBIMStream);
marker=IOUtils.readShortMM(is);
break;
default :
length=IOUtils.readUnsignedShortMM(is);
byte[] buf=new byte[length - 2];
IOUtils.readFully(is,buf);
if (emarker == Marker.UNKNOWN) segments.add(new UnknownSegment(marker,length,buf));
 else segments.add(new Segment(emarker,length,buf));
marker=IOUtils.readShortMM(is);
}
}
}
if (is instanceof RandomAccessInputStream) ((RandomAccessInputStream)is).shallowClose();
}

Fixed version (ground truth):
/** 
 * Insert a collection of Metadata into an image
 * @param metadata a collection of Metadata to be inserted
 * @param is InputStream for the image
 * @param os OutputStream for the image with Metadata inserted
 * @throws IOException
 */
public static void insertMetadata(Collection<Metadata> metadata,InputStream is,OutputStream os) throws IOException {
  boolean finished=false;
  int length=0;
  short marker;
  Marker emarker;
  int app0Index=-1;
  int exifIndex=-1;
  ByteArrayOutputStream eightBIMStream=null;
  Map<MetadataType,Metadata> metadataMap=new HashMap<MetadataType,Metadata>();
  for (  Metadata meta : metadata)   metadataMap.put(meta.getType(),meta);
  if (metadataMap.get(MetadataType.IPTC) != null) {
    IRB irb=(IRB)metadataMap.get(MetadataType.PHOTOSHOP_IRB);
    if (irb != null) {
      Map<Short,_8BIM> bimMap=new HashMap<Short,_8BIM>(irb.get8BIM());
      bimMap.remove(ImageResourceID.IPTC_NAA.getValue());
      ByteArrayOutputStream bout=new ByteArrayOutputStream();
      for (      _8BIM bim : bimMap.values())       bim.write(bout);
      metadataMap.put(MetadataType.PHOTOSHOP_IRB,new IRB(bout.toByteArray()));
    }
  }
  List<Segment> segments=new ArrayList<Segment>();
  Exif exif=(Exif)metadataMap.remove(MetadataType.EXIF);
  if (exif != null && exif.isThumbnailRequired() && !exif.containsThumbnail()) {
    is=new FileCacheRandomAccessInputStream(is);
    exif.setThumbnailImage(IMGUtils.createThumbnail(is));
  }
  if (Marker.fromShort(IOUtils.readShortMM(is)) != Marker.SOI)   throw new IllegalArgumentException("String_Node_Str");
  IOUtils.writeShortMM(os,Marker.SOI.getValue());
  marker=IOUtils.readShortMM(is);
  while (!finished) {
    if (Marker.fromShort(marker) == Marker.SOS) {
      JFIF jfif=(JFIF)metadataMap.remove(MetadataType.JPG_JFIF);
      for (int i=0; i < app0Index; i++)       segments.get(i).write(os);
      if (jfif != null) {
      }
      int index=Math.max(app0Index,exifIndex);
      for (int i=(app0Index < 0 ? 0 : app0Index); i < index; i++)       segments.get(i).write(os);
      if (exif != null) {
        exif.write(os);
      }
      XMP xmp=(XMP)metadataMap.remove(MetadataType.XMP);
      if (xmp instanceof JpegXMP) {
        xmp.write(os);
      }
      ICCProfile profile=(ICCProfile)metadataMap.remove(MetadataType.ICC_PROFILE);
      if (profile != null) {
        profile.write(os);
      }
      IPTC iptc=(IPTC)metadataMap.remove(MetadataType.IPTC);
      if (iptc != null) {
        Map<Short,_8BIM> bimMap=null;
        if (eightBIMStream != null) {
          IRB irb=new IRB(eightBIMStream.toByteArray());
          bimMap=new HashMap<Short,_8BIM>(irb.get8BIM());
          bimMap.remove(ImageResourceID.IPTC_NAA.getValue());
          eightBIMStream.reset();
        }
 else {
          eightBIMStream=new ByteArrayOutputStream();
        }
        iptc.write(eightBIMStream);
        _8BIM newBIM=new _8BIM(ImageResourceID.IPTC_NAA.getValue(),"String_Node_Str",eightBIMStream.toByteArray());
        if (bimMap != null) {
          bimMap.put(newBIM.getID(),newBIM);
          writeIRB(os,bimMap.values());
        }
 else {
          writeIRB(os,newBIM);
        }
      }
      IRB irb=(IRB)metadataMap.remove(MetadataType.PHOTOSHOP_IRB);
      if (irb != null) {
        writeIRB(os,irb.get8BIM().values());
      }
      Comments comments=(Comments)metadataMap.remove(MetadataType.COMMENT);
      if (comments != null) {
        for (        String comment : comments.getComments())         writeComment(comment,os);
      }
      for (int i=index; i < segments.size(); i++)       segments.get(i).write(os);
      IOUtils.writeShortMM(os,marker);
      copyToEnd(is,os);
      finished=true;
    }
 else {
      emarker=Marker.fromShort(marker);
switch (emarker) {
case JPG:
case JPG0:
case JPG13:
case TEM:
        segments.add(new Segment(emarker,0,null));
      marker=IOUtils.readShortMM(is);
    break;
case APP0:
  length=IOUtils.readUnsignedShortMM(is);
byte[] app0Bytes=new byte[length - 2];
IOUtils.readFully(is,app0Bytes);
if (metadataMap.get(MetadataType.JPG_JFIF) != null) {
segments.add(new Segment(emarker,length,app0Bytes));
}
 else segments.add(new Segment(emarker,length,app0Bytes));
app0Index=segments.size();
marker=IOUtils.readShortMM(is);
break;
case APP1:
length=IOUtils.readUnsignedShortMM(is);
byte[] temp=new byte[length - 2];
IOUtils.readFully(is,temp);
if (metadataMap.get(MetadataType.XMP) != null && temp.length >= XMP_EXT_ID.length() && new String(temp,0,XMP_EXT_ID.length()).equals(XMP_EXT_ID)) {
;
}
 else if (metadataMap.get(MetadataType.XMP) != null && temp.length >= XMP_ID.length() && new String(temp,0,XMP_ID.length()).equals(XMP_ID)) {
;
}
 else if (exif != null && temp.length >= EXIF_ID.length() && new String(temp,0,EXIF_ID.length()).equals(EXIF_ID)) {
exifIndex=segments.size();
}
 else {
segments.add(new Segment(emarker,length,temp));
}
marker=IOUtils.readShortMM(is);
break;
case APP2:
length=IOUtils.readUnsignedShortMM(is);
byte[] icc_profile_buf=new byte[length - 2];
IOUtils.readFully(is,icc_profile_buf);
if (metadataMap.get(MetadataType.ICC_PROFILE) != null && icc_profile_buf.length >= ICC_PROFILE_ID.length() && new String(icc_profile_buf,0,ICC_PROFILE_ID.length()).equals(ICC_PROFILE_ID)) {
;
}
 else {
segments.add(new Segment(emarker,length,icc_profile_buf));
}
marker=IOUtils.readShortMM(is);
break;
case APP13:
if (eightBIMStream == null) eightBIMStream=new ByteArrayOutputStream();
readAPP13(is,eightBIMStream);
marker=IOUtils.readShortMM(is);
break;
default :
length=IOUtils.readUnsignedShortMM(is);
byte[] buf=new byte[length - 2];
IOUtils.readFully(is,buf);
if (emarker == Marker.UNKNOWN) segments.add(new UnknownSegment(marker,length,buf));
 else segments.add(new Segment(emarker,length,buf));
marker=IOUtils.readShortMM(is);
}
}
}
if (is instanceof RandomAccessInputStream) ((RandomAccessInputStream)is).shallowClose();
}