public void writeIptc(final ByteSource byteSource, final OutputStream os, PhotoshopApp13Data newData)
            throws ImagingException, IOException, ImagingException {
        final JFIFPieces jfifPieces = analyzeJfif(byteSource);
        final List<JFIFPiece> oldPieces = jfifPieces.pieces;
        final List<JFIFPiece> photoshopApp13Segments = findPhotoshopApp13Segments(oldPieces);

        if (photoshopApp13Segments.size() > 1) {
            throw new ImagingException("Image contains more than one Photoshop App13 segment.");
        }
        List<JFIFPiece> newPieces = removePhotoshopApp13Segments(oldPieces);

        {
            // discard old iptc blocks.
            final List<IptcBlock> newBlocks = newData.getNonIptcBlocks();
            final byte[] newBlockBytes = new IptcParser().writeIptcBlock(newData.getRecords(), newData.isForceUtf8Encoding());

            final int blockType = IptcConstants.IMAGE_RESOURCE_BLOCK_IPTC_DATA;
            final byte[] blockNameBytes = ImagingConstants.EMPTY_BYTE_ARRAY;
            final IptcBlock newBlock = new IptcBlock(blockType, blockNameBytes, newBlockBytes);
            newBlocks.add(newBlock);

            newData = new PhotoshopApp13Data(newData.getRecords(), newBlocks, newData.isForceUtf8Encoding());

            final byte[] segmentBytes = new IptcParser().writePhotoshopApp13Segment(newData);
            final JFIFPieceSegment newSegment = new JFIFPieceSegment(JpegConstants.JPEG_APP13_MARKER, segmentBytes);

            newPieces = insertAfterLastAppSegments(newPieces, Arrays.asList(newSegment));
        }

        writeSegments(os, newPieces);
    }
Reads a JPEG image, replaces the IPTC data in the App13 segment but leaves the other data in that segment (if present) unchanged and writes the result to
a stream.

@param byteSource ByteSource containing JPEG image data.
@param os         OutputStream to write the image to.
@param newData    structure containing IPTC data.
@throws ImagingException if there are more than one Photoshop App13 segment, or if the Photoshop segment cannot be parsed
@throws IOException      if it fails to read from the origin byte source, or to write to the target byte source
@throws ImagingException if it fails to write the target image
src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/JpegIptcRewriter.java