protected List insertAfterLastAppSegments(List segments, List newSegments)
            throws ImageWriteException
    {
        int lastAppIndex = -1;
        for (int i = 0; i < segments.size(); i++)
        {
            JFIFPiece piece = (JFIFPiece) segments.get(i);
            if (!(piece instanceof JFIFPieceSegment))
                continue;

            JFIFPieceSegment segment = (JFIFPieceSegment) piece;
            if (segment.isAppSegment())
                lastAppIndex = i;
        }

        List result = new ArrayList(segments);
        if (lastAppIndex == -1)
        {
            if(segments.size()<1)
                throw new ImageWriteException("JPEG file has no APP segments.");
            result.addAll(1, newSegments);
        }
        else
        result.addAll(lastAppIndex + 1, newSegments);

        return result;
    }