public void updateExifMetadataLossless(final ByteSource byteSource, final OutputStream os, final TiffOutputSet outputSet)
            throws ImagingException, IOException, ImagingException {
        // List outputDirectories = outputSet.getDirectories();
        final JFIFPieces jfifPieces = analyzeJfif(byteSource);
        final List<JFIFPiece> pieces = jfifPieces.pieces;

        final AbstractTiffImageWriter writer;
        // Just use first APP1 segment for now.
        // Multiple APP1 segments are rare and poorly supported.
        if (!jfifPieces.exifPieces.isEmpty()) {
            final JFIFPieceSegment exifPiece = (JFIFPieceSegment) jfifPieces.exifPieces.get(0);

            byte[] exifBytes = exifPiece.segmentData;
            exifBytes = remainingBytes("trimmed exif bytes", exifBytes, 6);

            writer = new TiffImageWriterLossless(outputSet.byteOrder, exifBytes);

        } else {
            writer = new TiffImageWriterLossy(outputSet.byteOrder);
        }

        final boolean includeEXIFPrefix = true;
        final byte[] newBytes = writeExifSegment(writer, outputSet, includeEXIFPrefix);

        writeSegmentsReplacingExif(os, pieces, newBytes);
    }
Reads a JPEG image, replaces the EXIF metadata and writes the result to a stream.

<p>
Note that this uses the "Lossless" approach - in order to preserve data embedded in the EXIF segment that it can't parse (such as Maker Notes), this
algorithm avoids overwriting any part of the original segment that it couldn't parse. This can cause the EXIF segment to grow with each update, which is
a serious issue, since all EXIF data must fit in a single APP1 segment of the JPEG image.
</p>

@param byteSource ByteSource containing JPEG image data.
@param os         OutputStream to write the image to.
@param outputSet  TiffOutputSet containing the EXIF data to write.
@throws ImagingException if it fails to read the JFIF segments
@throws IOException      if it fails to read the image data
@throws ImagingException if it fails to write the updated data
src/main/java/org/apache/commons/imaging/formats/jpeg/exif/ExifRewriter.java