public byte[] writeIptcBlock(List<IptcRecord> elements, final boolean forceUtf8Encoding) throws ImagingException, IOException {
        Charset charset;
        if (forceUtf8Encoding) {
            // Using UTF-8 is forced
            charset = StandardCharsets.UTF_8;
        } else {
            // Check if all values can be converted to bytes with DEFAULT_CHARSET,
            // otherwise use UTF-8
            charset = DEFAULT_CHARSET;
            for (final IptcRecord element : elements) {
                final byte[] recordData = element.getValue().getBytes(charset);
                if (!new String(recordData, charset).equals(element.getValue())) {
                    charset = StandardCharsets.UTF_8;
                    break;
                }
            }
        }
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (AbstractBinaryOutputStream bos = AbstractBinaryOutputStream.create(baos, getByteOrder())) {
            if (!charset.equals(DEFAULT_CHARSET)) {
                bos.write(IptcConstants.IPTC_RECORD_TAG_MARKER);
                bos.write(IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER);
                bos.write(ENV_TAG_CODED_CHARACTER_SET);
                final byte[] codedCharset = CHARACTER_ESCAPE_SEQUENCE;
                bos.write2Bytes(codedCharset.length);
                bos.write(codedCharset);
            }

            // first, right record version record
            bos.write(IptcConstants.IPTC_RECORD_TAG_MARKER);
            bos.write(IptcConstants.IPTC_APPLICATION_2_RECORD_NUMBER);
            bos.write(IptcTypes.RECORD_VERSION.type); // record version record
                                                      // type.
            bos.write2Bytes(2); // record version record size
            bos.write2Bytes(2); // record version value

            // make a copy of the list.
            elements = new ArrayList<>(elements);

            // sort the list. Records must be in numerical order.
            final Comparator<IptcRecord> comparator = (e1, e2) -> e2.iptcType.getType() - e1.iptcType.getType();
            elements.sort(comparator);
            // TODO: make sure order right

            // write the list.
            for (final IptcRecord element : elements) {
                if (element.iptcType == IptcTypes.RECORD_VERSION) {
                    continue; // ignore
                }

                bos.write(IptcConstants.IPTC_RECORD_TAG_MARKER);
                bos.write(IptcConstants.IPTC_APPLICATION_2_RECORD_NUMBER);
                if (element.iptcType.getType() < 0 || element.iptcType.getType() > 0xff) {
                    throw new ImagingException("Invalid record type: " + element.iptcType.getType());
                }
                bos.write(element.iptcType.getType());

                final byte[] recordData = element.getValue().getBytes(charset);
                /*
                 * if (!new String(recordData, charset).equals(element.getValue())) { throw new ImageWriteException( "Invalid record value, not " +
                 * charset.name()); }
                 */

                bos.write2Bytes(recordData.length);
                bos.write(recordData);
            }
        }

        return baos.toByteArray();
    }
Constructs a new instance with the default, big-endian, byte order.
src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java