public void updateXmpXml(final ByteSource byteSource, final OutputStream os, final String xmpXml) throws ImagingException, IOException {
        final JFIFPieces jfifPieces = analyzeJfif(byteSource);
        List<JFIFPiece> pieces = jfifPieces.pieces;
        pieces = removeXmpSegments(pieces);

        final List<JFIFPieceSegment> newPieces = new ArrayList<>();
        final byte[] xmpXmlBytes = xmpXml.getBytes(StandardCharsets.UTF_8);
        int index = 0;
        while (index < xmpXmlBytes.length) {
            final int segmentSize = Math.min(xmpXmlBytes.length, JpegConstants.MAX_SEGMENT_SIZE);
            final byte[] segmentData = writeXmpSegment(xmpXmlBytes, index, segmentSize);
            newPieces.add(new JFIFPieceSegment(JpegConstants.JPEG_APP1_MARKER, segmentData));
            index += segmentSize;
        }

        pieces = insertAfterLastAppSegments(pieces, newPieces);

        writeSegments(os, pieces);
    }
Reads a JPEG image, replaces the XMP XML and writes the result to a stream.

@param byteSource ByteSource containing JPEG image data.
@param os         OutputStream to write the image to.
@param xmpXml     String containing XMP XML.
@throws ImagingException if it fails to read or write the JFIF segments
@throws IOException      if it fails to read or write the data from the segments
src/main/java/org/apache/commons/imaging/formats/jpeg/xmp/JpegXmpRewriter.java